The Math class, which is part of the Java standard class library, provides various class methods that perform
common calculations, such as trigonometric functions.
Write a class called MathAssignment, which provides some of the functionality which is also provided by
the Math class. This class should define the static methods described below:
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.
public class MathAssignment {
public static double absoluteValue(double value){
double absoluteValue;
if(value >= 0){
absoluteValue = value;
}
else{
absoluteValue = -value;
}
return absoluteValue;
}
}
Comments
Leave a comment