Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: dunno how to test this code so i dont know if it works help please

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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"

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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?

    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();
            }
    }

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: dunno how to test this code so i dont know if it works help please

    Quote Originally Posted by jonathanfox View Post
    ...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:
    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
    Last edited by Zaphod_b; July 23rd, 2012 at 06:20 PM.

  6. The Following User Says Thank You to Zaphod_b For This Useful Post:

    jonathanfox (July 24th, 2012)

  7. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: dunno how to test this code so i dont know if it works help please

    it works , thought the answer for it was way too easy but screw it it does what its supposed too, thanks guys

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  3. Replies: 4
    Last Post: July 25th, 2011, 06:12 PM
  4. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  5. I dunno What I'm Doing :(
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2010, 09:36 PM