Write a Java user-defined class called Student
and a class tester program called Project9_lastname
. These will be separate Java source files (Project9_lastname.java
and Student.java
).
Learning outcomes
- Build a program using multiple source files.
- Build a user-defined class.
- Create overloaded mutators.
- Create accessors and mutators.
- Build code to test all code paths.
The Student
class will consist of the following instance variables:
private String name;
private int test1, test2, finalExam;
private double average;
You will write the code for the following methods of the Student
class:
public String toString()
public void setTest1(int t)
public void setTest2(int t)
public void setFinal(int finalExam)
public void setGrades(int t1, int t2, int f)
public void setName(String name)
public int getTest1()
public int getTest2()
public int getFinal()
public String getName()
public double getAverage()
private void doAverage()
[Note that setFinal()
and setName()
must use the this
reference.]
In addition you will write code for the following constructors of the Student
class:
public Student()
public Student(String name)
public Student(String name, int test1, int test2, int finalExam)
[Note that the last two constructors must use the this
reference.]
Whenever a grade is changed, you should call doAverage()
which recalculates the average for that object. This should also be done with the constructors. This will guarantee an up to date average for the user.
The default values for grades should always be zero when not specified. Name should be the empty string (“”) when not provided.
The toString()
method should return a String in the form:
“Name has test grades test1, test2 and finalExam and an average of average.”
The program Project9_lastname.java
will exercise the Student
class with reference variables student1
, student2
and student3
.
With the student1
reference variable, perform the following:
- Use the
Student()
consructor. - Use the
setName()
,setTest1()
,setTest2()
andsetFinal()
mutator methods with the values “Bob Stevens”, 89, 92 and 93 respectively. - Use the
getName()
,getTest1()
,getTest2()
,getFinal()
andgetAverage()
accessor methods to verify the content. - Test the
toString()
method.
With the student2
reference variable, perform the following:
- Use the
Student(String name)
constructor with “James Kline”. - Use the
setGrades()
mutator method with values 75, 66 and 89 respectively. - Use the
getName()
,getTest1()
,getTest2()
,getFinal()
andgetAverage()
accessor methods to verify the content. - Test the
toString()
method.
With the student3
reference variable, perform the following:
- Use the
Student(String name, int test1, int test2, int finalExam)
constructor with the values “Nancy Keen”, 94, 97 and 100 respectively. - Use the
setTest()
mutator method to correct the first test grade from 94 to 95. - Use the
getName()
,getTest1()
,getTest2()
,getFinal()
andgetAverage()
accessor methods to verify the constructor and content. - Test the
toString()
method.
Your output should look something like this:
student1.getName() = Bob Stevens student1.getTest1() = 89 student1.getTest2() = 92 student1.getFinal() = 93 student1.getAverage() = 91.333 Bob Stevens has test grades 89, 92 and 93 and an average of 91.333 student2.getName() = James Kline student2.getTest1() = 75 student2.getTest2() = 66 student2.getFinal() = 89 student2.getAverage() = 76.667 James Kline has test grades 75, 66 and 89 and an average of 76.667 student3.getName() = Nancy Keen student3.getTest1() = 95 student3.getTest2() = 97 student3.getFinal() = 100 student3.getAverage() = 97.333 Nancy Keen has test grades 95, 97 and 100 and an average of 97.333
Submit both Java files to the Learning Management System for grading.