Ask Question
5 June, 09:17

Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is the RectangleArea holding the Main (), and the other one is Rectangle. The Rectangle class has three members: a property of length, a property of width, and a method with a name of CalArea () to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize the rectangle. Calculate and display the area of the rectangle by first initialize an object named myRectangle and then calculate its area using the CalArea () method.

+1
Answers (1)
  1. 5 June, 11:16
    0
    public class RectangleArea {

    public static void main (String[] args) {

    Rectangle myRectangle = new Rectangle (15.8, 7.9);

    double calArea = myRectangle. calcArea (15.8, 7.9);

    System. out. println ("The area is " + calArea);

    }

    }

    class Rectangle{

    private double length;

    private double width;

    //Defined Constructor

    public Rectangle (double length, double width) {

    this. length = length;

    this. width = width;

    }

    public double calcArea (double len, double wi) {

    return len * wi;

    }

    }

    Explanation:

    This is solved with Java programming language Start by creating both classes RectangleArea and and Rectangle SInce both classes are in the same file, only one can be public Class Rectangle contains the three members are decribed by the question private double length, private double width and the method calcArea (). It also has a sefl-declared constructor. In the class RectangleArea, an object of Rectangle is created and initialized. The method calcArea is called using the created object and the value is printed.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers