Ads

Monday, December 28, 2020

Write a program to print the area of a rectangle by creating a class named Area; having two methods. First method named as "setValues ()" that takes length and breadth of rectangle as parameters and the second method named as "getArea ()" that returns the area of the rectangle. Length and breadth of rectangle should be entered through keyboard.




Here's the program to solve the problem as described:

Source Code..


import java.util.Scanner;

 

public class Area {

    double length;

    double breadth;

   

    public void setValues(double length, double breadth) {

        this.length = length;

        this.breadth = breadth;

    }

   

    public double getArea() {

        return length * breadth;

    }

   

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        Area rectangle = new Area();

       

        System.out.print("Enter length of rectangle: ");

        double length = scanner.nextDouble();

       

        System.out.print("Enter breadth of rectangle: ");

        double breadth = scanner.nextDouble();

       

        rectangle.setValues(length, breadth);

        double area = rectangle.getArea();

       

        System.out.println("Area of rectangle is: " + area);

       

        scanner.close();

    }

}


explaining the program in detail.

 

First, we import the Scanner class from the java.util package. We need the Scanner class to get user input from the console.

import java.util.Scanner;

Next, we define a class named Area. Inside the class, we define two instance variables, length and breadth. These variables will hold the length and breadth of the rectangle

 

public class Area {

    double length;

    double breadth;

   

    // methods will be defined here...

}

 

After defining the instance variables, we define two methods inside the Area class.

The first method is named setValues. This method takes two parameters, length and breadth, and sets the instance variables length and breadth to these values

 

public void setValues(double length, double breadth) {

    this.length = length;

    this.breadth = breadth;

}

 

The second method is named getArea. This method returns the area of the rectangle by multiplying the length and breadth instance variables.

 

public double getArea() {

    return length * breadth;

}

 

After defining the Area class and its methods, we define the main method. This method is the entry point of our program.

Inside the main method, we create an instance of the Area class named rectangle.

Area rectangle = new Area();

 

We then use a Scanner object to get user input for the length and breadth of the rectangle.

 

Scanner scanner = new Scanner(System.in);

 

System.out.print("Enter length of rectangle: ");

double length = scanner.nextDouble();

 

System.out.print("Enter breadth of rectangle: ");

double breadth = scanner.nextDouble();

 

We call the setValues method of the rectangle object to set its length and breadth instance variables.

 

rectangle.setValues(length, breadth);

 

We then call the getArea method of the rectangle object to calculate the area of the rectangle and store it in a variable named area

double area = rectangle.getArea();

Finally, we print the area of the rectangle to the console using System.out.println.

System.out.println("Area of rectangle is: " + area);

 

We also close the Scanner object to release its resources

scanner.close();

 

That's it! The program prompts the user to enter the length and breadth of the rectangle, calculates its area, and prints the result to the console.




No comments:

Post a Comment