A method called absoluteValue, which takes as its only parameter a value of type double, and
returns a value of type double. The parameter represents an arbitrary number, and the value returned
by the method represents the absolute value of this arbitrary number. The absolute value of a number
x, denoted |x|, is x if x is greater than or equal to 0, and -x if x is less than 0. For example, suppose the
following method call is executed:
double value = MathAssignment.absoluteValue(-42.0);
After the execution of the above method call, the value stored in variable value will be 42.0, as the
absolute value of -42 is 42.
3. Now write the main method and call both methods with appropriate arguments. Make sure that you
call both methods with enough valid values to show their
public class MathAssignment{
public static void main(String args[]) {
double value = MathAssignment.absoluteValue(Integer.parseInt(args[0]));
System.out.println(value);
}
static double absoluteValue(double assignment){
if(assignment < 0){
return assignment * -1;
}
return assignment;
}
}
Comments
Leave a comment