In: Computer Science
Write a java program StudentDriver class to test the methods of the Student class.
Use data:
Mary Brown 1234
John Jones 5678
Maty Jones 1234
Note: we are including Mary Jones twice so we can test the
"equals" method.
We can test "compareTo", by test student1 and student2, then
student2 and student3.
Just submit your driver class as text file.
Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets...
public class Student implements Comparable { //instance variables private String lastName; private String firstName; private String ID; private double totalScore; private int numQ; //Constructor public Student(String lastName, String firstName, String ID) { this.lastName = lastName; this.firstName = firstName; this.ID = ID; totalScore = 0; numQ = 0; } //instance methods public void setLast(String lastName) {this.lastName = lastName;} public void setFirst(String firstName) {this.firstName = firstName;} public void setID(String ID) { this.ID = ID;} public void setTotalScore(double total) {this.totalScore = total;} public void setNumQ(int numQ) {this.numQ = numQ;} public String getLast() {return this.lastName;} public String getFirst() {return this.firstName;} public String getID() {return this.ID;} public double getTotalScore() {return this.totalScore;} public int getNumQ() {return this.numQ;} public void addQuiz(int score) { totalScore += score; numQ++;} public double getAverage() {return totalScore/numQ;} public boolean equals(Student other) {return this.ID.equals(other.ID);} public int compareTo(Student other) {return this.lastName.compareTo(other.lastName);} public String toString() {return this.lastName + ", " + this.firstName + " " + "\n" + this.ID + "\n";} }
Java code:
public class StudentDriver {
public static void main(String args[])
{
System.out.println("Student driver class..");
Student s1 = new Student("Mary","Brown","1234");
Student s2 = new Student("John","Jones","5678");
Student s3 = new Student("Mary","Jones","1234");
Student s4 = new Student("Mary","Jones","1234");
System.out.println("Testing compareTo method with s1 and
s2:");
System.out.println(s1.compareTo(s2));
System.out.println("Testing compareTo method with s2 and
s3:");
System.out.println(s2.compareTo(s3));
System.out.println("Testing equals method with s3 and s4:");
System.out.println(s3.compareTo(s4));
System.out.println("Testing addQuiz method:");
s1.addQuiz(50);
System.out.println("After calling addQuiz. TotalScore =
"+s1.totalScore+" numQ = "+s1.numQ);
System.out.println("Testing setTotalScore and
getTotalScore:");
s1.setTotalScore(40);
System.out.println("After calling getTotalScore:
"+s1.getTotalScore());
s2.setTotalScore(30);
System.out.println("calling getAverage method:");
System.out.println(s1.getAverage());
System.out.println("calling toString method ");
System.out.println(s1.toString());
}
}
Execution screenshot;