Java週間テスト問題

3439 ワード

1、文字列「a-b-c-d-e-f」を「-」でカットし、「c」文字を見つけて大文字に置き換え、「f-e-d-C-b-a」を逆順に出力します.
public class Test1 {
	public static void main(String[] args) {
		String str = "a-b-c-d-e-f";

		String[] strs = str.split("-");

		//     ,       
		for (int i = 0; i < strs.length; i++) {
			String s = strs[i];
			if (s.equals("c")) {
				strs[i] = s.toUpperCase();
			}
		}
		String temp = "";
		for (int i = strs.length - 1; i >= 0; i--) {
			temp += strs[i] + "-";
		}
		//         
		System.out.println(temp.substring(0, temp.length()-1));
	}
}

2.学生クラス(学番、名前、年齢を含む)を定義し、グループのメンバーを1つのセットに追加し、学番でソートして出力します.
public class Student implements Comparable {
	private int code;
	private String name;
	private int age;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	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;
	}

	public Student(int code, String name, int age) {
		super();
		this.code = code;
		this.name = name;
		this.age = age;
	}

	@Override
	public int compareTo(Student o) {
		int flag;
		if (this.code > o.code) {
			flag = -1;
		} else if (this.code == o.code) {
			flag = 0;
		} else {
			flag = 1;
		}
		return flag;
	}

	@Override
	public String toString() {
		return "Student [code=" + code + ", name=" + name + ", age=" + age
				+ "]";
	}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test2 {
	public static void main(String[] args) {
		List teams = new ArrayList();
		//       
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		//          
		teams.add(stu1);
		teams.add(stu2);
		teams.add(stu3);
		teams.add(stu4);
		//        
		Collections.sort(teams);
		//     
		for (Student stu : teams) {
			System.out.println(stu);
		}
	}
}

3、第2題に続いて、一例でサービスクラスを設計し、集合中の学生オブジェクトをランダムに抽出し、出力を印刷する方法を定義する.
import java.util.ArrayList;
import java.util.List;

public class Test3 {
	public static void main(String[] args) {
		List teams = new ArrayList();
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		teams.add(stu1);
		teams.add(stu2);
		teams.add(stu3);
		teams.add(stu4);

		//           
		Service service = Service.getInstance();

		System.out.println("==========      =============");
		Student randomStu = service.randomStu(teams);
		System.out.println(randomStu);
	}
}
import java.util.List;

public class Service {
	private static Service service = new Service();
	
	private Service() {
		
	}
	
	public static Service getInstance() {
		return service;
	}
	
	public Student randomStu(List stus) {
		if (null != stus && !stus.isEmpty()) {
			return stus.get((int)(Math.random() * stus.size()));
		} else {
			return null;
		}
	}
}