Math Methods Sample Code      P. Komisar


 

class MathShow{
 public static void main(String[]args){
     String x,y,z;
     int a = -123;
     x=Math.abs(a) +"   // abs()";       //absolute number |-3| is 3
     out(x);
     double d=3.98;
     y=Math.floor(d) + "   // floor()";
     out(y);                  // outputs 3.0
     y=Math.ceil(d) + "   // ceil()";
     out(y);               // outputs.4.0,   floor and ceil return double corr to nearest int
      z=Math.min(a,d) +"   // min()";
     out(z);    // prints -123.0 notice promotes to double
     z=Math.max(a,d) +"   // max()";
     out(z);    // prints 3.98
     out("");   // spacer before the 3 random numbers
  for(int i=0;i<3;i++){
     x =( (int)( Math.random() * 100) + 1 ) +"   // random() " + (i+1);
     out(x);
     }
    out("");   // spacer after the 3 random numbers
     y = Math.round(6.5) + "   // round() ";
     out(y);
     z = Math.sin(1.0) + "   // sin()";
     out(z);
     z = Math.cos(1.0) + "   // cos()";
     out(z);
     z = Math.tan(1.0) + "   // tan()";
     out(z);
     x = Math.log(1) + "   // log()" ;
     out(x);      // log of one is zero
     y = Math.exp(4) + "   // exp()";
     out(y);
     z = Math.rint(5.555) + "   // rint()";
     out(z);
     x = Math.IEEEremainder(16.4,5.2) +"   // IEEEremainder";
     out(x);
     y = Math.toDegrees(6.28) + "   // 6.28 radians toDegrees()";
     out(y);
     y = Math.toRadians(180) + " // 180 degrees toRadians()";
     out(y);
    }
   //abreviated system out method
      static void out(String s){  System.out.println(s);  }
}