Results 1 to 13 of 13
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with this code, overload the constructor

    I have to write a program that returns an address and also three test scores. I need to provide a constructor that sets all instance values based on parameter values. I need to overload the constructor such that each test score is assumed to be initially zero and provide a method called setTestScore that accepts two Parameters: the test number 1-3 and the score, and a method called getTestScore that accepts the test number and returns the score.

    Java Code:
    public class StudentBody
    {
    private int value;
    
    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", "Smith", JHome, school);
    Thanks

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    public class Student() {
    private Address home;
    private Address work;
    private Address school;
    private int[] testScores = new int[] {0,0,0}

    void Student(Address _home, Address _work, Address _school) {
    this.home = _home;
    this.work = _work;
    this.school = = _school;
    }

    public int getTestScore(int testNo) {
    return testScore[testNo];
    }

    public void setTestScore(int testNo, int testScore) {
    testScore[testNo] = testScore;
    }
    }

  3. #3
    WishBone33 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    sorry to revive this old thread, but i have this assignment as a homework assignment and i'm having a lot of problems with it. this is what I have so far:

    public class Student
    {
    private String firstName, lastName;
    private Address homeAddress, schoolAddress;
    private int testScore1;
    private int testScore2;
    private int testScore3;



    //-----------------------------------------------------------------
    // Constructor: Sets up this student with the specified values.
    //-----------------------------------------------------------------
    public void Student (String first, String last, Address home,
    Address school)
    {
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;




    }

    public int getTestScore(int testNumber)
    {
    return testScore;
    }

    public void setTestScore(int testNumber, int testScore)
    {
    testScore = testScore;
    }

    public double average()
    {
    return ((testScore1 + testScore2 + testScore3) / 3);
    }

    //-----------------------------------------------------------------
    // Returns a string description of this Student object.
    //-----------------------------------------------------------------
    public String toString()
    {
    String result;

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

    return result;
    }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    'm having a lot of problems with it. this is what I have so far:
    What are they? Can you show the error messages or explain what the problems are?

  5. #5
    WishBone33 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    I'm sorry. I guess i should have included the problem with it. Ok, problem reads:

    Modify the Student class as follows. 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. 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 averages are included in the description of the student. Modify the driver class main method to exercise the new Student methods.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    i'm having a lot of problems with it
    Can you be more specific than just repeating the assignment?
    We're here to help YOU learn java, not to do assignments.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And can you just explain and work out step by step. It's so easy to find where you stuck with. As Norm says, we are not here to do your assignments.

  8. #8
    WishBone33 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Yes i am aware that none of you are here to do my homework. I was not asking anyone to do it for me. I was just hoping that someone would be able to point me in the right direction and guide me to being able to understand the problem and solving it myself.

    When i try to compile it, it says, "cannot find symblol - variable testScore"

  9. #9
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Because you didn't declare it as int testScore

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Please post the full text of error messages. The error message will have the source line number where the error occured. Look at the source and copy the code at that line here also.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by WishBone33 View Post
    When i try to compile it, it says, "cannot find symblol - variable testScore"
    Yes you didn't declare that variable. On the error message you get, there is the line number where you have done this mistake. Look at that line.

  12. #12
    WishBone33 is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    public class Student
    {
    private String firstName, lastName;
    private Address homeAddress, schoolAddress;
    private int testScore1;
    private int testScore2;
    private int testScore3;
    private int testScore;



    //-----------------------------------------------------------------
    // Constructor: Sets up this student with the specified values.
    //-----------------------------------------------------------------
    public void Student (String first, String last, Address home,
    Address school)
    {
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;




    }

    public int getTestScore(int testNumber)
    {
    return testScore;
    }

    public void setTestScore(int testNumber, int testScore)
    {
    testScore = testScore;
    }

    public double average()
    {
    return ((testScore1 + testScore2 + testScore3) / 3);
    }

    //-----------------------------------------------------------------
    // Returns a string description of this Student object.
    //-----------------------------------------------------------------
    public String toString()
    {
    String result;

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

    return result;
    }
    }

    Ok....... so i declared it on line 8. "int testScore" and it compiles ok now, but when i try to run the StudentBody, I get an error saying, "No SuchMethod Error: Student.<init>(Ljava/lang/String;Ljava/lang/String;LAddress;LAddress; )V"
    Last edited by WishBone33; 11-03-2008 at 02:44 AM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    When you get an error message, please copy and paste the WHOLE message.

    public void Student (String first, String last, Address home,
    Address school)
    Constructors do NOT return anything. void is not used with constructors

    testScore = testScore;

    This doesn't do what you expect.
    It sets the argument testScore = to the argument testScore.
    Same as:
    int ix = 10;
    ix = ix; // this does nothing useful

    If you have an arg with the same name as a class variable, you need to tell the compiler that you want to use the class variable by prefixing the variable with this. Eg: this.testScore =
    Last edited by Norm; 11-03-2008 at 03:00 AM.

Similar Threads

  1. Replies: 0
    Last Post: 03-29-2008, 12:38 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. Replies: 0
    Last Post: 09-26-2007, 08:29 PM
  5. return type determines override/overload?
    By hedefalk in forum Advanced Java
    Replies: 4
    Last Post: 07-11-2007, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •