所以我在设置一些方法时遇到了一些问题,我一直盯着电脑屏幕看了太久。我对5种方法中的4种都有困难。最后两个方法需要调用CompSciStudent类,因此使用'instanceof',因为并非所有数组中的Student都是CompSciStudents。这两种方法需要在Student数组中向Student添加CS语言,另一种方法是检查数组中有多少CompSciStudents知道特定语言。另外两种方法处理整个数组。第一个是将测试分数添加到特定位置的特定学生,阵列中的每个学生在阵列中调用时都会有一个特定的名称。最后一种方法是获得平均测试分数。该方法是调用阵列中Student和CompSciStudents的平均测试分数,然后分别取平均值,然后取平均值。看看Class结构:
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);
}
答案 0 :(得分:0)
您面临的主要问题是应用程序的设计。我建议一些设计方面的改进可以帮助您解决问题。
theStudents[]
声明为数组,而应将其设为List<Student> students
之类的列表。这将大大减少您在注册学生和放弃学生时所付出的额外努力。List<Student> students
(如第1点中所述),最后您的School类将有{ {1}}。这是一对多的关系。List<Department> departments
个对象时,不要忘记覆盖hashCode()
和equals(Object obj)
方法,因为它们是必要的。现在,您可以说&#34; Student
有部门。每个School
都有学生。每个Department
都有主题&#34;。
现在你的课程应该看起来像这样:
Student
注意:我并不是说这是一个完美的设计。但对于你所解释的场景来说,这是非常微妙的。同样,代码设计可能因人而异。