[英]How to create a roster of students in java
I have a homework assignment problem that looks like this: 我有一个作业分配问题,看起来像这样:
(20 pts) Create a Student class with the following: (20分)使用以下内容创建一个Student类:
- A private String variable named “name” to store the student's name 一个名为“ name”的私有字符串变量,用于存储学生的姓名
- A private integer variable named “UFID” that contains the unique ID number for this student 一个名为“ UFID”的私有整数变量,其中包含该学生的唯一ID号
- A private String variable named “DOB” to store the student's date of birth 名为“ DOB”的私有字符串变量,用于存储学生的出生日期
- A private integer class variable named numberOfStudents that keeps track of the number of students that have been created so far 一个私有的整数类变量,名为numberOfStudents,用于跟踪到目前为止已创建的学生数量
- A public constructor Student(String name, int UFID, String dob) 一个公共构造函数Student(字符串名称,int UFID,字符串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. 编写一个测试程序roster.java,其中包含当前在校学生的列表。 It should have methods to be able to enroll a new student and drop an existing student. 它应该具有能够招收新学生和辍学现有学生的方法。
I'm not asking anyone to do this assignment for me, I just really need some general guidance. 我并没有要求任何人为我完成这项任务,我只需要一些一般性指导即可。 I think I have the Student
class pretty well made, but I can't tell exactly what the addStudent()
and dropStudent()
methods should do - should it add an element to an array or something or just increments the number of students? 我认为我的Student
类做得很好,但是我无法确切地说出addStudent()
和dropStudent()
方法应该做什么-它应该向数组中添加元素还是添加一些元素,或者只是增加学生数量? The code I have so far looks like this. 到目前为止,我的代码看起来像这样。
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--;
}
}
Any guidance as I finish this up would be greatly appreciated. 我完成此过程中的任何指导将不胜感激。
The assignment writes itself: you need a Roster class that owns and maintains a collection of Students: 作业会自己写:您需要一个拥有并维护学生集合的名册类:
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); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.