***********IN JAVA************* Design and implement a class name Rectangle to r
ID: 3872254 • Letter: #
Question
***********IN JAVA*************
Design and implement a class name Rectangle to represent rectangle. The class contains:
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.
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 perimeter using default values.
Test Case
Input
1
1
Output
1.0
1.0
1.00
4.00
*******Use the following class driver with no modifications to it!********
import java.util.*;
import java.lang.*;
import java.io.*;
class DriverMain{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle testRectangle = new Rectangle(w, h);
System.out.println(testRectangle.getWidth());
System.out.println(testRectangle.getHeight());
System.out.printf("%.2f",testRectangle.getArea());
System.out.println();
System.out.printf("%.2f",testRectangle.getPerimeter());
System.out.println();
}
}
***********************************************************************************
---And use the following class that was refered to in main----
class Rectangle{
}
***********************************************************************************
Leaving comments in the code would be much appreciated to help me understand the code more!
Explanation / Answer
Rectangle.java
public class Rectangle {
// Declaring instance variables
private double width;
private double height;
// Parameterized constructor
public Rectangle(double width, double height) {
super();
setWidth(width);
setHeight(height);
}
// getters and setters
public double getWidth() {
return width;
}
public void setWidth(double width) {
if(width<0)
this.width=1;
else
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
if(height<0)
this.height=1;
else
this.height = height;
}
// This method will calculate and return the area of the rectangle
public Object getArea() {
return width * height;
}
// This method will calculate and return the perimeter of the rectangle
public Object getPerimeter() {
return 2 * (width + height);
}
}
________________
DriverMain.java
import java.util.*;
import java.lang.*;
import java.io.*;
class DriverMain {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
double h = input.nextDouble();
Rectangle testRectangle = new Rectangle(w, h);
System.out.println(testRectangle.getWidth());
System.out.println(testRectangle.getHeight());
System.out.printf("%.2f", testRectangle.getArea());
System.out.println();
System.out.printf("%.2f", testRectangle.getPerimeter());
System.out.println();
}
}
_________________
Output:
1
1
1.0
1.0
1.00
4.00
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.