dunno how to test this code so i dont know if it works help please
public class match
{
public int match(Set<Integer> myNumbers, Set<Integer> winningNumbers)
{
myNumbers.retainAll(winningNumbers);
return myNumbers.size();
}
}
what its supposed to do is return the number of numbers in the set myNumbers that match the numbers in the set winningNumbers. But it just seems way to easy to me is it wrong or not? also im not sure how to test it, would appreciate help
Re: dunno how to test this code so i dont know if it works help please
The only thing that confuses me is "you don't know how to test it"....
Ummm, give it some sets of values, and run it through...
numberOfMatchingNumbers = (disSetHere, datSetThere);
Sometimes the simple answer is the correct answer. Sometimes it is not.
Try it and see.
Re: dunno how to test this code so i dont know if it works help please
I tried doing that but when i made two sets and gave them values and tried to use the method , it said "cannot return value that is void"
Re: dunno how to test this code so i dont know if it works help please
Have you tried running in the debugger, or adding some printlns to see what is happening?
Code JAVA:
public class match {
public int match(Set<Integer> myNumbers, Set<Integer> winningNumbers)
{
//print or view in debugger
myNumbers.retainAll(winningNumbers);
//print or view in debugger
return myNumbers.size();
}
}
Re: dunno how to test this code so i dont know if it works help please
Quote:
Originally Posted by
jonathanfox
...not sure how to test it...
Personally, I never (that's never) write a single line of code for a new project until I have a test plan. Really. Never.
Anyhow, how about starting with something simple:
Code java:
import java.util.Set;
import java.util.HashSet;
public class z
{
public static void main(String [] args)
{
Set<Integer> myNumbers = new HashSet<Integer>() {
{
add(1);
add(2);
add(3);
add(4);
add(5);
}
};
Set<Integer> winningNumbers = new HashSet<Integer>() {
{
add(38);
add(2);
add(13);
add(5);
add(15);
}
};
match matchIt = new match();
System.out.printf("Initially:\n Winning Numbers: %s My numbers: %s\n",
winningNumbers, myNumbers);
System.out.println();
int winners = matchIt.match(myNumbers, winningNumbers);
System.out.printf("After match():\n Winning numbers: %s My Numbers: %s\n",
winningNumbers, myNumbers);
System.out.println();
System.out.printf("Winners = %d\n", winners);
}
}
Output:
Initially:
Winning Numbers: [2, 38, 5, 13, 15] My numbers: [1, 2, 3, 4, 5]
After match():
Winning numbers: [2, 38, 5, 13, 15] My Numbers: [2, 5]
Winners = 2
Then, maybe, go back and make a loop that gets five integers from the user and uses a random number generator to ccme up with five integers within a certain range for the winners. Populate the sets with the corresponding values, check for winners and go back through the loop. Exit the loop when the user enters something other than a positive integer. Stuff like that.
Cheers!
Z
Re: dunno how to test this code so i dont know if it works help please
it works :D, thought the answer for it was way too easy but screw it it does what its supposed too, thanks guys