Java 101: SuperLotto
So in Java class today, I got to write a lotto ticket program using an array, a nifty method, some string concatenation and a couple other cool Java tricks.
The spec said to create a getLottoTicket method that would create a 5-element array of random numbers between 1 and 47, then add a “mega” random number between 1 and 27. The main method would let the user choose how many lotto tickets to buy, then print out the resulting tickets.
Here’s what I wrote:
// SuperLotto.java
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class SuperLotto
{
public static String getLottoTicket()
{
//create the lottoTicket string and initialize to separator
String lottoTicket = "*****\n";
// create random number generator
Random lottoNumbers = new Random();
// create an array of 5 random numbers and sort it
int [] ticket = new int[5];
for( int counter = 0; counter < ticket.length; ++counter )
{
ticket[counter] = (1 + lottoNumbers.nextInt(47) );
}
Arrays.sort( ticket );
// walk through the sorted array,
// adding each element to the lotto ticket string
for(int counter = 0; counter < ticket.length; ++counter )
{
lottoTicket += ticket[counter];
lottoTicket += " ";
}
//add the mega number & separator to the string and return it
lottoTicket += "MEGA: ";
lottoTicket += (1 + lottoNumbers.nextInt(27) );
lottoTicket += "\n*****\n\n";
return lottoTicket;
}
public static void main( String [] args )
{
// create a variable for number of tickets and get user input
int numberOfTickets;
Scanner input = new Scanner( System.in );
System.out.print( "How many tickets do you want? " );
numberOfTickets = input.nextInt();
// print the ticket(s)
for( int counter = 1; counter <= numberOfTickets; ++counter)
{
System.out.printf( "LOTTO TICKET %d:\n", counter );
System.out.print( getLottoTicket() );
}
System.out.print( "\n\nProgrammed by Jolie O'Dell.\n\n" );
}
}
If this helps you at all, I’m ever so glad.
Also, big freakin’ ups to programmer Greg Houston for creating this lovely code-to-HTML app. Dude, I can’t tell you how much I love you for this. It’ll make blogging about code — both here and probably on Mashable — a hell of a lot easier.
Image courtesy of journeyscoffee.
Awesome stuff Jolie.
How you finding it? Are you enjoying it?
LOVING it. Honestly, I’m rarely happier than when I’m sitting around the computer science lab or at home by myself, just debugging, compiling, and writing code.
I wish I had gotten into OOP 10 years ago… Both my career path and my checking account balance would probably be a lot different right now!
Well at least you’re doing it now which is fantastic. Welcome to the game.
Thanks! Happy to be here.
I don’t know about your assignment, but are you allowed to have repeated number in your ticket? This could happen with your code.
Ah, good point indeed. That wasn’t included in the spec, but it probably should have been.
Nice work. You may want to replace the numbers with constants like this though:
private static final int MAX_RANDOM_NUMBER = 47;
private static final int MAX_MEGA_RANDOM_NUMBER = 27;
There are style concerns with having “magic numbers” in the body of a program, because they can be a hassle to change if they’re repeated through the code. If you’re using an IDE like Eclipse or Intellij IDEA, you can use the refactoring options to extract the constant for you.
Really takes me back. Specifically to Java, that is. I took a 20-week semester class in 2006. They had a 2nd class, but it wasn’t mandatory. Used the same book, though. Whopper of a book. I should revisit the first half and then finish it.