# Thread: Need help with class problem!!

1. Member
Join Date
Oct 2009
Posts
8
Rep Power
0

## 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 04:18 AM.

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

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
why does it say "getTotalScore() method defined incorrectly"?

5. Member
Join Date
Nov 2009
Posts
96
Rep Power
0
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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
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.

#### Posting Permissions

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