473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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


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
2 9662
horace1
1,510 Expert 1GB
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
thank you for the help
Dec 27 '06 #3

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

Similar topics

5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
6
by: ahin | last post by:
can any one help or give an idea about writing or from where to strat the following program? • Load a list of available courses from a text file into an array (max 4 courses). File format can...
4
by: smsm | last post by:
Project requirements: • Load a list of available courses from the text file “courses.txt” (file is given) into an ArrayList. File format is as follows: 311224 1700 Object Oriented Programming ...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
1
by: laila2ethan | last post by:
Need someones help , I am having a very difficult time understanding parameters and functions, can someone help me answer these questions 1. Write a program that helps an elementary school student...
0
by: yjh0914 | last post by:
hi guys! so im basically editting my post i made earlier as it wznt as specific.. i have to make a program that basically ranks students by their cumulative gpa. the student's info is on a csv file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.