219k views
5 votes
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.

1 Answer

3 votes

Answer:

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));

}

}

Step-by-step 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
User Kaadzia
by
6.5k points