By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,739 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,739 IT Pros & Developers. It's quick & easy.

Help with student program

P: 5
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to initially be zero. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called getTestScore that accepts the test number and returns the appropriate score. Provide a method called average that computes and returns the average test score for this student. Modify the toString method such that the test scores and average are included in the description of the student. Modify the driver class main method to exercise the new Student methods.

I want to make sure if i did the Student program right
I need help on the how to exercise the new Student methods


Expand|Select|Wrap|Line Numbers
  1. //  Represents a street address.
  2. public class Address
  3. {
  4.    private String streetAddress, city, state;
  5.    private long zipCode;
  6.  
  7.      public Address (String street, String town, String st, long zip)
  8.    {
  9.       streetAddress = street;
  10.       city = town;
  11.       state = st;
  12.       zipCode = zip;
  13.    }
  14.  
  15.      public String toString()
  16.    {
  17.       String result;
  18.  
  19.       result = streetAddress + "\n";
  20.       result += city + ", " + state + "  " + zipCode;
  21.  
  22.       return result;
  23.    }
  24. }
  25.  
  26.  
  27. //Creates a student object with a name, home address, school address, and 3 test scores
  28.     public class Student
  29.    {
  30.       private String firstName, lastName;
  31.       private Address homeAddress, schoolAddress;
  32.       private int test1;
  33.       private int test2;
  34.       private int test3;
  35.  
  36.        public Student (String first, String last, Address home,
  37.                    Address school)
  38.       {
  39.          firstName = first;
  40.          lastName = last;
  41.          homeAddress = home;
  42.          schoolAddress = school;
  43.          test1 = test2 = test3 = 0;
  44.  
  45.       }
  46.        public void setTestScore(int testNumber, int testScore)
  47.       {
  48.          if (testNumber == 1) 
  49.             test1 = testScore;
  50.          else if (testNumber == 2)
  51.             test2 = testScore;
  52.          else
  53.             test3 = testScore;
  54.       }
  55.  
  56.        public int getTestScore(int testNumber)
  57.       {
  58.          if (testNumber == 1)
  59.             return test1;
  60.          else if (testNumber == 2)
  61.             return test2;
  62.          else
  63.             return test3;
  64.       }
  65.  
  66.       double average = (test1 + test2 + test3)/3.0;
  67.  
  68.  
  69.        public String toString()
  70.       {
  71.          String result = firstName +" "+ lastName +"\n"+
  72.             "Home Address: "+ homeAddress + "\n"+
  73.             "School Address: "+ schoolAddress + "\n"+
  74.             "test1: " + test1 + "\n"+
  75.             "test2: " + test2 + "\n"+
  76.             "test3: " + test3 + "\n"+
  77.             "average: " +average+"\n";
  78.          return result;
  79.       }
  80.    }
  81.  
  82.  
  83. I need help on this part... how would i include the 3 test scores and average in here
  84.  
  85. //Exercises the Student methods
  86.  
  87.    import cs1.Keyboard;
  88.     public class StudentBody 
  89.    {
  90.        public static void main (String[] args)
  91.       {
  92.          Address school = new Address ("800 Lancaster Ave.", "Villanova",
  93.                                     "PA", 19085);
  94.  
  95.          Address jHome = new Address ("21 Jump Street", "Lynchburg",
  96.                                    "VA", 24551);
  97.          Student john = new Student ("John", "Gomez", jHome, school);
  98.  
  99.  
  100.          System.out.println (john);
  101.  
  102.       }
  103.          }
THanks
Dec 27 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
think you need to calculate average in toString(), e.g.
Expand|Select|Wrap|Line Numbers
  1. public String toString()
  2. {
  3. double average = (test1 + test2 + test3)/3.0;
  4.  
  5. String result = firstName +" "+ lastName +"\n"+
  6. "Home Address: "+ homeAddress + "\n"+
  7. "School Address: "+ schoolAddress + "\n"+
  8. "test1: " + test1 + "\n"+
  9. "test2: " + test2 + "\n"+
  10. "test3: " + test3 + "\n"+
  11. "average: " +average+"\n";
  12. return result;
  13. }
  14.  
then you would need to set the test scores in main() before printing results, e.g.
Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args)
  2. {
  3. Address school = new Address ("800 Lancaster Ave.", "Villanova","PA", 19085);
  4. Address jHome = new Address ("21 Jump Street", "Lynchburg","VA", 24551);
  5. Student john = new Student ("John", "Gomez", jHome, school);
  6.  
  7. john.setTestScore(1,10);
  8. john.setTestScore(2,20);
  9. john.setTestScore(3,30);
  10. System.out.println (john);
  11. }
  12.  
when run this would look like
John Gomez
Home Address: 21 Jump Street
Lynchburg, VA 24551
School Address: 800 Lancaster Ave.
Villanova, PA 19085
test1: 10
test2: 20
test3: 30
average: 20.0
Dec 27 '06 #2

P: 5
thank you for the help
Dec 27 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.