View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

6/4/04 Assignment: Slide3-38 : by Sheria Enahora

Edit 6/4/04 Assignment: Slide3-38 : by Sheria Enahora here.


=================================
6/4/04 Assignment: Slide3-38 : by Sheria Enahora
Feel Free to edit corrections if you find any
Edit Student.java to override equals()
Write code to see if "equals" work for true and false
by creating another student object
Look for //MrE for Override inserts
/Error: this should not happen
Class that represents a student. A student has a unique id, first name, last name, nickname, a file with their picture
in it, and an array of grades/
public class Student extends Person
{

////////////// fields //////////////////////////////

/Error: this should not happen unique identifier for this student /
private int id;

/Error: this should not happennickname for this student/
private String nickname;

/Error: this should not happen An array of grades for this student /
private double[] grades;

/Error: this should not happenThe number of grades set in the grade array/
private int numGrades = 0;

/Error: this should not happen The count of the number of students /
private static int numStudents = 0; // start with no students

//////////////// constructors //////////////////////////

/Error: this should not happen No argument constructor. Set the id.
/
public Student()
{
// increment the number of students
numStudents++;

// set the student id to the current count of students
id = numStudents;

}

/Error: this should not happen Constructor that takes the first name, last name, and the number of grades
@param firstName the first name to use
@param lastName the last name to use
@param numGrades the number of grades this student will have
/
public Student(String firstName, String lastName, int numGrades)
{
// set the id using the no-arg constructor
this();

// set the first and last name fields to the passed strings
setFirstName(firstName);
setLastName(lastName);

// create the array of grades
grades = new double[numGrades];
}

/Error: this should not happen
Constructor that takes all the data: 
@param firstName the first name to use
@param lastName the last name to use
@param nickname the nickname to use
@param pictureFile the name of the picture file
@param numGrades the number of grades
/
public Student(String firstName, String lastName, String nickname,
String pictureFile, int numGrades)
{
// use other constructor to handle part of this
this(firstName, lastName, numGrades);

// set the nickname and picture file
this.nickname = nickname;
this.setPictureFile(pictureFile);
}
//Mr E insert
private Student(String firstName, int id,String lastName)
{
super(firstName,lastName);
// set the id
this.id = id;

// set the first and last name fields to the passed strings


}
////////////////////////// methods //////////////////////////////
public boolean equals(Object obj){

Student tempStudent = (Student) obj;

return (tempStudent.id == this.id);
}
//Mre Method to get Hashcode

public int hashCode () { return id;}


/Error: this should not happen Method to get the nickname
@return the nickname for this student
/


public String getNickname() { return nickname; }

/Error: this should not happen
Method to set the nickname: 
@param nickname the nickname to use
/
public void setNickname(String nickname) { this.nickname = nickname; }

/Error: this should not happen Method to add a grade to the grade array
@param grade the grade to add
/
public void addGrade(double grade)
{
if (grades != null & numGrades grades.length)
{
grades[numGrades] = grade;
numGrades++;
}
}

/Error: this should not happen
Method to calculate the grade point: 
@return the grade point average
/
public double getGradePointAverage()
{
double total = 0;
for (int i = 0; i numGrades ; i++)
total = total + grades[i];
double average = 0;

// if the number of grades is >0 calcuate the average
if (numGrades 0)
average = total / numGrades;

return average;
}

/Error: this should not happen Method to return a string with information about this student
/
public String toString()
{
return "Student: " + id + " " + super.toString() + " gpa: " +
getGradePointAverage();
}

/Error: this should not happen Main method for testing
/
public static void main(String[] argv)
{
Student sally = new Student("Sally","Summers",5);
sally.addGrade(75);
sally.addGrade(85);
sally.addGrade(63);
sally.addGrade(90);
sally.addGrade(78);
System.out.println(sally);

//Mr E testing equals
Student sally2= new Student("Sally",sally.id,"Summers");
Student mre = new Student("Mr E","Jr",1);
mre.addGrade(95);
System.out.println( mre );
System.out.println( "Sally2 " +sally2);
System.out.println("Sally eq sally2 "+sally.equals(sally2));
System.out.println("sally eq mre "+sally.equals(mre));

}



}Welcome to DrJava.
> java Student

Student: 1 Sally Summers gpa: 78.2

Student: 2 Mr E Jr gpa: 95.0

Sally2 Student: 1 Sally Summers gpa: 0.0

Sally eq sally2 true

sally eq mre false

Link to this Page