Link to home
Start Free TrialLog in
Avatar of Ansary100
Ansary100

asked on

please I need help in java programming

I need to modify the Student class. 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 be initially zero.I need to Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. Also I need to provide a method called getTestScore that accepts the test number and returns the appropriate score.and I need to Provide a method called average that computes and returns the average test score for this student.I should Modify the toString method such that the test scores and average are included in the description of the student.finaly I need to Modify the driver class main method to exercise the new Student methods



public class Student
{
   private String firstName, lastName;
   private Address homeAddress, schoolAddress;

 
   public Student (String first, String last, Address home,
                   Address school)
   {
      firstName = first;
      lastName = last;
      homeAddress = home;
      schoolAddress = school;
   }

   public String toString()
   {
      String result;

      result = firstName + " " + lastName + "\n";
      result += "Home Address:\n" + homeAddress + "\n";
      result += "School Address:\n" + schoolAddress;

      return result;
   }
}

Avatar of Mick Barry
Mick Barry
Flag of Australia image

Add member vars for the three text scores and initialise them to zero.
And add a sperate constructor that took the name details plus the scores.
Well, this is totally a homework, and nobody is going to do it for you.
Avatar of Ansary100
Ansary100

ASKER

Hi Could you please at lease put me in the right direction by providing steps that could help me in solving it.
homework!
public class Student
{
   private String firstName, lastName;
   private Address homeAddress, schoolAddress;
   private int score[3];

 
   public Student (String first, String last, Address home,
                   Address school)
   {
      firstName = first;
      lastName = last;
      homeAddress = home;
      schoolAddress = school;
      score[0] = score[1] = score[2] = 0;
   }

   public String toString()
   {
      String result;

      result = firstName + " " + lastName + "\n";
      result += "Home Address:\n" + homeAddress + "\n";
      result += "School Address:\n" + schoolAddress;

      return result;
   }
   void setScore (int test, int score) {
        this.score[test] = score;
   }

   int get Score (int test) {
       return this.score[test];
}

A constructor is a block that has the name of the class, looks like a method but it is not.  So, declare another Student block without any parameters and do what the instructions say, instead of setting to the variables set them to zero.  The main method would then call the new constructor that has no parameters.  Create some variables to hold the data that you need, may need to work with arrays.  Create your new methods that work with the variables.  Sounds like you really (REALLY) need to hit the books though.  
>> Each student object should also contain the scores for three tests
Add a data member "scores" that is an int array of size 3: int scores[] = new int[3];
>> Provide a constructor that sets all instance values based on parameter values.
Take a copt of the current constructor and extend it with 3 int parameters (for the three scores)
>> Overload the constructor such that each test score is assumed to be initially zero.
For the existing constructor, make sure to set the three scores to 0
>> I need to Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score.
Well, make such a function returning void that takes as parameters: int index, int score
Use the "index" parameter as index in the scores array.
Remark that while the parameter is 1 to 3, the indices in the array are 0 to 2
>> Also I need to provide a method called getTestScore that accepts the test number and returns the appropriate score
Make such a function that returns an int and takes a parameter int index.
Use that "index" parameter as index in the scores array. Same remark.
>> and I need to Provide a method called average that computes and returns the average test score for this student
Make a function average() returning an int being the sum of the three scores divided by 3.
>> I should Modify the toString method such that the test scores and average are included in the description of the student.
Append to the result string
- a string containing the result of getTestScore(1) to getTestScore(3)
- a string containing the result of average()
>> I need to Modify the driver class main method to exercise the new Student methods
You didn't post the driver class
Oliver_Dornauf, if you know it's homework why do you post full blown code?
Read the EE rules.
zzynx, And why do you repeat comments that have already been posted.
like?
Absolutely not a homework. I’m a self learner. I’m not a student. I’m trying to learn java Programming through books and your help. I studded Java Script and thought that I could learn java programming but it seems to me that java programming is more difficult. That’s why I’m trying to read and learn. I’m not associated with any university at the moment. Please Help.
Java : Articles and Tutorials: http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/
Java tutorials: http://www.freewarejava.com/tutorials/index.shtml

Lots of inspiring small examples can be found at:
- http://javaalmanac.com/egs/index.html
- http://www.rgagnon.com/bigindex.html
- http://home-1.worldonline.nl/~bmc88/java/sbook/index.html

Global java Swing tutorials
- http://java.sun.com/docs/books/tutorial/uiswing/index.html
- http://java.sun.com/developer/onlineTraining/GUI/Swing1/ (Fundamentals of JFC Swing: Part 1)
- http://java.sun.com/developer/onlineTraining/GUI/Swing2/ (Fundamentals of JFC Swing: Part 2)
- http://mail.phys-iasi.ro/Library/Computing/jfc_unleashed/index.htm (JFC unleashed)

Java Books
Thinking in java By Bruce Eckels is one of the Good books about Java
http://64.78.49.204/TIJ-1st-edition.zip (Thinking in Java - 1st Edition)
http://64.78.49.204/TIJ-2nd-edition.zip (Thinking in Java - 2nd Edition)
http://64.78.49.204/TIJ-3rd-edition4.0.zip (Thinking in Java - 3rd Edition)
http://64.78.49.204/TIEJv1.1.zip (Thinking in Enterprise Java)
I’m not a beginner but I’m asking help of putting me in the right direction on solving some questions that I found challenged my ability. Would you please help in put me in the right track in answering this question
You've already recieved help pointing you in the right direction.
All you have left is to implement the constructor as I outlined above (copy your existing one and add extra parameters), update your toString() method and add an average method which is just the sum divided by 3

int average = (test[0] + test[1] + test[2]) / 3;

What exactly are you having problems with?
It seems to me that Oliver_Dornauf  did put me in the right direction. but I always like to see different views. befor I accept the better one thank you "objects"
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial