Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java user-defined class called Student and a class tester called p9. The

ID: 3815031 • Letter: W

Question

Write a Java user-defined class called Student and a class tester called p9. These will be separate Java source files (p9.java and Student.java). The Student class will consist of the following instance variables: private String name; private int test1, test2, finalExam; 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() [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.] The getAverage() method will return the computed average on the fly. The default values for grades should always be zero when not specified. Name should be the empty string (“”) when not provided. Any printed averages (including that retured by toString()) should always be to three (3) decimal places. 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 p9.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() and setFinal() mutator methods with the values “Bob Stevens”, 89, 92 and 93 respectively. Use the getName(), getTest1(), getTest2(), getFinal() and getAverage() accessor methods to verify the mutators. 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() and getAverage() accessor methods to verify the mutator. 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() and getAverage() accessor methods to verify the constructor and mutator. 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

Explanation / Answer

package myProject;
//Class Definition
class Student
{
   //Instance variables
   private String name;
   private int test1, test2, finalExam;
   private double average;
   //Default constructor
   Student()
   {
       name = "";
       test1 = 0;
       test2 = 0;
       finalExam = 0;
       average = 0.0;
   }
   //Parameterized constructor with one argument
   public Student(String name)
   {
       this.name = name;
   }
   //Parameterized constructor
   public Student(String name, int test1, int test2, int finalExam)
   {
       this.name = name;
       this.test1 = test1;
       this.test2 = test2;
       this.finalExam = finalExam;
   }
   //Set the value of test1
   public void setTest1(int t)
   {
       test1 = t;
   }
   //Set the value of test2
   public void setTest2(int t)
   {
       test2 = t;
   }
   //Set the value of final
   public void setFinal (int finalExam)
   {
       this.finalExam = finalExam;
   }
   //Set the values of test1, test2 and final
   public void setGrades(int t1, int t2, int f)
   {
       test1 = t1;
       test2 = t2;
       finalExam = f;
   }
   //Set the name
   public void setName(String name)
   {
       this.name = name;
   }
   //Returns test1
   public int getTest1()
   {
       return test1;
   }
   //Returns test2
   public int getTest2()
   {
       return test2;
   }
   //Returns final
   public int getFinal()
   {
       return finalExam;
   }
   //Returns name
   public String getName()
   {
       return name;
   }
   //Calculates and returns average
   public double getAverage()
   {
       return (test1 + test2 + finalExam) / 3.0;
   }
   //Overrides toString method
   public String toString()
   {
       String message = " ";
       double Avg = Math.round(getAverage() * 1000.0) / 1000.0;
       message += getName() + " has test grades " + getTest1() + ", " + getTest2() + " and " + getFinal()
               + "and an average of " + + Avg + " ";
       //Returns message
       return message;
   }
}//End of class

//Driver class
public class P9
{
   //Main method
   public static void main(String ss[])
   {
       //Creates an object using default constructor
       Student student1 = new Student();
       //Sets the values to the instance variables using mutator methods
       student1.setName("Bob Stevens");
       student1.setTest1(89);
       student1.setTest2(92);
       student1.setFinal(93);
       //Display student information using accessor methods
       System.out.println("student1.getName() = " + student1.getName());
       System.out.println("student1.getTest1() = " + student1.getTest1());
       System.out.println("student1.getTest2() = " + student1.getTest2());
       System.out.println("student1.getFinal() = " + student1.getFinal());
       System.out.printf("student1.getAverage() = %.3f", student1.getAverage());
       //Calls toString()
       System.out.println(student1);
      
       //Creates an object using parameterized constructor with one argument
       Student student2 = new Student("James Kline");
       //Sets the values to the instance variables using mutator method
       student2.setGrades(75, 66, 89);
       //Display student information using accessor methods
       System.out.println("student2.getName() = " + student2.getName());
       System.out.println("student2.getTest1() = " + student2.getTest1());
       System.out.println("student2.getTest2() = " + student2.getTest2());
       System.out.println("student2.getFinal() = " + student2.getFinal());
       System.out.printf("student2.getAverage() = %.3f", student2.getAverage());
       //Calls toString()
       System.out.println(student2);
      
       //Creates an object using parameterized constructor
       Student student3 = new Student("Nancy Keen", 94, 97, 100);
       //Sets the value of rest1 using mutator method
       student3.setTest1(95);
       //Display student information using accessor methods
       System.out.println("student3.getName() = " + student3.getName());
       System.out.println("student3.getTest1() = " + student3.getTest1());
       System.out.println("student3.getTest2() = " + student3.getTest2());
       System.out.println("student3.getFinal() = " + student3.getFinal());
       System.out.printf("student3.getAverage() = %.3f", student3.getAverage());
       //Calls toString()
       System.out.println(student3);
   }//End of main method
}//End of class

Output:

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 93and 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 89and 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 100and an average of 97.333

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote