Help with student program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk07
    New Member
    • Dec 2006
    • 5

    #1

    Help with student program

    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


    Code:
    //  Represents a street address.
    public class Address
    {
       private String streetAddress, city, state;
       private long zipCode;
    
         public Address (String street, String town, String st, long zip)
       {
          streetAddress = street;
          city = town;
          state = st;
          zipCode = zip;
       }
    
         public String toString()
       {
          String result;
    
          result = streetAddress + "\n";
          result += city + ", " + state + "  " + zipCode;
    
          return result;
       }
    }
    
    
    //Creates a student object with a name, home address, school address, and 3 test scores
        public class Student
       {
          private String firstName, lastName;
          private Address homeAddress, schoolAddress;
          private int test1;
          private int test2;
          private int test3;
             
           public Student (String first, String last, Address home,
                       Address school)
          {
             firstName = first;
             lastName = last;
             homeAddress = home;
             schoolAddress = school;
             test1 = test2 = test3 = 0;
          
          }
           public void setTestScore(int testNumber, int testScore)
          {
             if (testNumber == 1) 
                test1 = testScore;
             else if (testNumber == 2)
                test2 = testScore;
             else
                test3 = testScore;
          }
       
           public int getTestScore(int testNumber)
          {
             if (testNumber == 1)
                return test1;
             else if (testNumber == 2)
                return test2;
             else
                return test3;
          }
       	
          double average = (test1 + test2 + test3)/3.0;
                 	   
            
           public String toString()
          {
             String result = firstName +" "+ lastName +"\n"+
                "Home Address: "+ homeAddress + "\n"+
                "School Address: "+ schoolAddress + "\n"+
                "test1: " + test1 + "\n"+
                "test2: " + test2 + "\n"+
                "test3: " + test3 + "\n"+
                "average: " +average+"\n";
             return result;
          }
       }
    
    
    I need help on this part... how would i include the 3 test scores and average in here
    
    //Exercises the Student methods
    
       import cs1.Keyboard;
        public class StudentBody 
       {
           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", "Gomez", jHome, school);
                     
    				
             System.out.println (john);
            
          }
             }
    THanks
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    think you need to calculate average in toString(), e.g.
    Code:
    public String toString()
    {
    double average = (test1 + test2 + test3)/3.0;
    
    String result = firstName +" "+ lastName +"\n"+
    "Home Address: "+ homeAddress + "\n"+
    "School Address: "+ schoolAddress + "\n"+
    "test1: " + test1 + "\n"+
    "test2: " + test2 + "\n"+
    "test3: " + test3 + "\n"+
    "average: " +average+"\n";
    return result;
    }
    then you would need to set the test scores in main() before printing results, e.g.
    Code:
    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", "Gomez", jHome, school);
    
    john.setTestScore(1,10);
    john.setTestScore(2,20);
    john.setTestScore(3,30);
    System.out.println (john);
    }
    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

    Comment

    • sallyk07
      New Member
      • Dec 2006
      • 5

      #3
      thank you for the help

      Comment

      Working...