It's as follows:
I highlighted the part that I don't understand. What exactly does it mean when it's asking me to make those methods accept only two parameters? It seems like you would need 3, which are the test scores for each respective test?
The code I have so far:
public class Student {
private String ID;
private double test1;
private double test2;
private double test3;
private double average;
public Student(String sID, double sTest1, double sTest2, double sTest3, double sAverage)
{
ID = sID;
test1 = sTest1;
test2 = sTest2;
test3 = sTest3;
average = sAverage;
}
public Student(String sID)
{
ID = sID;
}
public void setTestScore(double sTest1, double sTest2, double sTest3)
{
}
public void getTestScore(double sTest1, double sTest2, double sTest3)
{
}
public double calcAverage()
{
average = (test1 + test2 + test3) / 3;
return average;
}
public void displayInfo(String ID, double test1, double test2, double test3, double average)
{
System.out.println("Student ID: " + ID);
System.out.println("Test 1 Score: " + test1);
System.out.println("Test 2 Score: " + test2);
System.out.println("Test 3 Score: " + test3);
System.out.println("Average test score: " + average);
}
}
Any insight as to what it's expecting me to do with the getTestScore
and setTestScore
methods would be appreciated.
Edit: Looks like the solution is to just use an array to store the values? I thought that would defeat the purpose of structuring it this way but it seems like as a beginner my options are a bit limited.
In answer to "Are there any cases where you should always use this ?" You should use it when it is needed to avoid ambiguity, for example if there is another variable with the same name in scope.
Definition and Usage If you omit the keyword in the example above, the output would be "0" instead of "5". this can also be used to: Invoke current class constructor. Invoke current class method.
Java has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
between() is a static method of the Range which is used to obtain an instance of Range with the specified minimum and maximum value. The specified minimum and maximum values are inclusive in nature.
What exactly does it mean when it's asking me to make those methods accept only two parameters? It seems like you would need 3, which are the test scores for each respective test?
That would be one way to do it, but they want to do it slightly differently.
They want to have a method that sets the score only for a single test.
That is probably better as it
can be adapted easily to a greater number of tests
allows to set the scores one-by-one (as you may not even know the other scores yet).
So the "extra" parameter specifies which test you are talking about here.
To set all three scores, you would call this method three times (with different parameters).
Setter should have 2 parameters: test number and score
public void setTestScore(int testNo, double score) {
//YOUR HOMEWORK
}
Getter should return the score for the test number
public double getTestScore(int testNo) {
return YOUR_HOMEWORK;
}
Your Assignment in this case is to think of a good data structure to hold the results of a number of tests (currently three) and to retrieve them at a later time.
Hint: as others here have already suggested, you might think of a datastructure to map key values (your test number) to values (your score). The most basic of these structures being an array where the index is your key (watch out: zero-based!) and score is the value at that index...
With Arrays
You could still have some way of initialization to specify how many tests there are to be. Lets for a moment think of double[] scores = new double[3];
as a member variable in your class. Setting the 3rd test to score
would be as simple as scores[2] = score;
. Getting the result of the first test simply is scores[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With