Right Click Here to Download Assignment Source Code
Alright ladies and gentlemen, it’s time to put your knowledge to the test!
I’ve been receiving tons of emails and comments with respect to wanting programming assignments. But I wasn’t sure exactly how I was going to go about doing it, until now.
Here’s what we’re going to do, I will outline the requirements for a practice assignment, and I will include an archive file with the contents of the assignment (at the top of this post). I will also include a video which will explain how to import the assignment into your SpringSource Tool Suite IDE and set it up so you’ll be good to go.
Sound good?
The Requirements
The assignment is to simulate the lottery. You will need to implement code that will generate 6 lottery numbers between 1 and 49 (inclusive), you will then need to implement the code that will read in 6 numbers that you will type into the console yourself. Then the numbers you input will be compared against the randomly generated lottery numbers and it will output which numbers match (if any).
Here’s the catch, you will need to make sure there are no duplicate numbers (either when being randomly generated or inputted in the console). It’s just like a real lottery after-all!
Video Explanation on How to Import the Assignment
Here’s a video that will explain how to import the assignment into your IDE.
Easy to Understand
Easy to Implement
Extremely Useful







{ 11 comments… read them below or add one }
Thank you, Trevor! Very good and interesting starting assignment.
It took me about 30 minutes to get the unit tests run sucessfully, but then I had a hard time to figure out where to enter the numbers
But I still have two unsolved problems:
1: Why do you declare a return type of Set (which is an interface only) but use HashSets in your test code?
2: How do I catch malformed/illegal numbers from in.nextInt(). I’m not allowed to catch an IOException in promptUserForLotteryNumbers(). I don’t want a stack trace, but simply force the user to enter a valid integer between 1 and 49.
Glad you got the unit tests running… good point on mentioning that you had a tough time figuring out where to enter the numbers, I should have been more explicit.
To all of those reading the comments who are having trouble figuring out how to enter the numbers, when you use the
Scannerto read input, it will be read from the console window. This means that when you want to “input” the lottery numbers manually, you will need to click on the console window (to give it focus) and you’ll see the cursor blinking… simply type in your number and press enter. This number will be read by theScannerand you’ll need to store the value into a variable for use.To answer your questions:
1) It’s good form to program to interfaces and not concrete classes, therefore I chose the interface Set in the declaration of the method. I would refer you to this discussion in StackOverflow for more information.
2) I didn’t want to make you guys worry about handling exceptions this time through, but if you feel like it, I think you can either catch the generic
Exceptiontype, or the more specificInputMismatchExceptionI can write the code to generate the random numbers (without duplicates) but am having problems trying to move these numbers into the Set. In the method generateLotteryNumbers(), I have tried using generatedLotteryNumbers.add(number) as well as generator.add(number) but this does not work. I’ve tried instantiating a new Set in the method but this does not make sense to me. I’m missing either a step or the whole boat … any hints to point me in the correct direction?
Nevermind – I solved it.
excellent assignment , waiting for the next …
Working on the next assignment right now
you are the best tutor in programming .thank you very much . i will certainly follow you tutorials and i believe i will a good programmer.
Hello from London, I’ve found your java tutorial via the podcasts. Great work in demystifying this stuff, it’s definitely working for me.
Regarding this assignment, I ploughed through it and managed to get it to work. I watched your walkthrough and noticed you employed a few different methods to achieve the same results as I have. However, I want to confirm I wasn’t picking up bad habits and such…
1) The playLottery method I used the following:
// intersection of two sets
userNumbers.retainAll(lotteryNumbers);
2) The generateLotteryNumbers method I used the following:
do {
int randomInt = randomGenerator.nextInt(49);
setOfNum.add(randomInt + 1);
} while (setOfNum.size() < 6);
Thoughts?
I'm glad you used a different method though, I learned a lot (debugging window, unit tests, boolean whiles etc)
This code looks great to me, I’ve never used the
retainAll()method before, but if it’s the intersection of two Sets, then that’s perfect.Very well done!
In the playLottery method example above by Serraphin, performing the retainAll method on those two Sets will destroy the userNumbers Set. A non-destructive way would be the following (refer to http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html, in the Set Interface Bulk Operations section):
//create a new Set, copying the contents of either userNumbers or lotteryNumbers
Set matched = new HashSet(lotteryNumbers);
//run the retainAll (intersection) method against the newly copied Set.
matched.retainAll(userNumbers);
You can see how this works by outputting the two sets after performing the intersection:
System.out.println(“Matched Nums: ” + matched.toString()); //Matched Nums: [35, 24]
System.out.println(“Orignal Nums: ” + lotteryNumbers.toString()); Original Nums: [8, 35, 24, 1, 49, 6]
If you don’t copy the Set before hand, you will directly modify the original set of numbers.
give me an example of a program that use stack and queue to reverse string of characters and how it can be implemented