7-6 学生列表2 (25 分)

7-6 学生列表2 (25 分)

编写学生类,包含学号no、姓名name、成绩score,提供必要的构造函数、toString函数和equals/hashcode函数,其中,toString函数的格式为“no:xxx name:xxx score:xxx”,no参与equals和hashcode的计算 在main函数中构造一个容器存放学生对象 从命令行输入多个学生对象,存入容器中 从命令行中读入在容器上的操作,具体操作包含:
add 添加一个学生(包含学号和学生姓名)
delete 删除一个学生(包含学号)
set 修改一个学生信息(只修改某学号学生的成绩)
完成操作后按学生的学号从小到大的顺序输出容器中的学生

输入格式:

学生个数 学生对象数据 操作数 操作内容

输出格式:

列表顺序输出集合中的学生

输入样例:

在这里给出一组输入。例如:

4
1 wong 90
2 liu 80
3 chen 70
4 fang 60
3
add 5 duan 80
delete 3
set 4 70

输出样例:

在这里给出相应的输出。例如:

no:1 name:wong score:90
no:2 name:liu score:80
no:4 name:fang score:70
no:5 name:duan score:80

代码

import java.util.*;

class Student implements Comparable {
    public int no;
    public String name;
    public int score;

    public Student(int no, String name, int score) {
        this.no = no;
        this.name = name;
        this.score = score;
    }

    public Student(int no) {
        this.no=no;
    }

    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 getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "no:" + this.no + " name:" + this.name + " score:" + this.score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return no == student.no && score == student.score && Objects.equals(name, student.name);
    }

    public int compareTo(Object o) {
        Student s = (Student) o;
        return this.no - s.no;
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name, score);
    }
}


public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        TreeSet ts = new TreeSet();
        for(int i=0;i<num;i++){
            ts.add(new Student(scanner.nextInt(),scanner.next(),scanner.nextInt()));
        }

        int n = scanner.nextInt();
        for (int i=0;i<n;i++){
            String str =scanner.next();
            switch (str){
                case "add":
                    ts.add(new Student(scanner.nextInt(),scanner.next(),scanner.nextInt()));
                    break;
                case "delete":
                    ts.remove(new Student(scanner.nextInt()));
                    break;
                case "set":
                    int no = scanner.nextInt();
                    Iterator iterator = ts.iterator();
                    while ((iterator.hasNext())){
                        Student s= (Student) iterator.next();
                        if(s.no==no){
                            s.setScore(scanner.nextInt());
                        }
                    }
            }

        }

        for(Object o :ts){
            System.out.println(o);
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、学生类的创建和使用 ○1创建一个Student类,包括的域有学号,姓名,性别和年龄,另外增加一个统计学生人数的静态域count; ○2在Student类建立构造方法初始化各域的值,并统计学生人数count; ○3创建别获得各域(学号,姓名,性别和年龄)的public方法,以及别设置各域(学号,姓名,性别和年龄)值的public方法(即get、set方法);另外还要创建获取静态域count值的get方法; ○4创建public型的toString方法,把该类的各域信息(学号,姓名,性别和年龄)组合成一个字符串,如:“141308008,小明,男,20岁”; ○5创建Student类的子类CollegeStudent类(大学生类),添加域:专业,静态域count统计大学生人数,建立构造方法并于其中统计大学生人数,并相应添加专业的get、set方法,获取静态域count值的get方法,重写toString方法(把专业信息加进去); ○6创建包含主方法的主类,并在主方法测试Student类与CollegeStudent类:创建Student类的两个对象;输出目前的Student类对象的人数;别显示对象的各域信息(学号,姓名,性别和年龄;使用toString方法);修改某个对象的姓名和年龄,然后再显示这个对象的姓名和年龄;比较两个对象的年龄大小,输出年龄较大者的对象的所有域信息(toString方法)。创建CollegeStudent类的一个对象;输出目前的CollegeStudent类对象的人数;修改对象的姓名、年龄和专业;然后再显示这个对象的全部信息(调用toString方法,或在println方法中直接输出使其自动调用toString方法)。 注:以上类中所有域都是private类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值