Ask Question
5 July, 23:07

Write a test program that creates two Rectangle objects-one with width 4 and height 40 and the other with width 3.5 and height 35.7. Display the width, height, area, and perimeter of each rectangle in this order.

+5
Answers (1)
  1. 6 July, 01:20
    0
    public class Rectangle {

    private double width;

    private double heigth;

    public Rectangle (double width, double heigth) {

    this. width = width;

    this. heigth = heigth;

    }

    public double getWidth () {

    return width;

    }

    public void setWidth (double width) {

    this. width = width;

    }

    public double getHeigth () {

    return heigth;

    }

    public void setHeigth (double heigth) {

    this. heigth = heigth;

    }

    public double perimeter (double width, double heigth) {

    double peri = 2 * (width+heigth);

    return peri;

    }

    public double area (double width, double heigth) {

    double area = width*heigth;

    return area;

    }

    }

    class RectangleTest{

    public static void main (String[] args) {

    //Creating two Rectangle objects

    Rectangle rectangle1 = new Rectangle (4,40);

    Rectangle rectangle2 = new Rectangle (3.5, 35.7);

    //Calling methods on the first Rectangel objects

    System. out. println ("The Height of Rectangle 1 is: "+rectangle1. getHeigth ());

    System. out. println ("The Width of Rectangle 1 is: "+rectangle1. getWidth ());

    System. out. println ("The Perimeter of Rectangle 1 is: "+rectangle1. perimeter (4,40));

    System. out. println ("The Area of Rectangle 1 is: "+rectangle1. area (4,40));

    / / Second Rectangle object

    System. out. println ("The Height of Rectangle 2 is: "+rectangle2. getHeigth ());

    System. out. println ("The Width of Rectangle 2 is: "+rectangle2. getWidth ());

    System. out. println ("The Perimeter of Rectangle 2 is: "+rectangle2. perimeter (4,40));

    System. out. println ("The Area of Rectangle 2 is: "+rectangle2. area (4,40));

    }

    }

    Explanation:

    Firstly A Rectangle class is created with two fields for width and heigth, a constructor and getters and setters the class also has methods for finding area and perimeters Then a RectangleTest class containing a main method is created and two Rectangle objects are created (Follow teh comments in the code) Methods to get height, width, area and perimeter are called on each rectangle object to print the appropriate value
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a test program that creates two Rectangle objects-one with width 4 and height 40 and the other with width 3.5 and height 35.7. ...” 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