Ex 82 Lottery

484 views
Skip to first unread message

joowo...@gmail.com

unread,
Jan 25, 2015, 8:34:33 AM1/25/15
to moo...@googlegroups.com
Hi,

I have been doing this exercise for an hour, and this is so frustrating. Now I am getting this message:
FAIL: LotteryNumbersTest testDrawNumbersCallRemovesOldNumbersAndDrawsNew
New numbers should have been drawn with drawNumbers call! Numbers were [2, 9, 19, 20, 25, 28, 38]


Here is my code,

public class LotteryNumbers {
    private ArrayList<Integer> numbers;
    
    public LotteryNumbers() {
        this.numbers = new ArrayList<Integer>();
        this.drawNumbers();
    }

    public ArrayList<Integer> numbers() {
        return this.numbers;
    }

    public void drawNumbers() {
        Random randomiser = new Random();
        while (this.numbers.size() < 7) {
            int number = randomiser.nextInt(38) + 1;
            if (!containsNumber(number)) {
                this.numbers.add(number);
                
            }
        }
        Collections.sort(this.numbers);
    }

    public boolean containsNumber(int number) {
        int counter = 0;
        for (int value : this.numbers) {
            if (value == number) {
                counter++;
            }
            if (counter >= 1) {
                return true;
            }
        }
        return false;
    }


 What do you think is wrong with my code? 

Jarmo Isotalo

unread,
Jan 25, 2015, 10:35:16 AM1/25/15
to joowo...@gmail.com, moo...@googlegroups.com
RemovesOldNumbersAndDrawsNew - after the method drawNumbers size of the numbers list should == 7; now you just keep appending numbers to the list.

--
http://mooc.fi
---
You received this message because you are subscribed to the Google Groups "mooc.fi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moocfi+un...@googlegroups.com.
To post to this group, send email to moo...@googlegroups.com.
Visit this group at http://groups.google.com/group/moocfi.
To view this discussion on the web visit https://groups.google.com/d/msgid/moocfi/4f133ba4-e619-4843-b061-5f2fdc38fb45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joni

unread,
Jan 25, 2015, 12:52:28 PM1/25/15
to moo...@googlegroups.com, joowo...@gmail.com
Hi,

Here is an example

LotteryNumbers lotteryNumbers = new LotteryNumbers();
System.out.println(lotteryNumbers.numbers()); // Print numbers
lotteryNumbers
.drawNumbers();
System.out.println(lotteryNumbers.numbers()); // This shouldn't print same numbers

So when you call drawNumbers method, it should always draw new numbers.

BR
Joni
Reply all
Reply to author
Forward
0 new messages