44-Class Library Use Case Analysis

Class library use case analysis

StringBuffer use

  Define a StringBuffer class object, and then add 26 lowercase letters to the object through the append() method. It is required to add it only once at a time, a total of 26 times, and then output in reverse order, and the first five characters can be deleted;
this operation It is mainly to train the processing methods in the StringBuffer class, because the main feature of StringBuffer is to allow modification.

public class String_Buffer_Use {
    
    

	public static void main(String[] args) {
    
    
		StringBuffer buf = new StringBuffer();
		for(int x='a'; x<='z';x++) {
    
    
			buf.append((char)x);
		}
		buf.reverse().delete(0, 5);		//反转删除
		System.out.println(buf);
	}
}

  Because StringBuffer is allowed to be modified, and String content is not allowed to be modified, the current program is a single-threaded development, and there is no need to consider concurrent access issues.

Random array

  Use the Random class to generate 5 random integers between 1 and 30.

import java.util.Arrays;
import java.util.Random;

class NumberFactory{
    
    
	private static Random random =new Random();
	/**
	 * 通过随机数生成一个数组的内容,该内容不包括0
	 * @param len 要开辟数组大小
	 * @return 包含随机数字的数组
	 */
	public static int [] create(int len) {
    
    
		int data [] = new int [len];
		int foot = 0 ;
		while(foot< data.length) {
    
    
			int num = random.nextInt(30);
			if(num != 0) {
    
    
				data[foot++] = num;
			}
		}
		return data;
	}
}

public class Random_shuzu_use {
    
    

	public static void main(String[] args) {
    
    
		int result [] = NumberFactory.create(5);
		System.out.println(Arrays.toString(result));
	}
}

Email verification

  Enter an email address, and then use regular expressions to verify that the email address is correct.

package support_package_use;

class Validator{
    
    		//验证程序类
	private Validator() {
    
    }
	public static boolean isEmail(String email) {
    
    
		if(email == null|| "".equals(email)) {
    
    
			return false;
		}
		String regex = "\\w+@\\w+\\.\\w+";
		return email.matches(regex);
		
	}
}

public class Email_valid_use {
    
    

	public static void main(String[] args) {
    
    
		if(args.length != 1) {
    
    
			System.exit(1);		//系统退出
		}
		String email = args[0];
		if(Validator.isEmail(email)) {
    
    
			System.out.println("有效");
		}else {
    
    
			System.out.println("无效");
		}
	}
}

Toss a coin

  Use 0~1 random number to simulate coin tossing experiment, count the number of positive and negative after 1000 times;

import java.util.Random;

class Coin{
    
    
	private int front;	//正面
	private int back;		//背面
	private Random random = new Random();
	/**
	 * 扔硬币处理
	 * @param num 执行次数
	 */
	public void throwCoin(int num) {
    
    
		for(int x=0;x<num;x++) {
    
    
			int temp = random.nextInt(2);
			if(temp ==0) {
    
    
				this.front ++;
			}else {
    
    
				this.back++;
			}
		}
	}
	public int getFront() {
    
    
		return front;
	}
	public int getBack() {
    
    
		return back;
	}
}

public class Random_Use {
    
    

	public static void main(String[] args) {
    
    
		Coin coin = new Coin();
		coin.throwCoin(1000);
		System.out.println("front:"+ coin.getFront());
		System.out.println("back:"+ coin.getBack());
	}
}

IP verification

  Write a regular expression to determine whether a given IP address is a valid IP address.
IP composition:

  • The content of the first digit is none, 1, 2.
  • The following content can be 0-9;
  • The content of the third place is 0-9.
class Validators{
    
    
	public static  boolean validateIP(String ip) {
    
    
		if(ip == null || "".equals(ip)) {
    
    
			return false;
		}
		String regex = "([12]?[0-9]?[0-9]\\.){3}[12]?[0-9]?[0-9]";
		if(ip.matches(regex)) {
    
    
			String result[] = ip.split("\\.");
			for(int x=0; x<result.length;x++) {
    
    
				int temp = Integer.parseInt(result[x]);
				if(temp > 255) {
    
    
					return false;
				}
			}
		} else {
    
    
			return false;
		}
		return true;
	}
}

public class Ip_Valid_Use {
    
    

	public static void main(String[] args) {
    
    
		String str = "192.168.1.1";
		System.out.println(Validators.validateIP(str));
	}
}

HTML split

  Given the following HTML code, split:

<font face="Arial,Serif" size="+2" color=red>

  Split results face Arial,Serif, size +2,color red

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class HTML_split_use {
    
    

	public static void main(String[] args) {
    
    
		String str = "<font face=\"Arial,Serif\" size=\"+2\" color=\"red\">";
		String regex = "\\w+=\"[a-zA-Z0-9,\\+]+\"" ;
		Matcher matcher = Pattern.compile(regex).matcher(str);
		while(matcher.find()) {
    
    
			String temp = matcher.group(0);
			String result[] = temp.split("=");
			System.out.println(result[0]+"\t"+ result[1].replaceAll("\"",""));
		}
	}
}

country code

  Write a program to achieve international applications, enter the country code from the command line, for example, 1 for China, 2 for the United States, and then call Ran's resource file display according to the code.
Implementation Specify the area through the object of the Locale class, and then use the ResourceBundle class to load the resource file.

import java.util.Locale;
import java.util.ResourceBundle;

class Message{
    
    
	public static final int CHINA=1;
	public static final int USA=2;
	private static final String key = "info";
	private static final String BASENAME ="Messages";
	public String getMessage(int num) {
    
    
		Locale loc = this.getLocal(num);
		if(loc == null) {
    
    
			return "Nothing";
		}else {
    
    
			return ResourceBundle.getBundle(BASENAME, loc).getString(key);
		}
	}
	private Locale getLocal(int num) {
    
    
		switch(num) {
    
    
		case CHINA:
			return new Locale("zh","CN");
		case USA:
			return new Locale("en","US");
		default:
			return null;
		}
	}
}

public class Country_Use {
    
    

	public static void main(String[] args) {
    
    
		if(args.length != 1) {
    
    
			System.exit(1);
		}
		int choose = Integer.parseInt(args[0]);
		System.out.println(new Message().getMessage(choose));
	}

}

Student information comparison

  According to the format of "Name: Age: Score|Name: Age: Score", you will deliberately wait for the string "Zhang San: 21:98|Li Si: 22:89|Wang Five: 20: 70", and require that each group of values ​​be separated. Save it in the Student object and sort: Grade>Age;

import java.util.Arrays;

class Student implements Comparable<Student>{
    
    
	private String name;
	private int age;
	private double score;
	public Student(String name, int age, double score) {
    
    
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}
	@Override
	public String toString() {
    
    
		return "Student [name=" + name + ", age=" + age + ", score=" + score + "]\n";
	}
	@Override
	public int compareTo(Student o) {
    
    
		if(this.score< o.score) {
    
    
			return 1;
		}else if(this.score > o.score){
    
    
			return -1;
		} else {
    
    
			return this.age - o.age;
		}
	}	
}

public class Student_Use {
    
    

	public static void main(String[] args) {
    
    
		String input = "lyz:21:98|zky:22:89|zpb:20:70";
		String result[] = input.split("\\|");
		Student students[] = new Student[result.length];
		for (int x=0; x<result.length;x++) {
    
    
			String temp[] = result[x].split(":");
			students[x] = new Student(temp[0], Integer.parseInt(temp[1]), Double.parseDouble(temp[2]));
		}
		Arrays.sort(students);
		System.out.println(Arrays.toString(students));
	}
}

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/113029274