《Java程序设计》 创建5个学生对象,给一个学生数组赋值,每个学生属性有:学号,姓名,年龄。
《Java程序设计》创建5个学生对象,给一个学生数组赋值,每个学生属性有:学号,姓名,年龄。(1)将学生按学号排序输出;(2)给所有学生年龄加1;(3)统计大于20岁学生...
《Java程序设计》
创建5个学生对象,给一个学生数组赋值,每个学生属性有:学号,姓名,年龄。
(1)将学生按学号排序输出;
(2)给所有学生年龄加1;
(3)统计大于20岁学生人数。 展开
创建5个学生对象,给一个学生数组赋值,每个学生属性有:学号,姓名,年龄。
(1)将学生按学号排序输出;
(2)给所有学生年龄加1;
(3)统计大于20岁学生人数。 展开
展开全部
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Student {
public int getStudentID() {
return 银顷studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int studentID;
private int age;
private String name;
private Student(int studentID, int age, String name) {
super();
this.studentID = studentID;
this.age = age;
this.name = name;
}
public static void main(String[] args) {
Student[] StuArr =
new Student[] { new Student(550126, 15, "YYYYY"), new Student(550120, 21, "zhangsan"),
new Student(550121, 24, "lisi"), new Student(550122, 21, "wangwu"), new Student(550113, 18, "XXXX") };
List<Student> ListStu = Arrays.asList(StuArr);
Collections.sort(ListStu, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if (o1.getStudentID() > o2.getStudentID())
return 1;
else if (o1.getStudentID() < o2.getStudentID())
樱谨 return -1;
else
return 0;
}
});
System.out.println("******* 按 学号顺序 打印出来*********");
for (Student $student : ListStu)
System.out.println($student);
System.out.println("******* 所有学生年龄加1*********");
for (int i = 0; i < StuArr.length; i++) {
StuArr[i].setAge(StuArr[i].getAge() + 1);
}
for (int i = 0; i < StuArr.length; i++) {
System.out.println(StuArr[i]);
}
System.out.println("******* 统计所有大于20的人*********");
int COUNT = 0;
锋颂陆for (int i = 0; i < StuArr.length; i++) {
if (StuArr[i].getAge() > 20)
COUNT++;
}
System.out.println("Count:" + COUNT);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return new StringBuffer().append("Student ID:").append(this.studentID).append(" ,Aget:").append(this.age).append(
",Name=").append(this.name).toString();
}
}
展开全部
附件是桥差完整的eclipse/MyEclipse工程包,可以导哗销入自行测试;
或许比别人的实现要长,主要是基于可扩展性的考虑。
// 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<Student> create(int n){
List<Student> studentList = new ArrayList<Student>();
for(int i=0; i < n; i++){
Student student = create();
studentList.add(student);
}
return studentList;
}
/**
* 取得在[min,max]范围内5个不重复的随机数
*/
public static Set<Integer> getRandomNo(int min,int 乱消游max){
Random random = new Random();
Set<Integer> set = new HashSet<Integer>();
if(max - min < 5){
System.out.println("Error : The scope < 5");
return null;
}
while (set.size() < 5) {//the Elements of set is unique
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<Student> 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<Student> studentList){
Object[] studentArr = studentList.toArray();
Arrays.sort(studentArr);
for(Object student : studentArr){
System.out.println(student);
}
}
/**
* (2) 给所有学生年龄加n
*/
public void agePlus(List<Student> studentList, int n){
for(Student student : studentList){
student.setAge(student.getAge() + n );
System.out.println(student);
}
}
/**
* (3) 统计大于age岁的学生人数
*/
public int total(List<Student> studentList,int age){
int totalNo = 0;
for(Student student : studentList){
if(student.getAge() > age){
totalNo ++ ;
}
}
return totalNo;
}
public static void main(String[] args) {
List<Student> 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));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询