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. It

"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. It has set and get methods for both length and width. The set methods should 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."

it needs to be in a format where the user is prompt to select either entering the width or length or to exit.

my code so far:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package pkg3513_cw_rectangle2;

import java.util.Scanner;

/** * * @author */ public class Rectangle {

private double length; private double width;

public Rectangle() { length = 1; width = 1; }

public Rectangle(double a, double b) { length = a; width = b; }

public double getLength() { if (length > 0.0 && length < 20.0) { return length; } else { System.err.println("Length out of range, must be >0 & <20"); return length; } }

public double getWidth() { if (width > 0.0 && width < 20.0) { return width; } else { System.err.println("Width out of range, must be >0 & <20"); return width; } }

public void setLength(double l) { if (l > 0.0 && l < 20.0) { length = l; } }

public void setWidth(double w) { if (w > 0.0 && w < 20.0) { width = w; } }

public double getArea() { if ((length > 0.0 && length < 20.0) && (width > 0.0 && width < 20.0)) { return width * length; } else { return 0.0; } }

public double getPerimeter() { if ((length > 0.0 && length < 20.0) && (width > 0.0 && width < 20.0)) { return 2 * (width + length); } else { return 0.0; } } }

class testRectangle {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); double width; double length; int choice = 0; Rectangle rect; rect = new Rectangle(length, width); System.out.println("1. Set Length"); System.out.println("2. Set Width"); System.out.println("3. Exit"); System.out.println("Choice: "); choice = input.nextInt();

while (choice != 3) { switch (choice) { case 1: System.out.println(" Enter Length: "); length = input.nextDouble(); rect.setLength(length); System.out.println("Length : " + rect.getLength() + " Width: " + rect.getWidth() + " Perimeter: " + rect.getPerimeter() + " Area: " + rect.getArea() +" ");

break;

case 2: System.out.println(" Enter Width: "); width = input.nextDouble(); rect.setWidth(width); System.out.println("Length : " + rect.getLength() + " Width: " + rect.getWidth() + " Perimeter: " + rect.getPerimeter() + " Area: " + rect.getArea() +" ");

break;

} System.out.println("1. Set Length"); System.out.println("2. Set Width"); System.out.println("3. Exit"); System.out.println("Choice: "); choice = input.nextInt(); }

} }

i'm getting errors.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!