gpt4 book ai didi

java - 如何用java创建学生名册

转载 作者:行者123 更新时间:2023-12-02 00:07:20 25 4
gpt4 key购买 nike

我有一个家庭作业问题,如下所示:

(20 pts) Create a Student class with the following:

  • A private String variable named “name” to store the student’s name
  • A private integer variable named “UFID” that contains the unique ID number for this student
  • A private String variable named “DOB” to store the student’s date of birth
  • A private integer class variable named numberOfStudents that keeps track of the number of students that have been created so far
  • A public constructor Student(String name, int UFID, String dob)
  • Several public get/set methods for all the properties

    getName/setName
    getUFID/setUFID
    getDob/setDob
  • Write a test program, roster.java, that keeps a list of current enrolled students. It should have methods to be able to enroll a new student and drop an existing student.

我并不是要求任何人为我完成这项作业,我只是真的需要一些一般性指导。我认为我的 Student 类制作得很好,但我无法确切地知道 addStudent()dropStudent() 方法应该做什么do - 应该将一个元素添加到数组或其他东西中,还是只是增加学生的数量?到目前为止我的代码看起来像这样。

public class Student {
private String name;
private int UFID;
private String DOB;
private static int numberOfStudents;

public Student(String name, int UFID, String DOB) {
this.name = name;
this.UFID = UFID;
this.DOB = DOB;
}

public String getDOB() {
return DOB;
}

public void setDOB(String dOB) {
DOB = dOB;
}

public int getUFID() {
return UFID; }

public void setUFID(int uFID) {
UFID = uFID; }

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getNumberOfStudents() {
return numberOfStudents;
}

public void setNumberOfStudents(int numberOfStudents) {
Student.numberOfStudents = numberOfStudents;
}

public static void addStudent(String name, int UFID, String DOB) {
numberOfStudents++;
}

public static void dropStudent(String name) {
numberOfStudents--;
}
}

当我完成此工作时,任何指导将不胜感激。

最佳答案

作业是自己写的:您需要一个拥有并维护学生集合的名册类:

public class Roster {
private Set<Student> roster = new HashSet<Student>();

public void addStudent(Student s) { this.roster.add(s); }

public void removeStudent(Student s) { this.roster.remove(s); }
}

关于java - 如何用java创建学生名册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com