Question

JAVA Code Requried Copy the file java included below. This program will compile, but, when you...

  1. JAVA Code Requried
  2. Copy the file java included below. This 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. We will need to create this.
  3. Below 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 rectangle6. Find the perimeter of a triangle Enter the number of your choice:
  1. Add a line in the main method that calls the printMenu method as indicated by the comments.
  2. 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.

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 = π r 2.
  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 = 2πr.
  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

  1. Add lines in the main method in the GeometryDemo class which will call these methods. The comments indicate where to place the method calls.
  2. Below, write some sample data and hand calculated results for you to test all 6 menu items.
  3. Compile, debug, and run. Test out the program using your sample data.

Task #4 Java Documentation

  1. Write javadoc comments for each of the 7 static methods you just wrote.

They should include:

  1. A one line summary of what the method does.
  2. A description of what the program requires to operate and what the result of that operation is.
  3. @param listing and describing each of the parameters in the parameter list (if any).
  4. @return describing the information that is returned to the calling statement (if any).
  1. Generate the documentation. Check the method summary and the method details to ensure your comments were put into the Java Documentation correctly.

CODE IS BELOW

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 = 0; // The method's return value

char letter; // The user's Y or N decision

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);

// The do loop allows the menu to be displayed first

do

{

// TASK #1 Call the printMenu method

choice = keyboard.nextInt();

switch(choice)

{

case 1:

System.out.print("Enter the radius of " +

"the circle: ");

radius = keyboard.nextDouble();

// TASK #3 Call the circleArea method and

// store the result in the value variable

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();

// TASK #3 Call the rectangleArea method and

// store the result in the value variable

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();

// TASK #3 Call the triangleArea method and

// store the result in the value variable

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();

// TASK #3 Call the circumference method and

// store the result in the value variable

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();

// TASK #3 Call the perimeter method and

// store the result in the value variable

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();

// TASK #3 Call the perimeter method and

// store the result in the value variable

System.out.println("The perimeter of " +

"the triangle is " +

value);

break;

default:

System.out.println("You did not enter " +

"a valid choice.");

}

keyboard.nextLine(); // Consume the new line

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');

}

// TASK #1 Create the printMenu method here

// TASK #2 Create the value-returning methods here

// TASK #4 Write javadoc comments for each method

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

In the below program we implement the geometry calculator by displaying menu to user to select which operation to perform. By getting user choice we are performing the related operation. Operations we are performing are area of circle, area of rectangle, area of triangle, circumference of circle, perimeter of rectangle, perimeter of triangle. For each operation we are defining separate static methods. we also included sample test data at the end of program for reference.

Source Code:

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 = 0; // The method's return value
       char letter; // The user's Y or N decision
       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);
       // The do loop allows the menu to be displayed first
       do
       {
       // TASK #1 Call the printMenu method
           printMenu();
           choice = keyboard.nextInt();
           switch(choice)
           {
               case 1:
                   System.out.print("Enter the radius of " +
                   "the circle: ");
                   radius = keyboard.nextDouble();
                   // TASK #3 Call the circleArea method and
                   // store the result in the value variable
                   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();
                   // TASK #3 Call the rectangleArea method and
                   // store the result in the value variable
                   value = rectangleArea(length, width);
                   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();
                   // TASK #3 Call the triangleArea method and
                   // store the result in the value variable
                   value = triangleArea(base, height);
                   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();
                   // TASK #3 Call the circumference method and
                   // store the result in the value variable
                   value = circleCircumference(radius);
                   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();
                   // TASK #3 Call the perimeter method and
                   // store the result in the value variable
                   value = rectanglePerimeter(length, width);
                   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();
                   // TASK #3 Call the perimeter method and
                   // store the result in the value variable
                   value = trianglePerimeter(side1, side2, side3);
                   System.out.println("The perimeter of " +
                   "the triangle is " +
                   value);
                   break;
               default:
                   System.out.println("You did not enter " +
                   "a valid choice.");
           }
           keyboard.nextLine(); // Consume the new line
           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');
   }
   // TASK #1 Create the printMenu method here
   /**
* This is the menu method which displays menu items to user to select.
* @param Nothing.
* @return Nothing.
*/
   public static void printMenu(){
       System.out.println("\nThis is a geometry calculator");
       System.out.println("Choose what you would like to calculate");
       System.out.println("1 for Find the area of a circle\n2 for Find the area of a rectangle\n3 for Find the area of a triangle\n4 for Find the circumference of a circle\n5 for Find the perimeter of a rectangle\n6 for Find the perimeter of a triangle\n");
       System.out.println("Enter the number of your choice : ");
   }
  
// TASK #2 Create the value-returning methods here
   /**
* This is the circleArea method which calculates the area of a circle by taking radius of circle from user.
* @param radius radius of the circle.
* @return area of a circle by pi * radius * radius.
*/
   public static double circleArea(double radius){
       return Math.PI * radius * radius;

   }

   /**
* This is the rectangleArea method which calculates the area of a rectangle by taking length and width of rectangle from user.
* @param length length of the rectangle.
* @param width width of the rectangle.
* @return area of a rectangle by multiplying length and height.
*/
   public static double rectangleArea(double length, double width){
       return length * width;
   }

   /**
* This is the triangleArea method which calculates the area of a triangle by taking base and height of triangle from user.
* @param base base of the triangle.
* @param height height of the triangle.
* @return area of a triangle by multiplying base*height dividedby 2.
*/
   public static double triangleArea(double base, double height){
       return (base*height)/2;
   }

   /**
* This is the circleCircumference method which calculates the circumference of a circle by taking radius of circle from user.
* @param radius radius of the circle.
* @return Circumference of a circle by multiplying 2 * pi * radius.
*/
   public static double circleCircumference(double radius){
       return 2 * Math.PI * radius;
   }

   /**
* This is the rectanglePerimeter method which calculates the perimeter of a rectangle by taking length and width of rectangle.
* @param length length of the rectangle.
* @param width width of the rectangle.
* @return perimeter of a rectanglr by multiplying length * 2 + width * 2.
*/
   public static double rectanglePerimeter(double length, double width){
       return 2 * length + 2 * width;
   }

   /**
* This is the trianglePerimeter method which calculates the perimeter of a triangle by taking 3 sides length of triangle.
* @param side1,side2,side3 side lengths of the rectangle.
* @return perimeter of a triangle by adding 3 sides of triangle.
*/
   public static double trianglePerimeter(double side1, double side2, double side3){
       return side1 + side2 + side3;
   }

   /**
   Area of circle : radius - 4 (area - 50.26548245743669)
   Area of rectangle : length - 5, width - 5 (area - 25.0)
   Area of triangle : base - 5, height - 12 (area - 30.0)
   circleCircumference: radius - 4, (circumference - 25.132741228718345)
   rectanglePerimeter: length - 5, width - 5, (perimeter - 20.0)
   trianglePerimeter: side1 - 5, side2 - 6, side3 - 10, (perimeter - 21)
   */

}

Output:

Compile : javac Geometry.java

Run : java Geometry

This is a geometry calculator

Choose what you would like to calculate

1 for Find the area of a circle

2 for Find the area of a rectangle

3 for Find the area of a triangle

4 for Find the circumference of a circle

5 for Find the perimeter of a rectangle

6 for Find the perimeter of a triangle

Enter the number of your choice :

1

Enter the radius of the circle: 4

The area of the circle is 50.26548245743669

Do you want to exit the program (Y/N)?:

n

This is a geometry calculator

Choose what you would like to calculate

1 for Find the area of a circle

2 for Find the area of a rectangle

3 for Find the area of a triangle

4 for Find the circumference of a circle

5 for Find the perimeter of a rectangle

6 for Find the perimeter of a triangle

Enter the number of your choice :

2

Enter the length of the rectangle: 5

Enter the width of the rectangle: 5

The area of the rectangle is 25.0

Do you want to exit the program (Y/N)?:

n

This is a geometry calculator

Choose what you would like to calculate

1 for Find the area of a circle

2 for Find the area of a rectangle

3 for Find the area of a triangle

4 for Find the circumference of a circle

5 for Find the perimeter of a rectangle

6 for Find the perimeter of a triangle

Enter the number of your choice :

3

Enter the height of the triangle: 12

Enter the base of the triangle: 5

The area of the triangle is 30.0

Do you want to exit the program (Y/N)?:

n

This is a geometry calculator

Choose what you would like to calculate

1 for Find the area of a circle

2 for Find the area of a rectangle

3 for Find the area of a triangle

4 for Find the circumference of a circle

5 for Find the perimeter of a rectangle

6 for Find the perimeter of a triangle

Enter the number of your choice :

4

Enter the radius of the circle: 4

The circumference of the circle is 25.132741228718345

Do you want to exit the program (Y/N)?:

n

This is a geometry calculator

Choose what you would like to calculate

1 for Find the area of a circle

2 for Find the area of a rectangle

3 for Find the area of a triangle

4 for Find the circumference of a circle

5 for Find the perimeter of a rectangle

6 for Find the perimeter of a triangle

Enter the number of your choice :

5

Enter the length of the rectangle: 5

Enter the width of the rectangle: 5

The perimeter of the rectangle is 20.0

Do you want to exit the program (Y/N)?:

y

Add a comment
Know the answer?
Add Answer to:
JAVA Code Requried Copy the file java included below. This program will compile, but, when you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. 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 = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

    In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • I have this program: It passes on certain test cases. The Demo five has the %.1f...

    I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as: Enter length: \n Enter width: \n You entered: 87.3, 2.0, and 174.7\n -- Rectangle info --\n Length: 87.34\n Width: 2.0\n Area: 174.68\n And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you. Program Content:...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • The statement in the following program is in the incorrect order. Rearrange the statements so that...

    The statement in the following program is in the incorrect order. Rearrange the statements so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter, for a circle, it outputs the area and circumference; and for a cylinder, it output the volume and surface area. After rearranging the statements, your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT