Write a Program Area of Rectangle in Java- Technology369kk

 Area of Rectangle: 

In this post we are discussing  about area of rectangle using java programming language, I'm just suggest you pls create a file of java and run class file in cmd runner . 


Program:   


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.*;  //All in one

class AreaR{
    public static void main(String[] args){
        int width = 0;
        int length = 0;
   
    try {
        // read the length from console

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Please enter length of a rectangle: -");
            width = Integer.parseInt(br.readLine());

            System.out.print("Please enter width of a rectangle: -");
            length = Integer.parseInt(br.readLine());

    }
    // if Invailed value was entered

    catch(NumberFormatException ne){
            System.out.println("Invailed value" + ne);
            System.exit(0);

    }
    catch(IOException ioe){
            System.out.println("IO Error: "+ ioe);
            System.exit(0);
    }
        int area = length*width;
        System.out.println("Area of Rectangle value is:" + area);

    }
}



Explanations Program:  

1. Import Statements


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
  • BufferedReader and InputStreamReader:
    • Used for reading input from the console. 
  • IOException:
    • Handles input/output errors. 
  • NumberFromatException:
    • Handles error if a non-integer value is entered. 

2. Variable Declaration: 

int width = 0;
int length = 0;
   

  • width and length:
    • Store the dimensions of the rectangle, initialized to 0. 

3. Reading User Input 


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter length of a rectangle: -");
width = Integer.parseInt(br.readLine());

System.out.print("Please enter width of a rectangle: -");
length = Integer.parseInt(br.readLine());

  • BufferedReader and InputStreamReader:
    • Create a pipeline to read user input from the console.
  • System.out.print:
    • Prompts the user to enter the rectangle's length and width.
  • Integer.pareInt(): 
    • Converts the string input (from br.readLine()) into an integer.

4. Error Hanfling(Try-Catch Block)

The program uses two catch blocks to handle exceptions:
a. catch(NumberFormateException  ne)

catch(NumberFormatException ne){
            System.out.println("Invailed value" + ne);
            System.exit(0);

    }


  • Triggered if the user enters a non-numeric value.
  • Prints an error message: "Invalid value" and exits the program.

  • b. catch(IOException ioe)  


    catch(IOException ioe){
                System.out.println("IO Error: "+ ioe);
                System.exit(0);
        }

    • Triggered if an I/O error occurs while reading input.
    • Prints "IO Error" and exits the program.

    5. Calculate and Print the Area

    int area = length*width;
    System.out.println("Area of Rectangle value is:" + area);


  • area = length * width;:
    • Computes the rectangle's area.
  • System.out.println:
    • Displays the calculated area.
  • OUTPUT



    PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\AllPraQue> java AreaR  
    Please enter length of a rectangle: -5
    Please enter width of a rectangle: -10
    Area of Rectangle value is:50



    Key Points:

    1. Exception Handling:

      • Prevents the program from crashing due to invalid user input.
    2. BufferedReader:

      • Efficient way to read console input as strings.
    3. Type Conversion:

      • Converts user input from string to integer using Integer.parseInt().
    4. Multiplication Logic:

      • Calculates the area using the formula length * width.

    I hope 


    Post a Comment

    0 Comments