Monday 18 October 2010

Chapter 4 TEST





Objects First with Java
Chapter Four Test
Grouping Objects

A school administrator wants to create an application that will allow her to store and manipulate information about student courses and the grades that the student receives on the courses. 

She has been developing the application as a BlueJ project which currently has four classes.
class Course: stores the course title and the grade a student received on the course.
class Student: stores the student name, and a list of the Courses that the student has taken.
class StudentList: stores a list of all the students in a year (for example seniors or juniors).
class Admin: is a test application for the project.



The administrator is very busy, and has asked you to help her out with her application. 

You can see straight away that there are compilation problems. In fact there are only two errors, one is a compile-time error in StudentList, the other is a run-time error in class admin. When you run class admin (void main(args)) you will see the effects of the run-time error. The output should be..
John 0
Lizzie 0

Your answers should be written in your blog. Include any new relevant code with comments. 

1. Find the two problems and fix them. Explain the nature of the two problems that you fixed.

First Error:

The while loop in class Admin has no incrementation 

int index =0;
    while (index<seniors.classSize())
    {
        System.out.print(seniors.getStudent(index).getName());
        System.out.println(""+ " " + seniors.getStudent(index).getGPA());
        
        index ++;
     }

So I added the index ++ in the end to complete the while lop


public class Admin
{
static StudentList seniors;
public static void main(String args[])
{
    seniors = new StudentList();

    seniors.add(new Student("John"));
    seniors.getStudent(0).addCourse(new Course ("English",78));
    seniors.getStudent(0).addCourse(new Course ("Math",97));
    seniors.getStudent(0).addCourse(new Course ("History",82));

    seniors.add(new Student("Lizzie"));
    seniors.getStudent(1).addCourse(new Course ("English",89));
    seniors.getStudent(1).addCourse(new Course ("Math",88));
     seniors.getStudent(1).addCourse(new Course ("History",86));

     int index =0;
    while (index<seniors.classSize())
    {
        System.out.print(seniors.getStudent(index).getName());
        System.out.println(""+ " " + seniors.getStudent(index).getGPA());
     }

}//main
}//class Admin


import java.util.*;

public class StudentList
{
private ArrayList  <Student> listOfStudents;
private int numberOfStudents;

StudentList ()
{
    listOfStudents=new ArrayList <Student> ();
    numberOfStudents=0;

public void add(Student newStudent)
{
    listOfStudents.add(newStudent);
    numberOfStudents++;
}//add 

public Student getStudent(int index)
{
    return listOfStudents.get(index);
}//get 

public int classSize()
{
     return numberOfStudents;
}//classSize 

}//StudentList



import java.util.*;

public class Student
{
private String name;
private ArrayList <Course> courseList;
private int numberOfCourses;
private double GPA;

Student (String initName)
{
    name = initName;
    courseList=new ArrayList <Course>();
    numberOfCourses=0;
    GPA=0.0;
}//constructor 

public void addCourse(Course newCourse)
{
    courseList.add(newCourse);
    numberOfCourses++;

public String getName()
{
    return name;

public double getGPA()
{
    return GPA;
}
}//class Student



public class Course
{

private String courseTitle;
private int grade;

Course ()
{
}//constructor

Course (String initCourseTitle, int initGrade)
{
    courseTitle = initCourseTitle;
    grade = initGrade;
}//constructor

public void setCourseTitle(String initCourseTitle)
{
    courseTitle=initCourseTitle;
}//setCourseTitle

public void setGrade(int initGrade)
{
    grade=initGrade;
}//setGrade

public String getCourseTitle()
{
    return courseTitle;
}//getCourseTitle

public int getGrade()
{
    return grade;
}//getGrade 

}//class Course

2. Add a new student to class Admin with 4 courses. Run the class. Does it behave as expected?


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.

We have not learned For each loops in class and my attempts of trying to understand myself from the BlueJ book have all ended in complete failure.
I believe this method must be public so it can be called through class admin and not only through class student. 

************************************************************************
Part 2

After you complete each of the tasks (4-7) show your running program to the instructor.
At the end of the test your answers should be written on a copy of this document.
Answer the questions after each question where indicated (answer..).

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?

it returns a string(name of the object), @ ,and a number

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.



answer (code) ..

toString()


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()



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()



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()









No comments:

Post a Comment