所以我在设置一些方法时遇到了一些问题,我盯着电脑屏幕看太久了。我对5种方法中的4种有困难。最后两个方法需要调用CompCiStudent类,因此使用"instanceof",因为数组中并非所有Students都是CompCiStudents。这两种方法需要向Student数组中的Student添加CS语言,另一种方法是检查数组中有多少CompSciStudents知道特定语言。另外两种方法处理整个数组。第一种方法是只为某个位置的特定学生添加一个测试分数,当在数组中调用时,数组中的每个学生都会有一个特定的名字。最后一种方法是得到平均考试成绩。该方法是调用数组中Students和CompCiStudents的平均测试分数,并分别对每个分数进行平均,然后取这些平均值的平均值。看看类结构:
public class School {
private Student[] theStudents;
public School() {
this.theStudents = new Student[] { null };// needs to start out as empty
}
/*
* next two methods would allow a user to add or drop a student into the
* student array for the school ??Also with Enroll student, should be able
* to assign a class to the student, i.e. Calculas, history, etc??
*/
public void enrollStudent(Student newStudent) {
Student totalStudents[] = new Student[theStudents.length + 1];
for (int i = 0; i < theStudents.length; i++) {
totalStudents[i] = theStudents[i];
}
totalStudents[theStudents.length] = newStudent;
theStudents = totalStudents;
}
public void dropStudent(Student dropStudent) {
Student totalStudents[] = new Student[theStudents.length - 1];
for (int i = 0; i > theStudents.length; i--) {
totalStudents[i] = theStudents[i];
}
totalStudents[theStudents.length] = dropStudent;
theStudents = totalStudents;
}
// add Test Score for a student
public double addTestScore(String newStudent, double testScore) {
testScores[posi] = testScore;
}
/*
* count the number of students in a given class, not the school
*/
public int countClassSize(String course) {
// Need to access how the course names are stored for the school to
// count this size.
String courseName;
if (this.courseName.equals(course)) {
for (int i = 0; i < theStudents.length; i++) {
int count = count + 1;
}
}
}
/*
* get average average score of the student array
* The student array is made up of Regular students and
* CompSciStudents. The average should take the average of
* both the average score for Students and average score of
* CompSciStudents and return the average average.
*/
public double averageAverageScore(String course) {
double averageCourseScore;
if(this.theStudents.equals(course)/2){
return averageCourseScore;
}
}
/*
* add a programming language to only CS students at the school Will need to
* use the instanceof for proper type casting
*/
public String addProgrammingLanguage(String studentName, String programLanguage) {
for (int i = 0; i ; i++);
if (this.theStudents[i] instanceof CompSciStudent);
CompSciStudent tempStudent = (CompSciStudent)this.theStudents[i];
tempStudent.knowsLanguage(knowThisLanguage);
}
/*
* Count the number of students in the school that know a certain
* programming language, again will need to typecast properly
*/
public int numberThatKnowLanguage(String programLanguage) {
for();
if(this.theStudents[i] instanceof CompSciStudent);
CompSciStudent tempStudent = (CompSciStudent)this.theStudents[i];
tempStudent.learnedLanguage(knowThisLanguage);
}
您面临的主要问题是应用程序的设计。我将提出一些设计改进建议,这些改进将有助于你解决问题。
- 与其将
theStudents[]
声明为数组,不如将其设置为类似于List<Student> students
的List。这将大大减少你在招生和退学时所付出的额外努力 - 使用显式方法调用为学生添加主题不应该是理想的情况。科目应该与特定的学生有强烈的联系。为了遵守设计原则,学生对象的设计方式应为"学生有一个名称,has-aid,具有主题"。创建学生对象时要注意添加主题。因此,这就不需要添加额外的方法
- 将学生分类为计算机科学专业的学生、电子专业的学生等应该在单独的班级中处理,比如我们称之为系。现在,为了简单起见,我们只考虑两个部门。一个是计算机科学,另一个是电子。"每个部门都有学生"。根据设计原则(有一个关系),你应该在你的系班级中创建一个
List<Student> students
(如第1点所述),最后你的学校班级将有一个List<Department> departments
。这是一对多的关系 - 在比较两个或多个
Student
对象时,不要忘记覆盖hashCode()
和equals(Object obj)
方法,因为它们是必要的
所以现在,你可以说"School
有个系。每个Department
都有有科目"。
现在你的类应该是这样的:
public class School {
private List<Department> departments;
/*
* All your methods and constructors goes here.
*/
}
public class Department {
private List<Student> students;
/*
* All your methods and constructors goes here.
*/
}
public class Student {
private String name;
private String id;
private String courseStream;
private List<String> subjects;
/*
* All your methods and constructors goes here.
*/
}
注意:我并不是说这是一个完美的设计。但对于你所解释的场景来说,这是相当微妙的。同样,代码设计可能因人而异。