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
***********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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started