Hello! I am working on a program and I just want to know if I have followed the directions correctly. My code compiles and runs (Jgrasp) but I still feel like I am missing some items in my code? If anyone could take a second look and let me know it will be greatly appreciated.
Modify the Student class as follows:
a. Each Student object should also contain the scores for three tests (in addition to the existing instance variables).
b. Modify the existing constructor so that each test score is set to zero.
c. Overload the constructor (create an additional Student constructor) so that all instance values are set by parameter values – the three test scores will be set by additional test score values in the parameter list.
d. Provide a method called setTestScores that accepts two parameters: the test number (1, 2, or 3) and the score.
e. Provide a corresponding method called getTestScores that accepts one parameter, the test number (1, 2, or 3), and returns the score for that test number.
f. Provide a method called testAverage that computes and returns the average test score for this student. (Hint: the value returned should be a double. Don’t store the average, calculate it every time you print a student.)
g. Modify the toString method such that the test scores and average are included in the description of the student.
3. Modify the StudentBody main method to exercise the new Student methods:
a. Use each constructor twice to create 2 new Students, for a total of 4. (You can use the existing Students, John and Marsha, with the constructor in 2b; make up addresses and names for 2 new students using the overloaded constructor in 2c.)
b. Print out each Student object before any changes are made (zero test scores and average are okay).
c. Use the setTestScores method to change test scores for John and Marsha, the Students with 0 test scores.
d. Use the getTestScores method to display the scores that were changed for each Student. Print a line that resembles (without quotes): "John’s score for Test 1 is 89."
e. Print out each Student object after the changes are made.
public class Address { private String streetAddress, city, state; private long zipCode; // Constructor: Sets up this address with the specified data public Address (String street, String town, String st, long zip) { streetAddress = street; city = town; state = st; zipCode = zip; } // Returns a desciption of this address object public String toString() { String result; result = streetAddress + " \n"; result += city + ", " + state + " " + zipCode; return result; } }
public class Student { private String firstName, lastName; private Address homeAddress, schoolAddress; private String ts1, ts2,ts3; private int t1,t2,t3; // Constructor: Sets up this student with the specified values public Student (String first, String last, Address home, Address school) { firstName = first; lastName = last; homeAddress = home; schoolAddress = school; t1 = 0; t2 = 0; t3 = 0; } public Student (String first, String last, Address home, Address school,int t1,int t2,int t3) { firstName = first; lastName = last; homeAddress = home; schoolAddress = school; this.t1 = t1; this.t2 = t2; this.t3 = t3; } // Returns a string description of this student object public void setTestScores(String test,int score) { if(test.equals("ts1")) t1 = score; else if(test.equals("ts2")) t2 = score; else t3 = score; } public int getTestScores(String test) { if(test.equals("ts1")) return t1; else if(test.equals("ts2")) return t2; else return t3; } public double testAverage() { double avg = (double)(t1+t2+t3)/3; return avg; } public String toString() { String result; result = firstName + " " + lastName + "\n"; result+= "Home Address: \n" + homeAddress + "\n"; result+= "School Address: \n" + schoolAddress; result+= "Test1 : " + t1+" "; result+= "Test2 : " + t2+" "; result+= "Test3 : " + t3+"\n"; result+= "Average : "+(double)(t1+t2+t3)/3; return result; } }
public class StudentBody { // Creates some Address and Student objects and prints them public static void main (String[] args) { Address school = new Address ("800 Lancaster Ave." , "Villanova","PA" , 19085); Address jHome = new Address ("21 Jump Street", "Lynchburg","VA", 24551); Student john = new Student ("John", "Smith", jHome, school); Address mHome = new Address ("123 Main Street", "Euclid", "OH", 44132); Student marsha = new Student ("Marsha", "Jones", mHome, school); Student newJhon = new Student("John2","Smith", jHome, school,73,80,95); Student newMarsha = new Student ("Marsha2", "Jones", mHome, school,99,85,65); System.out.println (john); System.out.println(); System.out.println (marsha); john.setTestScores("ts1",73); System.out.println(); System.out.println("John Test Score for Test1 is : "+john.getTestScores("ts1")); System.out.println(); marsha.setTestScores("ts2",85); System.out.println(); System.out.println("marsha Test Score for Test2 is : "+marsha.getTestScores("ts2")); System.out.println(); System.out.println (john); System.out.println(); System.out.println (marsha); System.out.println(); System.out.println (newJhon); System.out.println(); System.out.println (newMarsha); } }