Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangles perimeter and area. Use

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.

1. In the test class write code to create and print a menu that returns a value corresponding to the menu choice.

student submitted image, transcription available below

2. Also in the test file, remember to code a Try Catch Illegal Argument Expection to determine if the user entered a length and width of 0.0-20.

The user is supposed to input the values of the length and width and then the area and perimeter will be calculated. I have most of the code typed out but I am having a problem finishing the RectangleTest code. I do not know how to finish the catch argument so it will call the IllegalArgumentException from the Rectangle class, telling the user that the "Length/width must be between 0 and 20." I do not know if I need to add anything to that class in order to do that.

This is what I have so far:

public class Rectangle {

private float length, width;

public Rectangle(){
this.length = (float) 1.0;
this.width = (float) 1.0;
}

// Method to set length
public void setLength(float length){
if (length<0.0 && length>20.0)
this.length = length;
else throw new IllegalArgumentException("Length must be between 0.0 and 20.0");
}

// Method to retrive length
public float getLength(){
return length;
}

// Method to set width
public void setWidth(float width){
if (width>0.0 && width<20.0)
this.width = width;
else throw new IllegalArgumentException("Width must be between 0.0 and 20.0");
}

// Method to get width
public float getWidth(){
return width;
}

public float perimeter(){
return 2*(this.length+this.width);
}

public float area(){
return this.length*this.width;
}

// Method to display rectangle values
public void display(){
System.out.println("Rectangle Length: "+this.length+ "Rectangle Width: "+this.width+
"Retangle Area: "+area()+ "Rectangle Perimeter: "+perimeter());

}
}
public class RectangleTest {

public static void main(String[] args){

// Create a new scanner object to obtain input from the command window
Scanner input = new Scanner(System.in);

try{
Rectangle r=new Rectangle();

System.out.println("1.Set Length 2.SetWidth 3.Exit Choice");
int choice = input.nextInt();

switch(choice){
case 1: System.out.println("Enter length of rectangle: ");
float length = input.nextInt();
r.setLength((float) length);
break;

case 2: System.out.println("Enter width of rectangle: ");
float width = input.nextInt();
r.setWidth((float) width);
break;

case 3: System.exit(0);
break;

default: System.out.println("Invalid option");
}

r.display();
}catch(IllegalArgumentException e){
 

1. Set Length 2. Set Width 3. Exit Choice:

Step by Step Solution

3.51 Rating (144 Votes )

There are 3 Steps involved in it

Step: 1

RectangleTestjava Java program to test the Rectangle class import javautilScanner public class Recta... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems analysis and design

Authors: kenneth e. kendall, julie e. kendall

8th Edition

135094909, 013608916X, 9780135094907, 978-0136089162

More Books

Students also viewed these Programming questions

Question

What is the process for validating data entered into fields?

Answered: 1 week ago

Question

When should attributes be used in an XML document?

Answered: 1 week ago

Question

List in shorthand notation the six basic query types.

Answered: 1 week ago