?
Solved

please I need help in java programming

Posted on 2005-04-03
17
Medium Priority
?
495 Views
Last Modified: 2008-01-09
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;
   }
}

0
Comment
Question by:Ansary100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +3
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13695306
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.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13695313
Well, this is totally a homework, and nobody is going to do it for you.
0
 

Author Comment

by:Ansary100
ID: 13695364
Hi Could you please at lease put me in the right direction by providing steps that could help me in solving it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 13695413
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];
}

0
 
LVL 7

Expert Comment

by:tomboshell
ID: 13695417
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.  
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13695420
>> 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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13695428
Oliver_Dornauf, if you know it's homework why do you post full blown code?
Read the EE rules.
0
 
LVL 92

Expert Comment

by:objects
ID: 13695445
zzynx, And why do you repeat comments that have already been posted.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13695478
like?
0
 

Author Comment

by:Ansary100
ID: 13695652
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13695671
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13695698
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)
0
 

Author Comment

by:Ansary100
ID: 13695729
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
0
 
LVL 92

Expert Comment

by:objects
ID: 13695743
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?
0
 

Author Comment

by:Ansary100
ID: 13695798
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"
0
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 13695855
>> I’m not a beginner
Depends on your definition of that ;°)

Some corrections:

public class Student
{
   private String firstName, lastName;
   private Address homeAddress, schoolAddress;
   private int score[] = new int[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 Student (String first, String last, Address home, Address school,
                         int score1, int score2, int score3)
   {
      firstName = first;
      lastName = last;
      homeAddress = home;
      schoolAddress = school;
      score[0] = score1;
      score[1] = score2;
      score[2] = score3;
   }

   public String toString()
   {
      StringBuffer result = new StringBuffer();               // <<<<<<< Use of StringBuffer is better
      result.append(firstName + " " + lastName + "\n");
      result.append("Home Address:\n" + homeAddress + "\n");
      result.append("School Address:\n" + schoolAddress);
      result.append("Results= " + getScore(1) + ", " + getScore(2) + ", " + getScore(3) + "\n");   // <<<<<<<
      result.append("Average = " + average());           // <<<<<<
      return result.toString();
   }
   void setScore (int test, int score) {
        this.score[test-1] = score;       // <<<<< -1
   }

   int get Score (int test) {
       return this.score[test-1];  // <<<<<<< -1

   int average() {
       return (score[0]+score[1]+score[2])/3;
   }
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question