Write a static method to return a double (parameter) rounded to the nearest 100th. Use the Math class' round method. Put this method in a class called My Math. Hints: multiply the double parameter by 100, round that, divide the rounded result by 100. Write a method to print a number (double parameter), its square and its cube, all rounded to the 100th, calling the round100th method created earlier in My Math class, but put this method in another class called Try My Math. Write a Java application program (main) that calls the method written earlier My Math class, passing Math .E in main. Put main in the Try My Math class.
Source code
import java.lang.Math;
public class TryMyMath{
public static void display(double n){
System.out.println("Number = " + MyMath.round100th(n));
System.out.println("Square = " + MyMath.round100th(n*n));
System.out.println("Cube = " + MyMath.round100th(n*n*n));
}
public static void main( String [] args ){
display(Math.E);
}
}
class MyMath{
public static double round100th(double n){
return Math.round(n*100.)/100.0 ;
}
}
Output
Comments
Leave a comment