Create a MyMath classAdd a method to calculate the area of a

Create a MyMath class:Add a method to calculate the area of a circle.

PI needs to be defined to three decimal places.

Add a method to determine the larger of two integers.

Do not use the Math class max method().

Make both methods static.

Do not ask for any input in the MyMath class. Do not create a Scanner object. Both methods should take parameters and return a value.

Create a Test class:

Do not create a MyMath object.

Gather all user input in the Test class. Do not ask for any input in the MyMath class.

Create a menu system:

Ask the user which calculation from the MyMath class to use.

Add a default message in case the user enters an invalid option.

Depending on the users choice, call the appropriate method from the MyMath class.

Display the results. Include an appropriate output message.

Solution

import java.text.DecimalFormat;

public class MyMath {

   /**
   * method to return area of circle
   *
   * @param radius
   * @return
   */
   public static double getCircleArea(double radius) {
       DecimalFormat decimalFormat = new DecimalFormat(\"#.000\");
       double area = Double
               .parseDouble(decimalFormat.format(radius * Math.PI));
       return area;
   }

   /**
   * method to return max number
   *
   * @param num1
   * @param num2
   * @return
   */
   public static int getMax(int num1, int num2) {
       if (num1 < num2)
           return num2;
       else
           return num1;
   }

}

import java.util.Scanner;

public class MathTest {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);

           do {
               int choice;
               System.out.println(\"***Menu***\");
               System.out.println(\"1. Area\");
               System.out.println(\"2. Max\");
               System.out.println(\"0. Exit\");
               System.out.print(\"Enter your choce:\");
               choice = scanner.nextInt();
               if (choice == 0)
                   break;
               switch (choice) {
               case 1: {
                   System.out.print(\"Enter the radius :\");
                   double radius = scanner.nextDouble();
                   System.out.println(\"Area of Circle:\"
                           + MyMath.getCircleArea(radius));
               }
                   break;
               case 2: {
                   System.out.print(\"Enter the First number:\");
                   int num1 = scanner.nextInt();
                   System.out.print(\"Enter the Second number:\");
                   int num2 = scanner.nextInt();
                   System.out.println(\"Max of Two numbers:\"
                           + MyMath.getMax(num1, num2));
               }
                   break;

               default:
                   System.out.println(\"Invalid Choice Selected!\");
                   break;
               }
           } while (true);

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

OUTPUT:

***Menu***
1. Area
2. Max
0. Exit
Enter your choce:1
Enter the radius :5
Area of Circle:15.708
***Menu***
1. Area
2. Max
0. Exit
Enter your choce:2
Enter the First number:6
Enter the Second number:5
Max of Two numbers:6
***Menu***
1. Area
2. Max
0. Exit
Enter your choce:6
Invalid Choice Selected!
***Menu***
1. Area
2. Max
0. Exit
Enter your choce:0

Create a MyMath class:Add a method to calculate the area of a circle. PI needs to be defined to three decimal places. Add a method to determine the larger of tw
Create a MyMath class:Add a method to calculate the area of a circle. PI needs to be defined to three decimal places. Add a method to determine the larger of tw
Create a MyMath class:Add a method to calculate the area of a circle. PI needs to be defined to three decimal places. Add a method to determine the larger of tw

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site