# Need help with class problem!!

• 11-23-2009, 04:14 AM
ricky
Need help with class problem!!
Here is the instruction

Quote:

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()
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 \:

Quote:

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;
}
/**
*
*
* @param 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 :

Quote:

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
• 11-23-2009, 04:34 AM
Fubarable
Quote:

Originally Posted by ricky
any help would be appreciated

With what, specifically? You haven't asked a specific question yet.
• 11-23-2009, 04:36 AM
pbrockway2
Comparing your answers with those expected it looks like you have some sort of rounding problem.

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...)
• 11-23-2009, 07:28 AM
ricky
why does it say "getTotalScore() method defined incorrectly"?
• 11-23-2009, 01:58 PM
sky
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.
• 11-23-2009, 04:54 PM
Tolls
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.