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

Task #1 void Methods 1. Copy the file Geometry.java (Code Listing 5.1) from the

ID: 3676722 • Letter: T

Question

Task #1 void Methods

1. Copy the file Geometry.java (Code Listing 5.1) from the Student CD or as directed by your instructor. 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.

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

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:

a. A one line summary of what the method does.

b. A description of what the program requires to operate and what the result of that operation is.

c. @param listing and describing each of the parameters in the parameter list (if any).

d. @return describing the information that is returned to the calling statement (if any).

2. Generate the documentation. Check the method summary and the method details to ensure your comments were put into the Java Documentation correctly.

Code Listing 5.1 (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 = 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 }

Explanation / Answer

Geometry.java

package org.students;

import java.util.Scanner;
/**
* <h1>Geometry</h1>
* <p>
* The Geometry program that will take the option from the user
* depending upon that it will perform the appropriate calculation
* and displays the output to the user.
* </p>
* @author Author Name
* @version 1.0
* @since Date
*/
public class Geometry {
   static float radius=0;
   static float length=0;
   static float width=0;
   static float height=0;
   static float base=0;
   static float side1=0;
   static float side2=0;
   static float side3=0;
   /**
       * It will Display the Menu.
       * @param args Unused.
* @return Nothing.
       */
   public static void display() {
       System.out.println("This is a geometry calculator Choose what you would like to calculate.");
       System.out.println("1. Find the area of a circle.");
       System.out.println("2. Find the area of a rectangle.");
       System.out.println("3. Find the area of a triangle.");
       System.out.println("4. Find the circumference of a circle.");
       System.out.println("5. Find the perimeter of a rectangle.");
       System.out.println("6. Find the perimeter of a triangle.");
       System.out.println(" ");
       System.out.print("Enter the number of your choice:");
   }
   /**
       * It will calculate the area of the circle.
       * @param args radius.
* @return area of the circle.
       */
   public static double circleArea(float radius) {
       double carea=Math.PI*radius*radius;
       return carea;
      
       }
   /**
       * It will calculate the area of the rectangle.
       * @param args length,width.
* @return area of the rectangle.
       */
   public static double rectangleArea(float length,float width) {
           double rarea=length*width;
           return rarea;
             
           }
       /**
           * It will calculate the area of the triangle.
           * @param args base,height.
   * @return area of the triangle.
           */
       public static double triangleArea(float base,float height) {
               double tarea=0.5*base*height;
               return tarea;
           }
           /**
           * It will calculate the circumference of the circle.
           * @param args radius.
   * @return circumference of the circle.
           */
           public static double circleCircumference(float radius) {
               double ccircum=2*Math.PI*radius;
               return ccircum;
           }
           /**
           * It will calculate the perimeter of the rectangle.
           * @param args length,width.
   * @return perimeter of the rectangle.
           */
           public static double rectanglePerimeter(float length,float width) {
               double rperi=2*(length+width);
               return rperi;
           }
           /**
           * It will calculate the perimeter of the traingle.
           * @param args side1,side2,side3.
   * @return perimeter of the rectangle.
           */
           public static double trianglePerimeter(float side1,float side2,float side3) {
               double tperi=(side1+side2+side3);
               return tperi;
           }
          
   public static void main(String[] args) {
       double circlearea=0.0,rectarea=0.0,triarea=0.0,circumc=0.0,perirect=0.0,peritri=0.0;
      
      
   while(true)
   {
       display();
       //Which is used to get the input given by the user.
           Scanner sc=new Scanner(System.in);
       int choice=   sc.nextInt();
       //which will the execute appropriate case depending upon user selection.
       switch(choice)
       {
       case 1:
      
       System.out.print("Enter the radius of the circle:");
       radius = sc.nextFloat();
       circlearea=circleArea(radius);
       System.out.println(" ");
       System.out.println("The area of the circle is " + circlearea);
       break;
      
       case 2:
          
       System.out.print("Enter the length of the rectangle: ");
       length = sc.nextFloat();
       System.out.print("Enter the width of the rectangle: ");
       width = sc.nextFloat();
       rectarea=rectangleArea(length,width);
       System.out.println(" ");
       System.out.println("The area of the rectangle is " + rectarea);
       break;
      
       case 3:
          
       System.out.print("Enter the height of the triangle: ");
       height = sc.nextFloat();
       System.out.print("Enter the base of the triangle: ");
       base = sc.nextFloat();
       triarea=triangleArea(base,height);
       System.out.println(" ");
       System.out.println("The area of the triangle is " +triarea);
       break;
      
       case 4:
          
       System.out.print("Enter the radius of the circle: ");
       radius = sc.nextFloat();
       circumc=circleCircumference(radius);
       System.out.println(" ");
       System.out.println("The circumference of the circle is " +circumc);
       break;
      
       case 5:
          
       System.out.print("Enter the length of the rectangle: ");
       length = sc.nextFloat();
       System.out.print("Enter the width of the rectangle: ");
       width = sc.nextFloat();
       perirect=rectanglePerimeter(length,width);
       System.out.println(" ");
       System.out.println("The perimeter of the rectangle is " +perirect);
       break;
      
       case 6:
          
       System.out.print("Enter the length of side 1 of the " + "triangle: ");
       side1 = sc.nextFloat();
       System.out.print("Enter the length of side 2 of the " + "triangle: ");
       side2 = sc.nextFloat();
       System.out.print("Enter the length of side 3 of the " + "triangle: ");
       side3 = sc.nextFloat();
       peritri=trianglePerimeter(side1,side2,side3);
       System.out.println(" ");
       System.out.println("The perimeter of the triangle is " + peritri);
       break;
      
       default: System.out.println("You did not enter a valid choice.");
       }
       System.out.println(" ");
       System.out.print("Do you want to continue(press Y/N):");
           char ch = sc.next(".").charAt(0);
           //If the user enter 'y' or 'Y' the program continue to execute display the menu again.
           if(ch == 'y' || ch == 'Y')
           {
               continue;
         
       }else{
           System.out.println("* Program Exit *");
       break;}
      
          
          
         
         
  
      
       }
          
          
  
   }
  
   }

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