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

Overview of Progranm This program will focus on using Math class methods as an i

ID: 3722574 • Letter: O

Question

Overview of Progranm This program will focus on using Math class methods as an introduction to using an Application Programming Interface (API). An APl is a way of communicating with more complex code through simpler interfaces. An analogy from ht Imagine you're sitting at a table in a restaurant with a menu of choices to order from. The kitchen is the part of the "system that will prepare your order. What is missing is the critical link to communicate your order to the kitchen and deliver your food back to your table. That's where the waiter or API comes in. The waiter is the messenger-or API-that takes your request or order and tells the kitchen-the system -what to do. Then the waiter delivers the response back to you; in this case, it is the food. Using Google, you can find out about Java classes and methods using the online API documentation. Usually, the first result that comes up is the official Oracle documentation about Java. Depending on the documentation, you may find sections for Constructor Summary Method Summary and/or Field Summary The Method Summary section of the Math documentation will look something like this: Method Summary All Methods Static Methods Concrete Meth Modifier and Type Method and Description static double abs(double a) Returns the absolute value of a double value. This gives you a quick summary of the Math method called abs .It requires one parameter, a double and returns a double value. A short description of the method will be provided, and clicking on the method name will provide a more detailed description.

Explanation / Answer

import java.util.*;

class MathWork {
  
   /**
   * Calculates Distance between two points Point 1 and Point 2
   * @param x1: X coordinate of Point 1
   * @param y1: Y coordinate of Point 1
   * @param x2: X coordinate of Point 2
   * @param y2: Y coordinate of Point 2
   * @return distance as double value
   */
   public static double calculateDistance(double x1, double y1, double x2, double y2 ){
       double distance = Math.sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1-y2));
       return distance;
   }
  
  
    public static void main(String args[]) {
       Scanner sc= new Scanner(System.in);
       double angle;
      
       // Loop till we get valid value for angle
       do {
           System.out.print("Enter an angle (0-360): ");
           angle = sc.nextDouble();
       } while (angle <0 || angle > 360);
      
       angle = angle*Math.PI/180; // convert angle to radians
      
       System.out.println(); // Leave a Blank Line
       System.out.printf("Sin: %.4f ",Math.sin(angle)); // Output Sin value
       System.out.printf("Cos: %.4f ",Math.cos(angle)); // Output Cos Value
       System.out.printf("Tan: %.4f ",Math.tan(angle)); // Output Tan Value
       System.out.println();
      
       // Input Values: x1, y1, x2, y2, x3, y3
       double x1, x2, x3, y1, y2, y3;
       System.out.print("Enter x1: ");
       x1 = sc.nextDouble();
       System.out.print("Enter y1: ");
       y1 = sc.nextDouble();
       System.out.print("Enter x2: ");
       x2 = sc.nextDouble();
       System.out.print("Enter y2: ");
       y2 = sc.nextDouble();
       System.out.print("Enter x3: ");
       x3 = sc.nextDouble();
       System.out.print("Enter y3: ");
       y3 = sc.nextDouble();
      
       double distance1 = calculateDistance(x1, y1, x2, y2); // distance between point 1 and point 2
       double distance2 = calculateDistance(x1, y1, x3, y3); // distance between point 1 and point 3
       double distance3 = calculateDistance(x2, y2, x3, y3); // distance between point 2 and point 3
      
       // Output distances
       System.out.println();
       System.out.printf("Distance 1: %.4f ",distance1);
       System.out.printf("Distance 2: %.4f ",distance2);
       System.out.printf("Distance 3: %.4f ",distance3);
      
       System.out.println();
       // Finding longest distance using max function
       double longestDistance = Math.max(distance1, Math.max(distance1, distance2));
       System.out.printf("Longest Distance: %.6f ",longestDistance);
       sc.close();
    }
}

***********************************

Sample Input Output 1:

Enter an angle (0-360): 21.12

Sin: 0.3603
Cos: 0.9328
Tan: 0.3863

Enter x1: 5.6
Enter y1: 22.3
Enter x2: -12.2
Enter y2: 0
Enter x3: 0
Enter y3: 7.3

Distance 1: 28.5330
Distance 2: 16.0112
Distance 3: 14.2172

Longest Distance: 28.532963

**************************

Sample Input Output2 :

Enter an angle (0-360): -1
Enter an angle (0-360): -2.12
Enter an angle (0-360): 361.0
Enter an angle (0-360): -3
Enter an angle (0-360): 87712
Enter an angle (0-360): 180

Sin: 0.0000
Cos: -1.0000
Tan: -0.0000

Enter x1: 1
Enter y1: 2
Enter x2: 3
Enter y2: 4
Enter x3: 5
Enter y3: 6

Distance 1: 2.8284
Distance 2: 5.6569
Distance 3: 2.8284

Longest Distance: 5.656854