展开全部
附件是完整的eclipse/MyEclipse工程包,可32313133353236313431303231363533e59b9ee7ad9431333335326133以导入自行测试;
或许比别人的实现要长,主要是基于可扩展性的考虑。
// Entity Class
public class Student implements Comparable{
private int no;
private String name;
private int age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int no, String name, int age) {
super();
this.no = no;
this.name = name;
this.age = age;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
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 "Student [no=" + no +", name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object o) {
Student stu = (Student) o ;
int a = no - stu.getNo();
// TODO Auto-generated method stub
return a;
}
}
// Entity Factory
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class StudentFactory {
static String name[] = {"Leon","Matthew","Porter","Pope","Taylor"};
static int index = 1;
static Random random = new Random();
/**
* Create a new student with name and age randomly in numeric sequence
*/
public static Student create(){
Student student = new Student();
student.setNo((Integer)getRandomNo(1, 60).toArray()[index-1]);
student.setName(name[(Integer)getRandomNo(0, 5).toArray()[index-1]]);
// setting age between [18,22]
student.setAge(new Random().nextInt(18)%(22-18+1) + 18);
index++;
return student;
}
/**
* Create n students
*
*/
public static List create(int n){
List studentList = new ArrayList();
for(int i=0; i
Student student = create();
studentList.add(student);
}
return studentList;
}
/**
* 取得在[min,max]范围内5个不重复的随机数
*/
public static Set getRandomNo(int min,int max){
Random random = new Random();
Set set = new HashSet();
if(max - min
System.out.println("Error : The scope
return null;
}
while (set.size()
int number=random.nextInt(max)%(max-min+1) + min;//
set.add(number);
}
return set;
}
public static void main(String[] args) {
StudentFactory studentFactory = new StudentFactory();
List studentList = studentFactory.create(5);
for(Student student: studentList){
System.out.println(student);
}
}
}
// requirement implementation
import java.util.Arrays;
import java.util.List;
public class Task {
/**
* (1) 将学生按学号按由小到大顺序输出
*/
public void sort(List studentList){
Object[] studentArr = studentList.toArray();
Arrays.sort(studentArr);
for(Object student : studentArr){
System.out.println(student);
}
}
/**
* (2) 给所有学生年龄加n
*/
public void agePlus(List studentList, int n){
for(Student student : studentList){
student.setAge(student.getAge() + n );
System.out.println(student);
}
}
/**
* (3) 统计大于age岁的学生人数
*/
public int total(List studentList,int age){
int totalNo = 0;
for(Student student : studentList){
if(student.getAge() > age){
totalNo ++ ;
}
}
return totalNo;
}
public static void main(String[] args) {
List studentList = StudentFactory.create(5);
Task task = new Task();
System.out.println("-----sort by no-----");
task.sort(studentList);
System.out.println("-----All students' age plus 1------");
task.agePlus(studentList, 1);
System.out.println("-----Total number of age > 20 : " + task.total(studentList, 20));
}
}