1. One error found in the admin constructor where the while loop forgot to increment i (i++)
However, I could not find the second error, I need assistance finding it. I was considering whether the test is outdated, and the current BlueJ is compatible with one of the terms that were used to mislead us from finding the error, such as using array syntax for ArrayList, as you've said BlueJ can now read.
2. Add a new student to class Admin with 4 courses. Run the class. Does it behave as expected?
2. No, when we use the admin class' method it prints my student's name without his GPA, instead it says 0.0.
3. Implement method setGPA() of class Student. The implementation of this method requires a local variable to sum all the student's grades. This value of this variable will be constructed by looping (for each) through courseList, each time incrementing the value by the grade obtained on the course. The GPA will be determined by dividing the sum of the grades by the number of courses.
Should this method be private or public? Explain.
Should this method be private or public? Explain.
3. public double setGPA()
{
int totalClasses = 0;
int totalGrades = 0;
for(int index = 0; index < courseList.size(); index++)
{
totalClasses++;
totalGrades = totalGrades + courseList.get(index).getGrade();
}
return totalGrades / totalClasses;
}
It should be public, otherwise the method would only be available to the Student class, and the admit class would not be able to reach it.
4. The toString() method is inherited (from class Object) by class Student (all Java classes inherit from the Object class). What is the output when this method is called on a Student object?
Inherited methods can be replaced by methods with the same signatures (as the ancestral method) in the descendent class, thus overriding the inherited method.
Replace the inherited toString() method of class Student, by writing a new method that returns a String containing the student's name and a list of all their course titles.
4. It returned the following string: "Student@1f80cb01".
public String toString()
{
String studentNClass = "";
for(Course element : courseList)
{
studentNClass = studentNClass + name + " : " + element.getCourseTitle() + " ";
}
return studentNClass;
}
5. Create a new field in StudentList named deansList that holds a list of students who are on the Dean's List.
Create a makeDeansList() method that creates the list from all students in StudentList that have GPAs of 88 or higher.
answer (code)
new field..
makeDeansList()
5.
public void makeDeansList()
{
for(Student individual : listOfStudents)
{
if(individual.getGPA() >= 88)
deansList.add(individual);
}
}
6. Create a method named printDeansList() that prints the names of students on the Deans List with their GPAs. Each student should be printed on a new line.
answer (code)
printdeansList()
6. public void printDeansList()
{
for(Student individual : deansList)
{
System.out.println(individual + " With a GPA of: " + individual.getGPA());
}
}
7. Create a method named findTopStudent() that finds the student with the highest GPA and prints..
(example) The top student is Mario with a GPA of 98
answer (code)
findTopStudent()
7. public Student findTopStudent()
{
Student topStudent = deansList.get(0);
for(Student individual : deansList)
{
if(individual.getGPA() > topStudent.getGPA())
topStudent = individual;
}
return topStudent;
}
No comments:
Post a Comment