(The rectangle class) Following the example of the circle class in 8.2, a class
ID: 3634189 • Letter: #
Question
(The rectangle class) Following the example of the circle class in 8.2, a class named rectangle to represent a rectangle. The class contains: Two double data fields names width and height that specify the width and height of the rectangle. The default values are 1 for both width and height, A constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height, A method names get Area O that returns the area of this rectangle. A method named get Perimeter O that returns the perimeter. Draw the UML diagram for the class. Implement the class. Wright a test program that two Rectangle objects- one with width 4 and height 40 and the with width 3.5 and height 35.9. Display the width, height, area, and of each rectangle in this order.Explanation / Answer
please rate - thanks
message me if any problems
import java.util.Scanner;
public class rectangletest
{
public static void main (String[] args)
{Scanner in=new Scanner(System.in);
double h,w;
System.out.println("for rectangle 1: ");
System.out.print("Enter height: ");
h=in.nextDouble();
System.out.print("Enter width: ");
w=in.nextDouble();
Rectangle r1= new Rectangle(w,h);
System.out.println("for rectangle 2: ");
System.out.print("Enter height: ");
h=in.nextDouble();
System.out.print("Enter width: ");
w=in.nextDouble();
Rectangle r2= new Rectangle(w,h);
System.out.println();
System.out.println("rectangle 1:");
System.out.println("width: "+r1.getWidth());
System.out.println("height: "+r1.getHeight());
System.out.println("area: "+r1.getArea());
System.out.println("perimeter: "+r1.getPerimeter());
System.out.println();
System.out.println("rectangle 2:");
System.out.println("width: "+r2.getWidth());
System.out.println("height: "+r2.getHeight());
System.out.println("area: "+r2.getArea());
System.out.println("perimeter: "+r2.getPerimeter());
}
}
--------------------------------------------
public class Rectangle
{
private double width;
private double height;
public Rectangle()
{width=1;
height=1;
}
public Rectangle(double w,double h )
{width=w;
height=h;
}
public double getPerimeter()
{return 2*height+ 2*width;
}
public double getArea()
{return height*width;
}
public double getHeight()
{return height;
}
public double getWidth()
{return width;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.