Implement the trigResult method to return human-friendly text for all combinatio
ID: 3809303 • Letter: I
Question
Implement the trigResult method to return human-friendly text for all combinations of operations/angles (see the JavaDoc for details). You will earn 5 points for successfully implementing this method. (Note: there are much better ways to do this than a giant if-else statement)
I just need help with the final trigResult method
/**
* Potentially useful map of trig function names
* and their associated functions
*/
public static final Map<String, Function<Double, Double>> OPS = new LinkedHashMap<>();
static {
OPS.put("sin", x -> Math.sin(x));
OPS.put("cos", x -> Math.cos(x));
OPS.put("tan", x -> Math.tan(x));
}
/**
* Potentially useful list of pertinent angles
*/
public static final int[] ANGLES = {0, 30, 45, 60, 90};
/**
* Output for tan(90 degrees)
*/
public static final String UNDEFINED = "undefined";
/**
* Potentially useful map of the numerical
* result of trig operations and associated
* human-friendly text
*/
public static final Map<Double, String> NUMERICAL_RESULTS = new HashMap<>();
static {
NUMERICAL_RESULTS.put( 0., "0" );
NUMERICAL_RESULTS.put( 1., "1" );
NUMERICAL_RESULTS.put( 0.5, "1 / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 2. ) / 2 ), "sqrt(2) / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) / 2. ), "sqrt(3) / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) / 3. ), "sqrt(3) / 3" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) ), "sqrt(3)" );
}
/**
* Returns the result of performing
* a trig operation on a supplied
* angle (in degrees)
*
* @param op sin/cos/tan
* @param angle 0/30/45/60/90
* @return human-readable result
*/
public static String trigResult(String op, int angle) {
return "";
}
Explanation / Answer
Hi, Please find my inplementation of required method.
Please let me know in case of any issue.
public static final Map<String, Function<Double, Double>> OPS = new LinkedHashMap<>();
static {
OPS.put("sin", x -> Math.sin(x));
OPS.put("cos", x -> Math.cos(x));
OPS.put("tan", x -> Math.tan(x));
}
/**
* Potentially useful list of pertinent angles
*/
public static final int[] ANGLES = {0, 30, 45, 60, 90};
/**
* Output for tan(90 degrees)
*/
public static final String UNDEFINED = "undefined";
/**
* Potentially useful map of the numerical
* result of trig operations and associated
* human-friendly text
*/
public static final Map<Double, String> NUMERICAL_RESULTS = new HashMap<>();
static {
NUMERICAL_RESULTS.put( 0., "0" );
NUMERICAL_RESULTS.put( 1., "1" );
NUMERICAL_RESULTS.put( 0.5, "1 / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 2. ) / 2 ), "sqrt(2) / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) / 2. ), "sqrt(3) / 2" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) / 3. ), "sqrt(3) / 3" );
NUMERICAL_RESULTS.put( ( Math.sqrt( 3. ) ), "sqrt(3)" );
}
/**
* Returns the result of performing
* a trig operation on a supplied
* angle (in degrees)
*
* @param op sin/cos/tan
* @param angle 0/30/45/60/90
* @return human-readable result
*/
public static String trigResult(String op, int angle) {
double value = 0;
double radian = Math.toRadians(angle);
switch(op){
case "sin":
value = Math.sin(radian);
break;
case "cos":
value = Math.cos(radian);
break;
case "tan":
value = Math.tan(radian);
break;
default:
return "invalid";
}
return Double.toString(value);
}