Design and implement a class name Rectangle to represent rectangle. The class co
ID: 3586057 • Letter: D
Question
Design and implement a class name Rectangle to represent rectangle. The class contains 1 import java.util.*; 2 import java.lang.*; Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. No-arg constructor that creates a default rectangle A constructor that creates a rectangle with the specified width and height. A method named getArea) that returns the area of this rectangle A method named getPerimeter) that returns the perimeter import java.io.*; 4 class Rectangle 6 A test program will create Rectangle objects. Display the width, height, area, and perimeter of each rectangle in this order. NOTE Display the area and perimete as a floating-point number to 2 decimal places. If either width or height, is zero or negtive value, use the deafult value for both width and height and calculate the area and the 1 class DriverMain perimeter using default values. public static void main(String args[]) Scanner input new Scanner(System.in); double w input.nextDouble(); double h nput.nextDouble(); Test Case 4 6 Rectangle testRectangle = new Rectangle(w, h); System.out.println(testRectangle.getwidth)); System.out.println(testRectangle.getHeight()); System. out. printf("%" 2f", test Rectangle. getArea( ) ) ; System.out.println(); System. out. printf("%" 2f", test Rectangle. getPerimeter() ) ; System.out.println); 1.0 1.0 1.00 4.00 9 10 13 16 18 19 20Explanation / Answer
Rectangle.java
public class Rectangle {
private double width=1.0, height=1.0;
public Rectangle(){
}
public Rectangle (double w, double h){
width = w;
height = h;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.