Java utility classes (9)

1. Utility class:

9 utility classes:

1.1 when will enumeration be used?

Enumeration is a type;

 

public enum enumeration name {/ / defines the enumeration class

}

 

The enumeration can be defined as a new file or in a class of practical enumeration,

Such as:

package com.example.test;

import com.example.test.Student1.Xingbie;

public class TestStu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student1 stu=new Student1();
		stu.sex=Xingbie.female;
		System.out.println(stu.sex);
	}
}
package com.example.test;

public class Student1 {
	public enum Xingbie{
		//Male,
		//female
	}
	public Xingbie sex;
}

The operation result is:

package com.example.test;

//import com.example.test.Student1.Xingbie;

public class TestStu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student1 stu=new Student1();
		stu.sex=Genders.female;
		System.out.println(stu.sex);
	}

}
package com.example.test;

public enum Genders {
	//Male,
	//female
}
package com.example.test;

public class Student1 {
//	public enum Xingbie{
//		Male,
//		female
//	}
	public Genders sex;
}

The difference between enumeration class definition and a new file of new in a practical class:

The scope of enumeration classes is different. Enumeration classes are defined in practical classes. They can only be used by dependent classes, and other classes can use import. A new file can be used by other classes.

Advantages of enumeration type: type safety; easy to input; clear code;

Requirements, modification, enumeration:

Solution:

package com.example.test;

public class WeekDemo {
	public void doWhat(WeekEnum day) {//Enumeration constrains the range of values.
		switch(day) {
		case MON:
		case TUE:
		case WED:
		case THU:
		case FRI:
			System.out.println("Working day, try to write code!");
			break;
		case SAT:
			System.out.println("Saturday, rest! Watch movie!");
			break;
		case SUN:
			System.out.println("Sunday, rest! Watch movie!");
		}
	}
	public static void main(String[] args) {
		WeekDemo wd=new WeekDemo();
		wd.doWhat(WeekEnum.SUN);
	}
}

Interview questions: write down the exceptions you used

Write the packages and classes you used

 

1.2. java.lang.Math class

Math.random() generates a random number of 0.0 < = value < 1.0

package com.example.orient;

public class LuckyDog {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Membership card number 100 = random number < = = winning rule
		int custNo=9876;
		int baiWei= custNo/100%10;
		int suiJi=(int)(Math.random()*10);
		System.out.println(suiJi);
		if(baiWei==suiJi) {
			System.out.println("I'm a lucky dog!");
		}
		else {
			System.out.println("No prize");
		}
	}

}

1.3. java.util.Random is a class that generates random numbers through a seed and an algorithm.

Two steps: random = new random();

random.nextInt(maxNum); / / cannot exceed maxNum

package com.example.orient;

import java.util.Random;

public class RandomDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random  random = new Random();
		for(int i=1;i<=10;i++) {
			int a=random.nextInt(10);
			System.out.println("The first"+i+"Random number:"+a);
		}
	}

}

Code validation is as follows:

package com.example.orient;

import java.util.Random;

public class RandomDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*Random  random = new Random();
		for(int i=1;i<=10;i++) {
			int a=random.nextInt(10);
			System.out.println("The "+ i +" random number: "+ a";
		}*/
		//Two seed generators generate random numbers
		Random random1=new Random(9);
		Random random2=new Random(9);
		//Randomly generate an integer
		System.out.println(random1.nextInt());
		System.out.println(random2.nextInt());
	}

}

Operation result:

 

Tags: Java REST

Posted on Thu, 13 Feb 2020 09:53:58 -0800 by bassdog65