Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - What is this asking me to do?

Tags:

java

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.

like image 299
BaloneyOs Avatar asked Dec 01 '15 08:12

BaloneyOs


People also ask

Should I always use this Java?

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.

What happens if you dont use this keyword in Java?

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.

How do you do between conditions in Java?

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.

How do you find if a number is between two numbers in Java?

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.


2 Answers

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

  1. can be adapted easily to a greater number of tests

  2. 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).

like image 150
Thilo Avatar answered Oct 21 '22 14:10

Thilo


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];

like image 33
Jan Avatar answered Oct 21 '22 13:10

Jan