Results 1 to 6 of 6
Thread: Need help with class problem!!
- 11-23-2009, 03:14 AM #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 \:
here's the result from the teacher's checkresults :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;
}
}
any help would be appreciatedTesting 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%)Last edited by ricky; 11-23-2009 at 03:18 AM.
-
- 11-23-2009, 03:36 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Comparing your answers with those expected it looks like you have some sort of rounding problem.
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.)Java Code:public double getAverageScore() { return y / totalnum; }
(Your code would look nicer if you replaced x and y with more reasonable names...)
- 11-23-2009, 06:28 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
why does it say "getTotalScore() method defined incorrectly"?
- 11-23-2009, 12:58 PM #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.
- 11-23-2009, 03:54 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
class problem
By McChill in forum New To JavaReplies: 1Last Post: 02-24-2009, 03:26 AM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
problem in wrapper class
By binoympappachen in forum New To JavaReplies: 4Last Post: 12-13-2007, 12:31 PM -
Class problem
By Deagel in forum New To JavaReplies: 1Last Post: 10-31-2007, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks