Results 1 to 6 of 6
  1. #1
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default Need help with class problem!!

    Here is the instruction

    The Student Class: Implement a class Student. For the purpose of this exercise, a student has a name and a total quiz score. Supply an appropriate constructor. (Remember, when you create a student, the total quiz score will be 0, so you don't need to pass that as a parameter. The constructor will take only a single String parameter.) Also, supply the following methods:

    getName()
    addQuiz(int score)
    getTotalScore()
    getAverageScore()

    To compute the last method, remember that you'll also need to keep track of how many quizzes the student actually took. The average score should be a double, even though each individual quiz score is an int.

    this is what i've got so far \:

    public class Student
    {
    private String x;
    private int y;
    private int totalnum;
    private int score;

    /**
    * name.
    *
    * @param name
    */
    public Student(String name)
    {
    x = name;
    totalnum = 0;
    y = 0;
    }
    /**
    * get name.
    *
    * @return name.
    */
    public String getName()
    {
    return x;
    }
    /**
    *
    * add quiz.
    *
    * @param score
    */
    public void addQuiz(int score)
    {

    y += score;
    totalnum++;

    }
    /**
    * total score.
    *
    * @return score.
    */
    public int getTotalScore()
    {

    return y += score;
    }
    /**
    * get average.
    *
    * @return average.
    */
    public double getAverageScore()
    {
    return y / totalnum;
    }
    }
    here's the result from the teacher's checkresults :

    Testing student class Student
    --------------------------------------------------------------------------------
    X getTotalScore() method defined incorrectly.
    X Are addQuiz and getAverageScore working correctly? {69, 14} expected:<41.5> but was:<41.0>
    X Are addQuiz and getAverageScore working correctly? {17, 73, 83} expected:<57.666666666666664> but was:<57.0>
    X Are addQuiz and getAverageScore working correctly? {18, 74, 56, 73} expected:<55.25> but was:<55.0>
    --------------------------------------------------------------------------------
    11/15 tests passing (73%)
    any help would be appreciated
    Last edited by ricky; 11-23-2009 at 03:18 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ricky View Post
    any help would be appreciated
    With what, specifically? You haven't asked a specific question yet.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Comparing your answers with those expected it looks like you have some sort of rounding problem.

    Java Code:
    public double getAverageScore()
    {
        return y / totalnum;
    }
    Find your notes or textbook and read about "integer division" which is what you are doing in this method. (Yes, I know the return type is double. But the compiler is not going to second guess what you mean: give it a couple of ints and it will perform integer division.)

    (Your code would look nicer if you replaced x and y with more reasonable names...)

  4. #4
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    why does it say "getTotalScore() method defined incorrectly"?

  5. #5
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I think he means that you should declare y, score and totalnum as double, and getTotalScore() should return a double also, not a int. That will fix your problem.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    What is "y" and how is it different to "score"?
    And then why do you add the two together in getTotalScore()?

    I suspect, as mentioned above, you'd spot the problems if you gave your attributes meaningful names. Also, you really should format your code properly...indentation is your friend.

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  2. class problem
    By McChill in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 03:26 AM
  3. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  4. problem in wrapper class
    By binoympappachen in forum New To Java
    Replies: 4
    Last Post: 12-13-2007, 12:31 PM
  5. Class problem
    By Deagel in forum New To Java
    Replies: 1
    Last Post: 10-31-2007, 07:33 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
  •