I want to correctly validate a method regardless of the parameter ordering.
Therefore, snippet 1 and 2 should both pass the test case in snippet 3.
(snippet 1)
public Integer compute_speed(Integer distance, Integer time) {
return distance/time;
}
(snippet 2)
public Integer compute_speed(Integer time, Integer distance) {
return distance/time;
}
You can assume the two snippets as different code submissions by two students. And you can assume that the number of parameters can be as large as 10.
In the test case, I wrote,
(snippet 3)
return compute_speed(3, 1).equals(3);
This validates snippet 1 but fails for 2. How can I make it such that both snippets pass the test case?
If only there was something like,
return compute_speed(distance = 3, time = 1).equals(3);
Thanks in advance...
I don't think you can have both test cases pass. Generally,
a/b
will not equalb/a
.In fact, assuming snippets 1 and 2 are in the same class, it won't even compile, because both methods have the same signature. How would Java choose which method to invoke, when both are equally valid?
I don't see any reason to have more than one method here. That's all that is needed to compute the speed.
Java doesn't have the feature of named parameter notation that, say, PL/SQL has:
UPDATE
With the question being updated to testing different classes from different students, I would have a test case like this:
That would cover both cases of parameter ordering.
A cleaner way would be to create an interface with a
Integer computeSpeed(Integer time, Integer distance);
method:You then ask the students to implement it and call their implementation StudentNameDistanceCalculator. For example you will receive the following classes from your students:
You can then load all their classes in one project and test all the classes at once. For example with TestNg:
And you will get a detailed report of who doesn't pass the test: