Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment will be to allow the user to input the width and length of a rec

ID: 3554706 • Letter: T

Question

This assignment will be to allow the user to input the width and length of a rectangle. The program will then calculate the perimeter of the rectangle and the area of the rectangle. Then, the program will output all four dimensions (width, length, perimeter, and area) in a user-friendly dialog. Finally, the program will ask the user if he/she wants to calculate another rectangle. If so, the program will repeat the above process. If not, the program will say goodbye and stop. CODE COMMENTS PLEASE!


Instructions

In this assignment, students will develop two classes. The first class will contain:

Explanation / Answer

import java.util.Scanner;

/**
*
* @author Steves
*/
public class RectangleTest {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);
RectangleTest test = new RectangleTest();
Rectangle r = new Rectangle();
String option = new String();
test.sayGreeting();
System.out.println("Would you like to continue Y/N?");
option = s.next();
do {

if (option.equalsIgnoreCase("y")) {
System.out.println("Enter width of your Rectangle");
r.setWidth(s.nextDouble());
System.out.println("Enter height of your Rectangle");
r.setHeight(s.nextDouble());
test.dispalyResult(r);
System.out.println("Do you want to calculate another rectangle Y/N?");
option = s.next();
} else {

test.sayGoodBye();
break;
}
} while (!option.equalsIgnoreCase("n"));

r.calculateArea(r.height, r.width);
r.calculatePerimeter(r.height, r.width);
}

public void dispalyResult(Rectangle r) {
System.out.println("The width you entered is:" + r.width);
System.out.println("The height you entered is:" + r.height);
System.out.println("The area of your rectangle is: " + r.calculateArea(r.width, r.height));
System.out.println("The perimeter of your rectangle is: " + r.calculatePerimeter(r.width, r.height));

}

public void sayGreeting() {
System.out.println("Welcome to the Rectangle Calculator.");

System.out.println(" This program will accept input for the width and length of triangles, then calculate the area perimeter of each rectangle");
}

public void sayGoodBye() {
System.out.println("Thank you for using the Rectangle Calculator. Goodbye.");
}
}

class Rectangle {

double width, height;

public Rectangle() {
}

public double calculateArea(double w, double h) {
return (w * h);
}

public double calculatePerimeter(double w, double h) {
return (2 * (w + h));
}

public void setHeight(double height) {
this.height = height;
}

public void setWidth(double width) {
this.width = width;
}
}