): Create a class Rectangle with attributes length and width, each of which defa
ID: 3852178 • Letter: #
Question
): Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle’s perimeter and area. Use set and get methods for both length and width. The set methods will verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.
Notes:
1. In the test class write code to create and print a menu that returns a value corresponding to the menu choice.
1. Set length
2. set width
3. Exit
Choice:
2. Also in the test file, remember to code a Try Catch Illegial Argument Expection to determine if the user entered a length and width of 0.0-20.
Explanation / Answer
Rectangle.java
public class Rectangle {
// Declaring instance variables
private float length;
private float width;
// Zero argumented constructor
public Rectangle() {
this.length = 1;
this.width = 1;
}
// getters and setters
public float getLength() {
return length;
}
public void setLength(float length) {
if (length < 0.0 || length > 20.0)
throw new IllegalArgumentException("** Invalid Length value **");
else
this.length = length;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
if (width < 0.0 || width > 20.0)
throw new IllegalArgumentException("** Invalid Width value **");
else
this.width = width;
}
// Method which calculate the area of rectangle
public void calArea() {
System.out.println("Area of Rectangle :" + length * width);
}
// Method which calculate the perimeter of rectangle
public void calPerimeter() {
System.out.println("Perimeter of Rectangle :" + (2 * (length + width)));
}
}
__________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int choice;
float length, width;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Creating an Rectangle object
Rectangle r = new Rectangle();
//This loop continues to execute until the user selects exit
while (true) {
//Displaying the menu
System.out.println("Menu choice:");
System.out.println("1. Set length");
System.out.println("2. set width");
System.out.println("3. Exit");
//Getting the choice entered by the user
System.out.print("Choice:");
choice = sc.nextInt();
if (choice < 1 || choice > 3) {
System.out.println("Invalid Input");
continue;
}
switch (choice) {
case 1: {
System.out.print("Enter length :");
length = sc.nextFloat();
try {
r.setLength(length);
} catch (Exception e) {
System.out.println(e);
}
continue;
}
case 2: {
System.out.print("Enter width :");
width = sc.nextFloat();
try {
r.setWidth(width);
} catch (Exception e) {
System.out.println(e);
}
continue;
}
case 3: {
break;
}
}
break;
}
//calling the area and perimeter of an Rectangle
r.calArea();
r.calPerimeter();
}
}
____________________
Output:
Menu choice:
1. Set length
2. set width
3. Exit
Choice:1
Enter length :45
java.lang.IllegalArgumentException: ** Invalid Length value **
Menu choice:
1. Set length
2. set width
3. Exit
Choice:2
Enter width :55
java.lang.IllegalArgumentException: ** Invalid Width value **
Menu choice:
1. Set length
2. set width
3. Exit
Choice:1
Enter length :12
Menu choice:
1. Set length
2. set width
3. Exit
Choice:2
Enter width :13
Menu choice:
1. Set length
2. set width
3. Exit
Choice:3
Area of Rectangle :156.0
Perimeter of Rectangle :50.0
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.