Java Basics - Lecture 3 - Loops in Java

The Hacks of The Day

Loops in Java














Loops are used to perform a task repeatedly. There are 3 types of loops

  1. while loop
  2. for loop
  3. do-while loop

Suppose if you want to print first 5 digits

You need to write 5 print statements

System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);

Instead of writing repeated code, we use loops

While loop :

A control flow statement which is used to perform the repeated tasks based on a given Boolean condition. In while loop number of iterations is not fixed. Loop will be executed only if the condition is true. It is also called the entry control loop.

Flow Chart Diagram



Disadvantage: It generates an infinite loop if we don't give an incremental part

For Loop:

A control flow statement which is used to perform repeated tasks. But in for loop number of iterations are fixed.

Flow chart Diagram



Parts of for loop (Initialization, Conditional part, Incremental part)

Initialization: In this part, the variable is initialized. It is a starting point of the loop

Condition: In this part condition is checked, it returns a boolean. In the case of the true, the loop body is executed.

Increment/Decrement: Variable is incremented or decremented here for next iteration

Loop termination: When the condition becomes false, then the loop is getting terminated.

For Loop Example




public class ForLoop {
public static void main(String[] args)  {
  for(int j=1;j<=10;j++){
  System.out.println("Values of ==> " + j);
  }
 }
}

Output:
value of j ==> 1
value of j ==> 2
value of j ==> 3
value of j ==> 4
value of j ==> 5
value of j ==> 6
value of j ==> 7
value of j ==> 8
value of j ==> 9
value of j ==> 10
If you want to print 10 digits in reverse order then below code will work fine.

public class ForLoop{
 public static void main(String[] args){
  for(int k=1;k<=10;j--){
  System.out.println("Values of ==> " + k);
    }
  }
}

Post Increment

An operator that is used to increment the value of the variable after the expression is executed in which post-increment is used. The value is first used in an expression and then incremented.
public class PostIncrement{
 public static void main(String[] args){
 int i = 1;
 int j = i++; //post increment

   }
}

Pre Increment

An operator that is used to increment the value of the variable before the expression is executed in which post-increment is used. The value is first incremented in an expression and then used.  
public class PreIncrement{
 public static void main(String[] args){
 int a = 1;
 int b = ++a; //pre-increment
   } 
}

Post Decrement

An operator that is used to decrement the value of the variable before the expression is executed in which post-increment is used. The value is first decremented in an expression and then used.  
public class PostDecrement{
 public static void main(String[] args){
 int i = 2;
 int j = i--; //post decrement
   }
}

Pre Decrement

An operator that is used to decrements the value of the variable before the expression is executed in which post decrement is used. The value is first decremented in an expression and then used.  
public class PreDecrement{
 public static void main(String[] args){
 int a = 2;
 int b = --a; //pre-decrement
 System.out.println("Value of a ==> "+a);
 System.out.println("Value of b ==> "+b); 
  }
}

do-while loop :

The do-While loop is similar to while loop but the difference is that the body of the do-while loop is executed once and then the condition is verified. A control flow statement that executes a piece of code at least once.

Flow Chart Diagram


If the test expression is true, the body of the loop is executed again and the test expression is evaluated.

The first loop body is executed then the condition is checked, this process continues until the condition is false.

This process goes on until the test expression becomes false.

If the test expression is false, the loop ends.


public class DoWhileLoop{
 public static void main(String[] args){
 int counter = 5;
 int factorial = 1;
 do{
    factorial *= counter--;
 }while (counter > 0);
 System.out.println("result " +factorial);
  }
}
Output:
 result ==> 120

Array in Java

Array: Stores similar data type values in an array variable.
  1. A collection of similar data types
  2. The lowest index is always “0”
  3. The highest index is always n-1 (n is the size of the array)
Int array
Integer array stores the integer data type. 
 int arr[] = new int[4];
 arr[0] = 10;
 arr[1] = 20;
 arr[2] = 30;
 arr[3] = 40;
 System.out.println(arr[2]);
 System.out.println(arr[3]);

Output:
value of arr[2] ==> 30
value of arr[3] ==> 40

If you will try to find the arr[4], it will give exception

When you go beyond the limit, an exception will raise “ArrayIndexOutOfBoundsException”

Size of array
The size of the array is determined by using length() method.
System.out.println(arr.length)
Print all the values of the array: use for loop
 for(int i=0; i < arr.length; i++){
 System.out.println(arr[i]);
 }
Double array
Double array stores the double data type. 
double d[] = new double[3];
d[0] = 3;
d[1] = 14.33;
d[2] = 44.89;
Character array
Character array stores the character data type. 
char c[] = new char[3];
c[0] = 'a';
c[1] = '3';
c[2] = '$';
String array
String array stores the string data type. 
String s[] = new String[3];
s[0] = "test";
s[1] = "World";
s[2] = "Hello";
Disadvantages:
  1. The array size is fixed. Therefore it is called a static array
  2. Stores only similar data types
To overcome size problem we use Collections like ArrayList, HashTable
To overcome data type problem we use Object array
Object array(Object is a class) – It is used to store different data types

Object array
 Object obj[] = new Object[6];
 obj[0] = "Tom";
 obj[1] = 25;
 obj[2] = "M";
 obj[3] = 12.33;
 obj[4] = "1/10/1998";
 obj[5] = "London";
Find all elements of Object array
for(int i=0; i<obj.length; i++) {
 System.out.println(obj[i]);
}
Output
value of obj[0] ==> Tom
value of obj[1] ==> 25
value of obj[2] ==> M
value of obj[3] ==> 12.33
value of obj[4] ==> 1/10/1998
value of obj[5] ==> London

2D Array in Java
Collection of data in the form of cells
Represented as a matrix with a number of rows and columns
String x[][] = new String[3][5];
Output:
System.out.println(x.length);
Number of row ==> 3
System.out.println(x[0].length);
Number of columns ==> 5
//1st row

x[0][0] = "A";
x[0][1] = "B";
x[0][2] = "C";
x[0][3] = "D";
x[0][4] = "E";
//2nd row

x[1][0] = "A1";
x[1][1] = "B1";
x[1][2] = "C1";
x[1][3] = "D1";
x[1][4] = "E1";
//3rd row

x[2][0] = "A2";
x[2][1] = "B2";
x[2][2] = "C2";
x[2][3] = "D2";
x[2][4] = "E2";
Output
System.out.println(x[1][2]);
value of x[1][2] ==> C1</
System.out.println(x[2][2]);
value of x[1][2] ==> C2
System.out.println(x[0][4]);
value of x[1][2] ==> E
Print all the values of 2D array
Two for loops are required to get array values
//row = 0, col = 0 to 4
//row = 1, col = 0 to 4
//row = 2, col = 0 to 4

for(int i = 0; i < x.length; i++){
  for(int j = 0; j < x[0].length; j++){
  System.out.println(x[i][j]);
   }
}
Output:

  value of x[0][0] ==> A value of x[0][1] ==> B
  value of x[0][2] ==> C value of x[0][3] ==> D
  value of x[0][4] ==> E

  value of x[1][0] ==> A1 value of x[1][1] ==> B1
  value of x[1][2] ==> C1 value of x[1][3] ==> D1
  value of x[1][4] ==> E1

  value of x[2][0] ==> A2 value of x[2][1] ==> B2
  value of x[2][2] ==> C2 value of x[2][3] ==> D2
  value of x[2][4] ==> E2

Test Cases For Login Page

The Hacks of The Day


















Test Case1: Verify the login functionality with valid Username and Password

Test Case2: Verify the error message with both invalid fields

Test Case3: Verify the error message with invalid Username and valid Password

Test Case4: Verify the error message with valid Username and invalid Password

Test Case5: Verify the “Remember Me” functionality

Test Case6: Verify the “Forgot Password” functionality

Test Case7: Verify if there is a ‘Cancel’ button available to erase the entered text.

Test Case8: Verify that password is showing in the form of an asterisk

Test Case9: Verify that “Enter” key is working fine the login page

Test Case10: Verify if a user cannot enter the characters more than the specified range

in each field (Username and Password).

Test Case11: Verify the login page by pressing the ‘Back button’ of the browser.

Test Case12: Verify the timeout of the functionality of the login session.

Test Case13: Verify the login page and all its controls in different browsers

Test Case14: Verify if a user should not be allowed to log in with different credentials

from the same browser at the same time.

Test Case15: Verify if a user should be able to login with the same credentials in

different browsers at the same time.

Test Case16: Verify the Login page against the SQL injection attack.

Test Case17: Verify the implementation of the SSL certificate.

Test Case18: Verify the time taken to log in with a valid Username and Password.

Test Case19: Verify if the font, text color and color-coding of the Login page is as per

the standard.

Test Case 20: Empty spaces should not be allowed

Different Types of Software Testing

The Hacks of The Day

















Software Testing Types

There are many types of software testing such as Functional Testing, Non-Functional Testing, Automation Testing, Agile Testing, etc. Given below is a list of testing types

Functional Testing types include:

  1. Unit Testing
  2. Integration Testing
  3. Interface Testing
  4. Regression Testing
  5. System Testing
  6. Smoke Testing
  7. Sanity Testing
  8. Beta/Acceptance Testing

Non-functional Testing types include:

  1. Volume Testing
  2. Security Testing
  3. Performance Testing
  4. Compatibility Testing
  5. Install Testing
  6. Recovery Testing
  7. Reliability Testing
  8. Usability Testing

Above given testing, types are used most frequently. Now let's explain the testing types one by one which is mentioned and some other which are not mentioned. In this article, we will cover the most common testing types, not all the testing types.

Unit Testing

Testing type in which small units of an application are being tested is known as unit testing. It is normally done by developers, not by testers. The purpose of unit testing is to verify the code quality.

Smoke Testing

A testing type in which we check all the major functionalities of the application. Before starting detailed testing, testers run a smoke testing cycle to verify the build stability. Smoke testing can decide either it is good to proceed with the build or not.

Sanity Testing

Testing of small changes or bug fixes is called sanity testing. Insanity testing testers only verify the changed things, not the whole application. Suppose a tester finds a bug in the application. After that developer will fix the bug. After the bug is fixed by the developer, the updated build will be given to the tester, and the tester will only verify that bug.

Alpha Testing

A testing technique that is carried out at the developer's site is called alpha testing. Alpha testing is done before releasing the software to the end-user. This testing is carried out when the development phase is completed and software is about to launch. The main reason to run the alpha testing cycle is to identify all possible issues or defects before releasing it into the market or to the end-user.

Beta Testing

Testing which is carried out by customers is called beta testing. Beta testing is done in a real environment and end-users test the application with real data.

Ad-hoc Testing

Testing carried out by testing any flow of the application is called ad-hoc testing. This is also called random testing. Through Adhoc-testing we find the defects and try to break the application by executing any flow of the application or any random functionality. It is done without any plan or documentation.

Acceptance Testing

In acceptance, testing the client verifies whether the end to end the flow of the system is working fine or not. The client accepts the software only when all the features and functionalities work as expected. In acceptance testing client satisfaction is important.

Black Box Testing

Testing of an application's functionality without knowing the internal details is called black-box testing. Internal detail is not considered. Tests are based on the requirements and functionality.

White Box Testing

Testing of the internal structure of code is called white box testing. It is also called glass box testing. To perform white box testing coding knowledge is compulsory. Tests are run to cover the code statements, branches, conditions, etc.

Grey Box Testing

Grey box testing is a combination of both black box testing and white box testing. The aim of this testing is to find possible errors in the code and usage of the application. Both things are covered code and front-end functionality.


Back-end Testing

In back-end testing, data verification is done in the database. Either entered record is saved successfully or not. We can also verify the data retrieval.


Accessibility Testing

It determines that software or application is accessible for disabled persons or not. Disabled means color blind, deaf, mentally disabled, blind, and other groups.


Compatibility Testing

Compatibility testing validates how software behaves in a different environment. like web servers, hardware, operating systems, mobile devices, and browsers.

Functional Testing

Functional testing ignores the internal details. It focuses only on the functionality of the application either it is working as per requirements or not.

Non-Functional Testing

It is a type of testing which involves testing of non-functional requirements such as load testing, stress testing, security testing, recovery testing, volume testing, and system response, etc,


Exploratory Testing

In exploratory testing, the tester explores the application and finds the bug. A type of testing that is performed without any documentation. The application is tested by using it. Tester understands the application flow by using it and finds the defects. It's all about discovery, Investigation, and learning. Test cases are created in advance.


Monkey Testing

A technique in software testing where the user tests the application by giving random inputs and checking the behavior of the application. It aims to check the application behavior when a layman uses the software without any knowledge. No, a document is provided to perform the monkey testing.


Static Testing

Static testing is a type of testing which is performed without executing the application. It involves the review of documents, walkthroughs, and inspection of the deliverables of the project.

Dynamic Testing

Dynamic testing is a type of testing which is performed by running the application or executing the code. The user requirement's verification is done in dynamic testing.

Performance Testing

A type of nonfunctional testing is executed to check the performance of the application. How application behaves under different circumstances like load, stress, and volume.

Load Testing

Load testing is a subtype performance testing, the behavior of the application is determined during different load. The number of concurrent users is increased gradually to verify the system response under different loads.

Stress Testing

It is also a subtype of performance testing. Stress testing is used to determine the behavior of the application beyond the load. Suppose an application supports 5000 maximum number of concurrent users, so for stress testing, we will try with more than 5000 users. Similarly, we can go beyond the limit with storage capacity to check how and when it will crash.

Gorilla Testing

Gorilla testing is a type of testing wherein a module is tested repeatedly to ensure that its functionality is working fine. Developers can also do gorilla testing.

Recovery Testing

A type of testing that validates the recovery of data. How an application recovers data in case of crash or disaster.

Regression Testing

A type of testing that validates the functionality of a system as a whole after some modifications or changes. In regression testing, we verify that existing functionalities are working fine or not.

Security Testing

A type of testing is used to verify the security of a system. A special team performs security testing. It is done by penetrating different combinations of data in a hacking way. It helps us to protect our system from external threats. It also ensures that how our systems are secure from viruses and malicious programs.

Usability Testing

A testing technique is used to verify the look and feel of the software. Application is tested with a user point of view, that how much application is user friendly. A new user can understand easily or not. If an application is user friendly, then it will be easier for new users to use it. The application's navigation systems should be more clear for a layman.

Volume Testing

A type of non-functional testing is performed by a performance testing team. The application is tested by giving large amounts of data. Systems response time and behavior are measured when it comes across a huge volume of data.

Negative Testing

A testing technique which is used to break the system by providing negative inputs. Negative or incorrect data is used to check the system response. How it behaves in case of incorrect data.

Mutation Testing

Mutation testing is a type of white box testing where the source code of one program is changed and existing test cases identify the defects. Changes are done on a small scale so that the entire application's functionality would not be affected.

Happy Path Testing

A type of software testing which ensures that positive flows of application are working fine. No negative inputs are used in happy path testing. The main focus is only on the valid and positive inputs through which application generates the expected results.

Installation Testing

Installation testing is used to validate that systems are installed successfully and its functionality is working as expected.

Reliability Testing

A software testing technique ensures that how much the system is reliable and can it perform failure-free operations for a specific span of time in a particular environment.


Above mentioned testing types are used most commonly. There is still a long list of testing types, but not all testing types are used in all types of projects. We have covered the most common which are used in the software testing life cycle.

Software Testing Hierarchy

The Hacks of The Day














Software Testing Levels

In software testing, we go through different levels of testing. The main purpose of this hierarchy is to verify that the application is working fine and meeting the user requirements. So to produce a quality product we need different levels of testing. The various levels of testing are:

Unit Testing

A type of testing which ensures that all major functionalities of the application are working fine. It is also called “Build Verification Testing”. The purpose of smoke testing is just to ensure that we are good to proceed with the detailed testing.

Component Testing

Component testing is also called as module testing. The difference between the unit testing and component testing is, in-unit testing the developers test their code but in component testing, the whole component is tested. In component testing, we ensure the quality of the whole component.

Integration Testing

Integration testing is carried out when two or more than two modules are integrated, in order to test the behavior and functionality of both the modules after integration to ensure that they are working fine after integration.

System Testing

System Testing is a process that validates the system as a whole. System testing is carried out when all the components are integrated. Testers evaluate the system's compliance with its specified requirements. Testing is done as a System instead of individual components.


Acceptance Testing

Acceptance testing is basically done to ensure that the system is ready to operate in the real environment. 
  • Alpha Testing: Testing that is carried out at the developer's site is called alpha testing. It is done at the end of the development process.
  • Beta Testing: Beta testing is done by end-users. It is done just before the launch of the product.

More Testing Types

  • Regression Testing
  • Buddy Testing
  • Alpha Testing
  • Beta Testing

Summary

  • A level of software testing is a process where every unit or component of a software/system under development is tested.
  • The primary goal of system testing is to evaluate the system's functionality with the specified needs.
  • There are four main levels of testing are Unit Testing, Integration Testing, System Testing, and Acceptance Testing.


Software Testing Basics

The Hacks of The Day

Software Testing Basics: Software testing is a process to check the quality of the software and to reduce the risk of software failure in operation. To execute the application/program with the intent of finding bugs.

Software testing is a process that includes various activities like:

  • Review of SRS document
  • Test Planning
  • Test Case Designing
  • Execution of Test Plan
  • Execution of Test Cases
  • Bug Reporting


Static Testing: A testing technique that is used to test the system without code execution is called static testing.

Dynamic Testing: Testing that involves the execution of a component or system being tested is called dynamic testing.

“Testing also includes reviewing the requirements, user stories, and source code.”

Software Testing also used to validate the System, which is checking whether the system will meet the user's requirements in its operational environment.

Testing and debugging are two different things. Testing is a process that finds defects in the software system. Debugging is done by the developers that find, analyze, and fix the defects.

Why Testing is Necessary?

Software testing is a necessary part to make an application defect free. The following are the main reasons why testing is necessary.

To produce a quality product
To make an application bug free
To meet the user requirements
To check that functionality is working fine
To check the performance of the application
To find out the defects that were made during development

What is Error?

A mistake in coding is called error. A mistake that is made by developers while developing a software component or system. This could happen because of the following reasons

Misunderstanding of software functionality
Miscalculation of values
Misinterpretation of any value

What is Bug/Defect?

An error that is found while testing an application is called a defect. In simple words the variance between the expected and actual result.
Defect or Bug report consists of the following information

Bug ID: Each bug contains a unique identification number.

Bug Title/Summary: A brief description of the bug.

Bug Description: Detailed information on the bug with reproduction steps.

Priority: It can be High/Medium/Low based on the urgency of bug.

Severity: It can be Critical/Major/Minor based on the impact of the bug on application.

Attachment: Screen-shots/Videos are attached for a better description of the bug.

Bug Life Cycle:

A cycle in which a bug goes through during its lifetime. This cycle contains many stages which are given below

New: When a bug is logged in any bug reporting tool, it is given a “New” state.

Assigned: When a bug is posted, then Developer lead or Manager assigns it to the available

developer. It is given an “Assigned” state.

Open: When a developer starts working on that bug, it goes into the “Open” state.

Fixed: When a bug is fixed by the developer then he makes the bug status as “Fixed” and the bug is passed to the testing team.

Retest: After the bug is fixed by the developer, tester retest the changes.

Verified: When a bug is passed by the tester, then the state of the bug changed to “Verified”.

Closed: After PM verification if things are going well, then it gave a state “Closed”.

Duplicate: If the same bug is logged twice or the two bugs mention the same concept of the bug, then one bug status changed to "duplicate".

Rejected: If a bug is not genuine, then the developer rejects the bug. Then the bug goes to the “rejected” state

Deferred: The state of the bug is changed to “deferred” when it does not belong to the current sprint”. It means it will be fixed in the next sprint.

Not a bug: The status is given as "Not a bug" if the tester reports a bug that is not in actual.


What is Failure?

The inability of the software to work in a production environment. The system deviates from the expected results.

Software Testing Basic Questions

What is software testing
Why is software testing important?
When should you do software testing?
How much testing is enough?
Why does software have defects?
When can we stop testing?
When should we start testing?
Can we make an application 100% bug-free?