Do not use arrays. Design and implement class (called MyRectangle) that implemen
ID: 3723002 • Letter: D
Question
Do not use arrays. Design and implement class (called MyRectangle) that implements the following 3 methods:
Method isValid() returns true if the sum of the width and height is greater than 30
public static boolean isValid(double width, double height)
Method Area returns the area of the rectangle if it is a valid rectangle
public static double Area(double width, double height)
Method Perimeter returns the perimeter of the rectangle if it is a valid rectangle
public static double Perimeter(double width, double height)
Now, design and implement another class (called TestMyRectangle) to test class MyRectangle methods. The main method of class TestMyRectangle prompts the user to enter the width and height of a rectangle and uses class MyRectangle methods to print out a message followed by the area and perimeter if the rectangle is valid. Otherwise, it prints out only the message “This is invalid rectangle”. Note that method isvalid is used to validate the input before attempting to compute the area and perimeter. Save both classes in the same folder. Design the main method such that it allows the user to re-run the program with different inputs (as we did in the previous assignment using a sentinel loop structure).
Document your code and organize the outputs properly using escape characters as shown below.
Enter width: 4
Enter height: 5
This is invalid rectangle
Enter width: 20
Enter height: 15
This is valid rectangle
The area is 300
The perimeter is 70
Enter width: 10
Enter height: 5
This is invalid rectangle
Enter width: 30
Enter height: 6
This is valid rectangle
The area is 180
The perimeter is 72
Explanation / Answer
MyRectangle.java
class MyRectangle{
//check whether given rectangle is valid or not using static isValid method
public static boolean isValid(double width, double height){
//checking whther valid or not if valid return true else false
if(width+height>30)
return true;
return false;
}
//find out area of given rectangle width and height
public static double Area(double width, double height){
return width*height;
}
//find out perimeter of given rectangle width and height
public static double Perimeter(double width, double height){
return 2*(width+height);
}
}
//TestMyRectangle.java
import java.util.Scanner;
public class TestMyRectangle {
public static void main(String[] args) {
//for user input
Scanner in = new Scanner(System.in);
//sentinal loop
while(true){
System.out.print("Enter width: ");
double width = in.nextInt();
System.out.print("Enter height: ");
double height = in.nextInt();
//checking given width and height valid or not
if (MyRectangle.isValid(width, height)) {
System.out.println("This is valid rectangle");
//computing area and perimeter
double area = MyRectangle.Area(width, height);
double perimeter = MyRectangle.Perimeter(width, height);
System.out.printf("The area is %.0f " , area);
System.out.printf("The Perimeter is %.0f ", perimeter);
}
else{
//printing error message
System.out.println("Invalid rectangle");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.