Examples of usage of Java Math Random System and BigDecimal classes

  • 2021-07-10 19:36:50
  • OfStack

This article illustrates the usage of Java, Math, Random, System and BigDecimal. Share it for your reference, as follows:

Class Math

Method of Math


package cn.itcast_01;
/*
 * Math: A class used for mathematical operations. 
 *  Member variables: 
 * public static final double PI
 * public static final double E
 *  Member method: 
 * public static int abs(int a) Absolute value 
 * public static double ceil(double a): Rounding up 
 * public static double floor(double a): Rounding down 
 * public static int max(int a,int b): Maximum value  (min Self-study )
 * public static double pow(double a,double b):a Adj. b Sub-power 
 * public static double random(): Random number  [0.0,1.0)
 * public static int round(float a) 4 Shed 5 Into ( Parameter is double Self-study )
 * public static double sqrt(double a): Square root 
 */
public class MathDemo {
 public static void main(String[] args) {
 // public static final double PI
 System.out.println("PI:" + Math.PI);
 // public static final double E
 System.out.println("E:" + Math.E);
 System.out.println("--------------");
 // public static int abs(int a) Absolute value 
 System.out.println("abs:" + Math.abs(10));
 System.out.println("abs:" + Math.abs(-10));
 System.out.println("--------------");
 // public static double ceil(double a): Rounding up 
 System.out.println("ceil:" + Math.ceil(12.34));
 System.out.println("ceil:" + Math.ceil(12.56));
 System.out.println("--------------");
 // public static double floor(double a): Rounding down 
 System.out.println("floor:" + Math.floor(12.34));
 System.out.println("floor:" + Math.floor(12.56));
 System.out.println("--------------");
 // public static int max(int a,int b): Maximum value 
 System.out.println("max:" + Math.max(12, 23));
 //  Requirements: I want to get 3 Maximum value in data 
 //  Nested calls to methods 
 System.out.println("max:" + Math.max(Math.max(12, 23), 18));
 //  Requirements: I want to get 4 Maximum value in data 
 System.out.println("max:"
 + Math.max(Math.max(12, 78), Math.max(34, 56)));
 System.out.println("--------------");
 // public static double pow(double a,double b):a Adj. b Sub-power 
 System.out.println("pow:" + Math.pow(2, 3));
 System.out.println("--------------");
 // public static double random(): Random number  [0.0,1.0)
 System.out.println("random:" + Math.random());
 //  Get 1 A 1-100 Random number between 
 System.out.println("random:" + ((int) (Math.random() * 100) + 1));
 System.out.println("--------------");
 // public static int round(float a) 4 Shed 5 Into ( Parameter is double Self-study )
 System.out.println("round:" + Math.round(12.34f));
 System.out.println("round:" + Math.round(12.56f));
 System.out.println("--------------");
 //public static double sqrt(double a): Square root 
 System.out.println("sqrt:"+Math.sqrt(4));
 }
}

Run results:

PI:3.141592653589793
E:2.718281828459045
--------------
abs:10
abs:10
--------------
ceil:13.0
ceil:13.0
--------------
floor:12.0
floor:12.0
--------------
max:23
max:23
max:78
--------------
pow:8.0
--------------
random:0.39060160152994794
random:75
--------------
round:12
round:13
--------------
sqrt:2.0

Math.random()


package cn.itcast_02;
import java.util.Scanner;
/*
 *  Requirements: Please design 1 Methods can be achieved to obtain random numbers in any range. 
 *
 *  Analysis: 
 * A: Enter two data on the keyboard. 
 * int strat;
 * int end;
 * B: Find a way to get it in start To end Random number between 
 *  I write 1 Functions to achieve this effect, get 1 Random numbers. (int)
 * C: Output this random number 
 */
public class MathDemo {
 @SuppressWarnings("resource")
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.println(" Please enter the starting number: ");
 int start = sc.nextInt();
 System.out.println(" Please enter the end number: ");
 int end = sc.nextInt();
 for (int x = 0; x < 100; x++) {
 //  Invoke function 
 int num = getRandom(start, end);
 //  Output result 
 System.out.println(num);
 }
 }
 /*
 *  Write 1 Functions   Two definitions:   Return value type: int  Parameter list: int start,int end
 */
 public static int getRandom(int start, int end) {
 int number = (int) (Math.random() * (end - start + 1)) + start;
 return number;
 }
}

Run results:

Please enter the starting number:
100
Please enter the end number:
1000
394
478
224
432
917
443
715
830
123
735
510
581
134
508
318
156
365
223
553
954
401
514
732
766
812
358
118
907
113
923
182
123
111
728
217
235
444
963
754
426
889
885
650
475
673
783
906
324
414
792
695
468
406
524
346
701
220
350
505
866
186
925
986
147
608
487
957
964
369
373
468
982
291
372
867
280
110
680
268
110
895
897
586
445
387
728
114
427
974
452
497
444
765
603
243
381
436
757
316
137

Class Random


package cn.itcast_01;
import java.util.Random;
/*
 * Random: Classes that produce random numbers 
 *
 *  Construction method: 
 * public Random(): No seed, the default seed is used, which is the millisecond value of the current time 
 * public Random(long seed): Give the specified seed 
 *
 *  Given the seed, the random number is the same every time. 
 *
 *  Member method: 
 * public int nextInt() Returns the int Random numbers in the range 
 * public int nextInt(int n): Returns the [0,n) Random numbers within the range 
 */
public class RandomDemo {
 public static void main(String[] args) {
 //  Create an object 
 // Random r = new Random();
 Random r = new Random(1111);
 for (int x = 0; x < 10; x++) {
 // int num = r.nextInt();
 int num = r.nextInt(100) + 1;
 System.out.println(num);
 }
 }
}

Class System

System class, which provides some useful fields and methods

Running the garbage collector


package cn.itcast_01;
public class Person {
 private String name;
 private int age;
 public Person() {
 super();
 }
 public Person(String name, int age) {
 super();
 this.name = name;
 this.age = age;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 @Override
 public String toString() {
 return "Person [name=" + name + ", age=" + age + "]";
 }
 @Override
 protected void finalize() throws Throwable {
 System.out.println(" The current object has been reclaimed " + this);
 super.finalize();
 }
}


package cn.itcast_01;
/*
 * System Class contains 1 Some useful class fields and methods. It cannot be instantiated. 
 *
 *  Methods: 
 * public static void gc() Run the garbage collector. 
 * public static void exit(int status)
 * public static long currentTimeMillis()
 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 */
public class SystemDemo {
 public static void main(String[] args) {
 Person p = new Person(" Angie Chiu ", 60);
 System.out.println(p);
 p = null; //  Jean p No longer specify heap memory 
 System.gc();
 }
}

Exit jvm and get the millisecond value of the current time


package cn.itcast_02;
/*
 * System Class contains 1 Some useful class fields and methods. It cannot be instantiated. 
 *
 *  Methods: 
 * public static void gc() Run the garbage collector. 
 * public static void exit(int status): Terminate the currently running  Java  Virtual machine. Parameters are used as status codes; By convention, non-  0  The status code of indicates abnormal termination. 
 * public static long currentTimeMillis(): Returns the current time in milliseconds 
 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 */
public class SystemDemo {
 public static void main(String[] args) {
 // System.out.println(" We like Brigitte Lin ( Dong Fangbubai )");
 // System.exit(0);
 // System.out.println(" We also like Angie Chiu ( White Lady )");
 // System.out.println(System.currentTimeMillis());
 //  It doesn't make much sense for us to get this reality alone at present 
 //  So, what exactly does it do ?
 //  Requirements: Please give me statistics of the running time of this program 
 long start = System.currentTimeMillis();
 for (int x = 0; x < 100000; x++) {
 System.out.println("hello" + x);
 }
 long end = System.currentTimeMillis();
 System.out.println(" Total time spent: " + (end - start) + " Milliseconds ");
 }
}

Array copy


package cn.itcast_03;
import java.util.Arrays;
/*
 * System Class contains 1 Some useful class fields and methods. It cannot be instantiated. 
 *
 *  Methods: 
 * public static void gc() Run the garbage collector. 
 * public static void exit(int status): Terminate the currently running  Java  Virtual machine. Parameters are used as status codes; By convention, non-  0  The status code of indicates abnormal termination. 
 * public static long currentTimeMillis(): Returns the current time in milliseconds 
 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
 *  Copies from the specified source array 1 The copy starts at the specified position and ends at the specified position of the target array. 
 */
public class SystemDemo {
 public static void main(String[] args) {
 //  Defining Array 
 int[] arr = { 11, 22, 33, 44, 55 };
 int[] arr2 = { 6, 7, 8, 9, 10 };
 //  Please look at the meaning of this code 
 System.arraycopy(arr, 2, arr2, 1, 2);
 System.out.println(Arrays.toString(arr));
 System.out.println(Arrays.toString(arr2));
 }
}

Run results:

[11, 22, 33, 44, 55]
[6, 33, 44, 9, 10]

More readers interested in java related content can check the topics of this site: "Java Data Structure and Algorithm Tutorial", "Java Operation DOM Node Skills Summary", "Java File and Directory Operation Skills Summary" and "Java Cache Operation Skills Summary"

I hope this article is helpful to everyone's java programming.


Related articles: