Java:创建一个简单类,并测试其方法(第四周)

题目来源:大工慕课 链接
作者:Caleb Sung

题目要求

  1. 阅读Core Java中的示例代码清单4.2。
  2. 实现一学生类,其Fields包括身份证,姓名,性别,年龄,专业,GPA以及学生已经注册的课程列表。 另外,方法还包括分别获取所有子类的姓名以及已注册课程的行为。
  3. 在主函数中,创建如下所示的学生数组,然后测试Student类的所有方法。
ID Name Gender Age Major Courses
150001 HanMei F 19 E-Commerce DB, Java, Econmics
150002 LiLei M 20 E-Commerce DB, Java, Data Structures
150003 Tom M 19 Bioinformatics Java, Data Structures, Biology
150004 Jerry M 18 Bioinformatics DB, Java, Data Structures

解答示范

public class StudentTest {

    public static void main(String[] args) {
        //Database initialization;
        Student[] stu = new Student[4]; 
        stu[0] = new Student(150001, "Hanmei", 'F', 19, 3, "E-Commerce", 3.3);
        stu[1] = new Student(150002, "LiLei", 'M', 20, 3, "E-Commerce", 3.5);
        stu[2] = new Student(150003, "Tom", 'M', 19, 3, "Bioinformatics", 3.4);
        stu[3] = new Student(150004, "Jerry", 'M', 18, 3, "Bioinformatics", 3.2);

        stu[0].enroll("DB");
        stu[0].enroll("Java");
        stu[0].enroll("Econmics");
        stu[1].enroll("DB");
        stu[1].enroll("Java");
        stu[1].enroll("Data Structures");
        stu[2].enroll("Java");
        stu[2].enroll("Data Structures");
        stu[2].enroll("Biology");
        stu[3].enroll("DB");
        stu[3].enroll("Java");
        stu[3].enroll("Data Structures");

        //Output
        for(int i = 0; i <= 3; i++) {
            System.out.println(stu[i].getName()+" (ID: "+stu[i].getId()+") :");
            //System.out.println("Gender: "+stu[i].getGender());
            //System.out.println("Age: "+stu[i].getAge());
            //System.out.println("Major: "+stu[i].getMajor());
            //System.out.println("GPA: "+stu[i].getGpa());
            System.out.println("Courses:");
            for (String c : stu[i].getCourses())
                System.out.println("@ "+c);
            System.out.println("---------------------------------");
        }System.out.println("Finished.");
        System.out.println("---------------------------------\n");

        //Add course
        System.out.println("Do you want to add courses to students? (Y/N)");
        Scanner in = new Scanner(System.in);
        char sym = in.next().charAt(0);
        if (sym == 'Y') {
            System.out.println("Please input a student's id(the student must be in the list:");
            Scanner inp = new Scanner(System.in);
            String tmp = inp.nextLine();
            for(int i = 0; i <= 3; i++) {
                String name = stu[i].getName();
                if(name == tmp) {
                    in = new Scanner(System.in);
                    String cor = in.nextLine();
                    stu[i].enroll(cor);
                    if(stu[i].enroll(cor) == true) {
                        System.out.println("Successfully enrolled.");
                        break;
                    }else {
                        System.out.println("Failed in enrolling");
                        break;
                    }
                }System.out.println("Cannot enroll more courses!");
            }inp.close();
        }
        else if (sym == 'N')
            System.out.println("Thanks for using! :)");
        else
            System.out.println("Illegal Command!");
        in.close();
    }

    public static class Student{
        // fields
        private long id;
        private String name;
        private char gender;
        private int age;
        private String major;
        private double gpa;
        private String[] courses;

        // constructors
        public Student(long id, String name, char gender, int age, int maxCourses, String major, double gpa) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.age = age;
            this.major = major;
            this.gpa = gpa;
            //this.courses = courses;
            courses = new String[maxCourses];
        }

        //methods
        public long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public char getGender() {
            return gender;
        }

        public int getAge() {
            return age;
        }

        public String getMajor() {
            return major;
        }

        public double getGpa() {
            return gpa;
        }

        public String[] getCourses() {
            return courses;
        }

        boolean enroll(String course) {
            for (int i = 0; i < this.courses.length; ++i) {
                if (courses[i] == null) {
                    courses[i] = course;
                    return true;
                }
            }return false;
        }
    }
}

你可能感兴趣的:(Java与面向对象编程)