美文网首页
Java OJ作业6

Java OJ作业6

作者: V0W | 来源:发表于2018-10-25 16:57 被阅读0次

    167 - 学生列表

    Description

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

    Input

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

    Output

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

    Sample Input

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

    Sample Output

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

    MyAnswer

    import java.util.*;
    
    /*
     * title: 学生列表
     * author: V0W
     * */
    
    public class Main{
        public static void main(String[] args){
            ArrayList<Student> ALs = new ArrayList<>();
            Scanner scan = new Scanner(System.in);
            int stunum = scan.nextInt();
            int i,j;
            for(i=0; i<stunum; i++){
                int no = scan.nextInt();
                String name = scan.next();
                int score = scan.nextInt();
                Student stu = new Student(no,name,score);
                ALs.add(stu);
            }
            int donum = scan.nextInt();
            for(i=0; i<donum; i++){
                String dothing = scan.next();
                if(dothing.equals("add")){
                    int no = scan.nextInt();
                    String name = scan.next();
                    int score = scan.nextInt();
                    Student stu = new Student(no,name,score);
                    ALs.add(stu);
                }else if(dothing.equals("delete")){
                    int dno = scan.nextInt();
                    for(j=0;j<ALs.size(); j++)
                        if (ALs.get(j).getNo()==dno)
                            ALs.remove(j);
                }else if(dothing.equals("set")){
                    int setno = scan.nextInt();
                    int setScore = scan.nextInt();
                    for(Student stu:ALs)
                        if(stu.no == setno)
                            stu.setScore(setScore);
                }
    
            }
            for(Student s:ALs)
                System.out.println(s.toString());
        }
    }
    
    
    class Student{
        int no;
        String name;
        int score;   //根据题目描述,应该是int,实际情况下double更合适
        Student(int no,String name,int score){
            this.no = no;
            this.name = name;
            this.score = score;
        }
    
        public int getNo() {
            return no;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "no:"+no+" name:"+name+" score:"+score;
        }
    
        @Override
        public boolean equals(Object obj) {
            if(obj == null)
                return false;
            else{
                boolean res = false;
                if(obj instanceof Student){
                    Student s = (Student)obj;
                    if(this.no == s.no)
                        res = true;
                }
                return res;
            }
        }
    
        @Override
        public int hashCode() {
            return no*520+1314; /*这里的hashcode使用下述方法更好*/
            /*
            * int res = 17;
            * res = res*31+no;
            * return res;
            * */
        }
    }
    

    168 - 学生Map

    Description

    修改《学生列表》题目,使用学生Map来存放学生的集合,其中key为学号,value为学生对象
    输出时按照学生的学号顺序输出
    

    Input

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

    Output

    按照学号顺序输出集合中的学生
    

    Sample Input

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

    Sample Output

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

    MyAnswer

    import java.util.*;
    
    /*
     * title: 学生Map
     * author V0W
     * reference:   https://blog.csdn.net/nyist327/article/details/65447506
     *              https://www.cnblogs.com/Berryxiong/p/6144086.html
     * */
    
    public class Main{
        public static void main(String[] args){
            Map<Integer,Student> hmap = new HashMap<Integer,Student>();
            Scanner scan = new Scanner(System.in);
            int stunum = scan.nextInt();
            int i,j;
            for(i=0; i<stunum; i++){
                Integer no = scan.nextInt();
                String name = scan.next();
                int score = scan.nextInt();
                Student stu = new Student(no,name,score);
                hmap.put(no, stu);
            }
            int donum = scan.nextInt();
            for(i=0; i<donum; i++){
                String dothing = scan.next();
                if(dothing.equals("add")){
                    Integer no = scan.nextInt();
                    String name = scan.next();
                    int score = scan.nextInt();
                    Student stu = new Student(no,name,score);
                    hmap.put(no,stu);
                }else if(dothing.equals("delete")){
                    Integer dno = scan.nextInt();
                    hmap.remove(dno);
    
                }else if(dothing.equals("set")){
                    int setno = scan.nextInt();
                    int setScore = scan.nextInt();
                    for(Student stu: hmap.values()){
                        if(stu.getNo()==setno)
                            stu.setScore(setScore);
                    }
                }
    
            }
            for(Student stu: hmap.values()){
                System.out.println(stu.toString());
            }
        }
    }
    
    
    class Student{
        int no;
        String name;
        int score;   //根据题目描述,应该是int,实际情况下double更合适
        Student(int no,String name,int score){
            this.no = no;
            this.name = name;
            this.score = score;
        }
    
        public int getNo() {
            return no;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "no:"+no+" name:"+name+" score:"+score;
        }
    
        @Override
        public boolean equals(Object obj) {
            if(obj == null)
                return false;
            else{
                boolean res = false;
                if(obj instanceof Student){
                    Student s = (Student)obj;
                    if(this.no == s.no)
                        res = true;
                }
                return res;
            }
        }
    
        @Override
        public int hashCode() {
            return no*520+1314; /*这里的hashcode使用下述方法更好*/
            /*
            * int res = 17;
            * res = res*31+no;
            * return res;
            * */
        }
    }
    

    184 - 4

    Description

    在上题的基础上构建一个书单类BookList,该类中用一个列表类对象存放书单,提供添加图书(addBook)、查找图
    书(searchBook)的函数
    main函数从键盘输入多个Book添加到书单中,(添加时,提供书的名称、价格、作者、版本号),而后从键盘读入一
    本书,查找该列表对象中是否包含该书,若包含,输出”found: 该书在列表中的序号”,若不包含,
    输出“not found”,查找时,提供书的名称、作者、版本号。
    

    Input

    添加书的个数
    添加的书
    查找的书
    

    Output

    查找结果
    

    Sample Input

    2
    ThinkingInJava
    86
    BruceEckel
    4
    CoreJava
    95
    CayS.Horstmann
    10
    CoreJava
    CayS.Horstmann
    10
    

    Sample Output

    found: 1 
    

    Post Append Code

    public class Main{
        
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            BookList bl = new BookList();
            int n = s.nextInt();
            for (int i=0; i<n;i++) {
                bl.addBook(new Book(s.next(),
                        s.nextInt(),
                        s.next(),
                        s.nextInt()));
            }
            bl.searchBook(new Book(s.next(),
                        0,
                        s.next(),s.nextInt()));
        }
    }
    
    

    MyAnswer

    import java.util.*;
    
    /*
     * title: BookList
     * author V0W
     * reference:
     * */
    class Book{
        String bookname;
        int price;
        String author;
        int version;
        Book(String bookname,int price,String author,int version){
            this.bookname = bookname;
            this.author = author;
            this.price = price;
            this.version = version;
        }
    }
    
    class BookList{
        List<Book> booklist = new ArrayList<Book>();
        void addBook(Book b){
            booklist.add(b);
        }
        void searchBook(Book b){
            for(Book i:booklist){
                if(i.bookname.equals(b.bookname)&&i.author.equals(b.author)&&i.version==b.version){
                    System.out.println("found: "+booklist.indexOf(i));
                    return ;
                }
            }
            System.out.println("not found");
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Java OJ作业6

          本文链接:https://www.haomeiwen.com/subject/kvsctqtx.html