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

The attached program is your starting point for this assignment. The Geometry.ja

ID: 3715774 • Letter: T

Question

The attached program is your starting point for this assignment. The Geometry.java program will compile, but when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. You will need to create one per the following:

Above the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will simply print out instructions for the user with a menu of options for the user to choose from. The menu should appear to the user as:

This is a geometry calculator, Choose what you would like to calculate:

Find the area of a rectangle

Find the perimeter of a rectangle

Find the perimeter of a triangle

Enter the number of your choice:

Add a line in the main method that calls the printMenu method as indicated by the comments.

Compile, debug, and run. You should be able to choose any option, but you will always get 0 for the answer. We will fix this in the next task.

Write a static method called rectangleArea that takes in the length and width of the rectangle and returns the area using the formula A = lw (ie. Length * width).

Write a static method called rectanglePerimeter that takes in the length and the width of the rectangle and returns the perimeter of the rectangle using the

formula P = 2l +2w.

Write a static method called trianglePerimeter that takes in the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.

Add lines in the main method in the Geometry class which will call these methods. The comments indicate where to place the method calls.

Explanation / Answer

Geometry.java

import java.util.Scanner;

/**

This program demonstrates static methods

*/

public class Geometry

{

public static void main (String [] args)

{

int choice; //the user's choice

double value; //the value returned from the method

char letter; //the Y or N from the user's decision to exit

double length; //the length of the rectangle

double width; //the width of the rectangle

double side1; //the first side of the triangle

double side2; //the second side of the triangle

double side3; //the third side of the triangle

//create a scanner object to read from the keyboard

Scanner keyboard = new Scanner (System.in);

//do loop was chose to allow the menu to be displayed first

letter = 'y';

while (letter == 'Y' || letter == 'y') {

printMenu();

// add call to printMenu here

choice = keyboard.nextInt();

value = 0.0;

switch (choice)

{

case 1:

System.out.print("Enter the length of the rectangle: ");

length = keyboard.nextDouble();

System.out.print("Enter the width of the rectangle: ");

width = keyboard.nextDouble();

// add call to rectangleArea method here

value = rectangleArea(length, width);

System.out.println("The area of the rectangle is " + value);

break;

case 2: // add prompts for inputs as in case 1 above

System.out.print("Enter the length of the rectangle: ");

length = keyboard.nextDouble();

System.out.print("Enter the width of the rectangle: ");

width = keyboard.nextDouble();

// add call to rectanglePerimeter method here

value = rectanglePerimeter(length, width);

System.out.println("The perimeter of the rectangle is " + value);

break;

case 3:

// add prompts for inputs as in case 1 above

System.out.print("Enter the side1 of the triangle: ");

side1 = keyboard.nextDouble();

System.out.print("Enter the side2 of the triangle: ");

side2 = keyboard.nextDouble();

System.out.print("Enter the side3 of the triangle: ");

side3 = keyboard.nextDouble();

value = trianglePerimeter(side1, side2, side3);

// add call to trianglePerimeter method here

System.out.println("The perimeter of the triangle is " + value);

break;

default:

System.out.println("You did not enter a valid choice.");

}

keyboard.nextLine(); //consumes the new line character after the number

System.out.println("Do you want to exit the program (Y/N)?: ");

String answer = keyboard.nextLine();

letter = answer.charAt(0);

}

}

// code for the three required static methods goes here

// add call to rectangleArea method here

public static double rectangleArea(double length, double width) {

return length*width;

}

// add call to rectanglePerimeter method here

public static double rectanglePerimeter(double length, double width) {

return 2 * (length+width);

}

// add call to trianglePerimeter method here

public static double trianglePerimeter(double side1, double side2, double side3) {

return side1+side2+side3;

}

public static void printMenu() {

System.out.println("1. Find the area of a rectangle 2. Find the perimeter of a rectangle 3. Find the perimeter of a triangle Enter the number of your choice:");

}

}

Output:

1. Find the area of a rectangle
2. Find the perimeter of a rectangle
3. Find the perimeter of a triangle
Enter the number of your choice:
1
Enter the length of the rectangle: 2
Enter the width of the rectangle: 3
The area of the rectangle is 6.0
Do you want to exit the program (Y/N)?:  
y
1. Find the area of a rectangle
2. Find the perimeter of a rectangle
3. Find the perimeter of a triangle
Enter the number of your choice:
2
Enter the length of the rectangle: 4
Enter the width of the rectangle: 5
The perimeter of the rectangle is 18.0
Do you want to exit the program (Y/N)?:  
y
1. Find the area of a rectangle
2. Find the perimeter of a rectangle
3. Find the perimeter of a triangle
Enter the number of your choice:
3
Enter the side1 of the triangle: 3
Enter the side2 of the triangle: 4
Enter the side3 of the triangle: 5
The perimeter of the triangle is 12.0
Do you want to exit the program (Y/N)?:  
n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote