Question
Task #1 void Methods 1. Copy the file Geometry.java to your NetBeans source directory. This program will compile and run. 2. Above the main method,
Task #1 void Methods
1. Copy the file Geometry.java to your NetBeans source directory. This program will compile and run.
2. 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
1. Find the area of a circle
2. Find the area of a rectangle
3. Find the area of a triangle
4. Find the circumference of a circle
5. Find the perimeter of a rectangle
6. Find the perimeter of a triangle
Enter the number of your choice:
3. Add a line in the main method that calls the printMenu method as indicated by the comments.
4. Compile, debug, and run. You should be able to choose any option, but you will always get 0 for the answer except the first one that has example for you. We will fix this in the next task.
Task #2 Value-Returning Methods
1. Write a static method called circleArea that takes in the radius of the circle and returns the area using the formula A = X r X r .
2. 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.
3. Write a static method called triangleArea that takes in the base and height of the triangle and returns the area using the formula A = bh.
4. Write a static method called circleCircumference that takes in the radius of the circle and returns the circumference using the formula C = 2r.
5. 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.
6. 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.
Task #3 Calling Methods
Add lines in the main method in the Geometry class which will call these methods. The comments indicate where to place the method calls. Compile, debug, and run. Test out the program using your sample data.
import java.util.Scanner;
/**
This program demonstrates static methods
*/
public class Geometry
{
/**
The printMenu method prints the menu for the user to choose from
*/
public static void printMenu ()
{
System.out.println("This is a geometry calculator");
System.out.println("Choose what you would like to calculate");
System.out.println("1. Find the area of a circle");
// To do - Add more menu items mentioned on Task #1
}
/**
The circleArea method returns the area of the circle
@param radius The radius of the circle
@return The area of the circle
*/
public static double circleArea(double radius)
{
return Math.PI * radius * radius;
}
// To do - Add more method follow the style of circleArea for Task #2
public static void main (String [] args)
{
int choice; //the user's choice
double value = 0.0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double height; //the height of the triangle
double base; //the base of the triangle
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
do
{
printMenu();
choice = keyboard.nextInt();
switch (choice)
{
case 1:
System.out.print("Enter the radius of the circle: ");
radius = keyboard.nextDouble();
value = circleArea(radius);
System.out.println("The area of the circle is " + value);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
//To do for Task #3 - call the rectangleArea method and store the result in the value variable
// follow the style in case 1
System.out.println("The area of the rectangle is " + value);
break;
case 3:
System.out.print("Enter the height of the triangle: ");
height = keyboard.nextDouble();
System.out.print("Enter the base of the triangle: ");
base = keyboard.nextDouble();
//To do for Task #3 - call the triangleArea method and store the result in the value variable
// follow the style in case 1
System.out.println("The area of the triangle is " + value);
break;
case 4:
System.out.print("Enter the radius of the circle: ");
radius = keyboard.nextDouble();
//To do for Task #3 - call the circumference method and store the result in the value variable
// follow the style in case 1
System.out.println("The circumference of the circle is " + value);
break;
case 5:
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
//To do for Task #3 - call the perimeter method and store the result in the value variable
// follow the style in case 1
System.out.println("The perimeter of the rectangle is " + value);
break;
case 6:
System.out.print("Enter the length of side 1 of the triangle: ");
side1 = keyboard.nextDouble();
System.out.print("Enter the length of side 2 of the triangle: ");
side2 = keyboard.nextDouble();
System.out.print("Enter the length of side 3 of the triangle: ");
side3 = keyboard.nextDouble();
//To do for Task #3 - call the perimeter method and store the result in the value variable
// follow the style in case 1
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);
}while (letter != 'Y' && letter != 'y');
}
} // End of the class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started