Java程序设计-实验6-sdust

6-3 成绩管理系统 (20 分)

构造一个成绩管理系统CourseManagementSystem,该系统包括如下几个方法:void add(int no, int grade)添加该学号的成绩,如果系统已有该学生成绩,则输出"the student already exists";void delete(int no)删除某学号成绩,如果不存在此学生则输出"no such student";int query(int no)查询并返回该学号的成绩;统计成绩void statistics( )统计[0-59]、[60-69]、[70-79]、[80-89]、[90-100]各成绩段的学生个数并打印。请选择合适的容器实现上述功能。(题目假设不会重复添加相同学号的学生成绩) main函数中读入操作类型及相关参数,并调用statictic函数输出学生成绩统计信息。
输入描述:

操作个数 操作名 操作参数
输出描述:

查询学生的成绩 各成绩段的学生个数
裁判测试程序样例:

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        CourseManagementSystem cms = new CourseManagementSystem();
        int ops = sc.nextInt();
        for (int i=0;i<ops;i++) {
            String op = sc.next();
            if (op.equals("add")) 
                cms.add(sc.nextInt(), sc.nextInt());
            else if  (op.equals("delete"))
                cms.delete(sc.nextInt());
            else if  (op.equals("query")) {
                int no = sc.nextInt();
                int s = cms.query(no);
                System.out.println("the score for "+no+" is : "+s);
            }
        }
        cms.statistic();
    }
}

/* 你的代码被嵌在这里*/

输入样例:

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

8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90

输出样例:

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

the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1

Accepted Code

class CourseManagementSystem {
    HashMap<Integer, Integer> mp = new HashMap<>();

    void add(int no, int grade) {
        if (mp.containsKey(no)) {
            System.out.println("the student already exists");
        } else {
            mp.put(no, grade);
        }
    }

    void delete(int no) {
        if (!mp.containsKey(no)) {
            System.out.println("no such student");
        } else {
            mp.remove(no);
        }
    }

    int query(int no) {
        return mp.get(no);
    }

    void statistic() {
        Set<Integer> s = mp.keySet();
        int e = 0, d = 0, c = 0, b = 0, a = 0;
        for (Integer i : s) {
            if (mp.get(i) < 60 && mp.get(i) >= 0) {
                e++;
            } else if (mp.get(i) < 70 && mp.get(i) >= 60) {
                d++;
            } else if (mp.get(i) < 80 && mp.get(i) >= 70) {
                c++;
            } else if (mp.get(i) < 90 && mp.get(i) >= 80) {
                b++;
            } else if (mp.get(i) <= 100 && mp.get(i) >= 90) {
                a++;
            }
        }
        System.out.println("[0-59] : " + e);
        System.out.println("[60-69] : " + d);
        System.out.println("[70-79] : " + c);
        System.out.println("[80-89] : " + b);
        System.out.println("[90-100] : " + a);
    }
    
}

7-3 学生列表2 (20 分)

编写学生类,包含学号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

Accepted Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main{
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        ArrayList<Student> arrayList = new ArrayList<Student>();
        for (int i  = 0; i < n; i++) {
            arrayList.add(new Student(scan.nextInt(), scan.next(), scan.nextInt()));
        }
        int m = scan.nextInt();
        for (int j = 0; j < m; j++) {
            String s = scan.next();
            if (s.equals("add")) {
                arrayList.add(new Student(scan.nextInt(), scan.next(), scan.nextInt()));
            } else if (s.equals("delete")) {
                int tmp = scan.nextInt();
                arrayList.removeIf(student -> student.no == tmp);
            } else if (s.equals("set")){
                int tmp = scan.nextInt();
                for (Student student : arrayList)
                    if (student.no == tmp)
                        student.score = scan.nextInt();
            }
        }

        arrayList.sort(new myCom());

        for (Student student : arrayList) {
            System.out.println(student);
        }


        scan.close();
    }
}

class Student implements Comparable<Student> {
    int no;
    String name;
    int score;


    Student(int no1, String name1, int score1) {
        no = no1;
        name = name1;
        score = score1;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Student) {
            return ((Student) obj).no == no;
        }
        return false;
    }

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

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }

}

class myCom implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.no - o2.no;
    }
}

7-2 office文档页码打印 (20 分)

在office软件(word,excel)中,有时只需要打印整个文档中的一部分,就需要用户选择需要打印的页码范围。目前输入的页码范围格式定义为:以逗号分割,可以使用-表示连续页码。例如:1,3,5-9,20。表示需要打印的页码为1,3,5,6,7,8,9,20。

本题目要求读入一行字符串,作为需要打印的页码范围。需要注意以下几点:

1、页码范围输入可以不按顺序。例如:5,3,7,9-10,1-2;
2、连续的页码定义也可能不会按照由小到大的顺序输入。例如:1,9,5,20-15,10;
3、输入的页码范围可能会有重复。例如:1,9,15,5-10,12-20;

输入格式:

第一行:表示页码范围的格式化字符串
输出格式:

将需要打印的页码按照由小到大的顺序输出,以空格分割
输入样例:

1,3,5-9,20

输出样例:

1 3 5 6 7 8 9 20

输入样例:

12-20,1,15,9,5-10

输出样例:

1 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20

Accepted Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main{
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);

        TreeSet<Integer> treeSet = new TreeSet<>();
        String str = scan.next();
        String [] s = str.split(",");
        for (String s1 : s) {
            if (s1.contains("-")) {
                String []tmp = s1.split("-");
                int x = Integer.parseInt(tmp[0]);
                int y = Integer.parseInt(tmp[1]);
                if (x > y) {
                    for (int i = y; i <= x; i++) {
                        treeSet.add(i);
                    }
                } else {
                    for (int i = x; i <= y; i++) {
                        treeSet.add(i);
                    }
                }
            } else {
                treeSet.add(Integer.parseInt(s1));
            }
        }

        ArrayList arr = new ArrayList(treeSet);

        for (int i  = 0; i < arr.size(); i++) {
            System.out.print(arr.get(i));
            if (i != arr.size() - 1) {
                System.out.print(" ");
            }
        }

        scan.close();
    }
}

7-4 sdust-Java-字符串集合求并集 (20 分)

从键盘接收N个英文字符串(其中不同的字符串数量大于10),从头开始取5个不同的字符串放入一个集合S1,然后接着取5个不同的字符串放入另一个集合S2,按照字母顺序输出S1和S2的并集中的每个字符串(字符串区分大小写)
输入格式:

一行以空格分开的英文字符串(不同的字符串数量大于10)。
输出格式:

按照字母顺序(先比较字符串首字母,首字母相同的比较字符串第二个字母,以此类推)输出的S1和S2并集的字符串。
输入样例:

android python java javaee javase database java jsp servlet java algorithm junit

输出样例:

algorithm
android
database
java
javaee
javase
jsp
python
servlet

Accepted Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main{
    public static void main(String[] args) throws ParseException {
        Scanner scan = new Scanner(System.in);

        Set<String> set1 = new TreeSet<>();
        Set<String> set2 = new TreeSet<>();
        String str = scan.nextLine();
        String [] s = str.split(" ");

        for (String s1 : s) {
            if (set1.size() < 5) {
                set1.add(s1);
            } else if (set2.size() < 5) {
                set2.add(s1);
            } else {
                break;
            }
        }

        Set<String> set3 = union(set1, set2);

        for (String str3 : set3) {
            System.out.println(str3);
        }

        scan.close();
    }

    //求并集
    public static Set<String> union(Set<String> arr1, Set<String> arr2) {
        Set<String> set = new TreeSet<String>();
        for (String str : arr1) {
            set.add(str);
        }
        for (String str : arr2) {
            set.add(str);
        }
        String[] result = {};
        return set;
    }
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值