Final class, all static methods
package com.gaowei_13; /* * Math class * final class * Member variables: * public static final double PI * public static final double E * Member methods: * public static int abs(int a): take the absolute value * public static double ceil(double a): 向上取整 * public static double floor(double a): round down * public static int max(int a,int b): maximum value * public static int min(int a,int b): minimum value * public static double pow(double a,double a):a的b次幂 * public static double random(): random number [0.0, 1.0] * public static int round(float a): rounding//+0.5 first and then rounding down * public static int sqrt(double a): returns the positive square root of double * */ public class MathDemo1 { public static void main(String[] args) { System.out.println("PI:"+Math.PI); System.out.println("E:"+Math.E); //public static int abs(int a): take the absolute value System.out.println(Math.abs(10)); System.out.println(Math.abs(-10)); //public static double ceil(double a) System.out.println(Math.ceil(12.43)); //13.0 System.out.println(Math.ceil(12.56)); //13.0 //public static double floor(double a) System.out.println(Math.floor(12.43)); //12.0 System.out.println(Math.floor(12.56)); //12.0 //public static int max(int a,int b) System.out.println(Math.max(12,10)); //12 System.out.println(Math.max(12,56)); //56 //The maximum value of the three data // nested invocation of the method System.out.println(Math.max(Math.max(5, 90),56)); //The maximum value of the four data System.out.println(Math.max(Math.max(5, 90),Math.max(23, 67))); //public static double pow(double a,double a):a的b次幂 System.out.println(Math.pow(2, 3)); //8.0 //public static double random(): random number [0.0, 1.0] System.out.println(Math.random()); System.out.println("random1-100:"+ ((int)(Math.random()*100)+1)); //public static int round(float a): rounding System.out.println(Math.round(12.4f)); System.out.println(Math.round(12.6f)); //public static double sqrt(double a): returns the positive square root of double System.out.println(Math.sqrt(4)); } }
===================================
Interview questions:
package com.gaowei_13;
/*
* need:
* Design a method to obtain random numbers in any range
*
*
*/
public class MathDemo2 {
public static void main(String[] args) {
int start = 12;
int end = 30;
for(int x=0;x<100;x++) {
int randNum = getRandom(12,30);
System.out.println(randNum);
}
}
private static int getRandom(int start, int end) {
int a = (int)(Math.random()*(end-start+1))+start;
// 1--100
//int a = (int)(Math.random()*(100-1+1))+1;
return a;
}
}