Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaHow to Write Robust Programs with the Help of Loops in Java?

How to Write Robust Programs with the Help of Loops in Java?

Here we will discuss how we can write effective codes with the help of loops. It is a general perception that the approach using loops is treated as naive approach to solve a problem statement. But still, there is a huge scope of improvisation here. So basically, a loop statement allows us to execute a statement or group of statements multiple times. In addition to this, we can also manipulate its execution according to our requirements and write robust codes. 

Suppose you want to find the area and perimeter of a  circle. The general approach for a rookie programmer would be to check if the radius is greater than zero. If it is not then print enter non zero positive number. Now, what are the flaws here? If the user enters a number less then or equal to zero multiple times then we need to compile our code again and again since the program will print enter non zero positive number and finish its execution. 

Example 1

Java




// Java Program to Simply Get the Area of Shapes
// Without considering robustness into play
  
// Importing Scanner class to
// read input
import java.util.Scanner;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object of Scanner class to
        // take input
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Radius::");
        double radius = s.nextDouble();
        double perimeter;
        double area;
  
        // If radius is negative
        if (radius <= 0) {
  
            System.out.println(
                "please enter non zero positive number ");
        }
  
        // Radius is positive
        else {
  
            // Compute area and perimeter
            perimeter = 2 * Math.PI * radius;
            area = Math.PI * radius * radius;
  
            // Print and display area and parameter
            System.out.println("Perimeter:: " + perimeter);
            System.out.println("Area:: " + area);
        }
    }
}


Output:

C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
-5
please enter non zero positive number
//Execution Finished
C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
0
please enter non zero positive number
//Execution Finished

Output explanation:

In the above example, we saw that If the user enters any non zero and negative integers than we need to compile and run the program again and again. This awkward situation can be prevented using while loop. Using loops we can manipulate its execution according to our requirements and write healthy codes. 

Example 2

Java




// Java program to Illustrate Robustness In a Program
// Using Loops
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
        // Custom input - Radius of a circle
        double radius = 50;
  
        // Reading input
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
  
        // Asking from user to enter radius
        System.out.println("Enter Radius::");
  
        // 1st.This while loop will run since
        // 50>Integer.MIN_VALUE
        while (radius > Integer.MIN_VALUE) {
  
            // 2nd.It's time to read the buffer
            radius = Double.parseDouble(br.readLine()); //**
  
            // 3rd.If radius>0 give result and break the
            // loop.
            if (radius > 0) {
                double Perimeter = 2 * Math.PI * radius;
                double Area = Math.PI * Math.pow(radius, 2);
                System.out.println("Perimeter:: "
                                   + Perimeter);
                System.out.println("Area:: " + Area);
                break;
            }
  
            // 4th.If radius=-100(say) then print the
            // following
            System.out.println(
                "please enter non zero positive number");
            System.out.println("Enter Radius::");
  
            // 5th.As -100>Integer.MIN_VALUE the loop will
            // run again.
        }
    }
}


Output:

C:\Users\USER\Desktop\Network Java>javac GFG.java
C:\Users\USER\Desktop\Network Java>java GFG
Enter Radius::
-10
please enter non zero positive number
Enter Radius::
0
please enter non zero positive number
Enter Radius::
3
Perimeter:: 18.84955592153876
Area:: 28.274333882308138
//Execution Finished

Note: In the above program, we see if we read the buffer once then, it will no longer store anything. If we try to read it again then it will ask for input.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments