15-110 FALL 2009 (CORTINA)

PROGRAM 2 - due Monday, September 14, 2009 by 11:59PM

Electronic handin will be available after 5PM on Thursday.

When a visitor enters the Pittsburgh Museum, they have to sign in at a computer terminal to print out an entry ticket. You are hired by Pittsburgh Museum to write the program to generate the ticket. The ticket will contain the visitor's name in all uppercase letters, a ticket number, a validation code and an expiration date for the ticket. A sample ticket is shown below.

******************************************** 
  ---- PITTSBURGH MUSEUM ENTRY TICKET ---- 
 
  LISA SIMPSON 
  Ticket Number: 5316-L-204-N
  Validation Code: BWL
  Expires:  FEB 2010
******************************************** 

Assignment

Using Eclipse, write a simple Java program in a project named Program2 that contains one class named TicketGenerator that reads in the name of the visitor from the keyboard and then outputs a valid ticket for the museum.

Program Requirements

Your TicketGenerator class must have static methods as described below.

  1. Write a static method named displayFirstNum that prints out a random four digit number that does not start with a 0.

  2. Write a static method named displaySecondNum that prints out a random three digit sequence made up of only even digits (including 0). HINT: Think about how to generate a random integer from the set {0,2,4,6,8} and do this three times.

  3. Write a static method named displayValidationCode that prints out a random three-letter sequence made up of only uppercase letters. Letters may be repeated in the sequence. In this method, define a string that contains the letters of the alphabet: ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ). Then generate 3 random integers between 0 and 25 (inclusive) and use these to get the three random letters.

  4. Write a static method named displayExpirationDate that prints out a date consisting of a three letter month abbreviation in uppercase chosen at random and the year 2010 (example: FEB 2010). In this method, define a string that contains the month names ( "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC" ). Then generate a random number between 1 and 12 for the month and use this number and the substring method of the String class to get the appropriate month name from the string above. For example, if the random number is 2, then you want substring(3,6) from the string above. Can you generalize this for any random number between 1 and 12? YOU DO NOT NEED TO USE THE if OR switch STATEMENTS IN YOUR SOLUTION.

  5. Write a main method that does the following:

    1. Prompt the user for his or her first name and last name, one at a time. The names may be entered in uppercase, lowercase or some combination of uppercase and lowercase. You may assume that the name will not include anything except letters. Use the Scanner class to read in the name.

    2. Display a valid ticket for the user, calling the static methods you wrote in steps 1-4 when appropriate.

      The ticket number is made up of the following in the order shown, separated by dashes:

      • A random four digit number that does not start with a 0
      • The first letter in the visitor's first name in uppercase
      • A random three digit sequence that contains only even digits (0 included)
      • The last letter in the visitor's last name in uppercase

  6. Be sure your code is written in acceptable Java style, including use of appropriate variable names, indentation, comments explaining each method, and use of appropriate naming conventions (e.g. variableName, ClassName, methodName() ).

Sample Output

Your program should work for any valid name, not just the one shown in the example. Your output should be formatted as shown in the following example. Follow the formatting in this example as closely as possible for maximum credit. User input is shown in italics.

Please enter your first name: 
Lisa
Please enter your last name:
siMPson

********************************************
  ---- PITTSBURGH MUSEUM ENTRY TICKET ----

  LISA SIMPSON
  Ticket Number: 5316-L-204-N
  Validation Code: BWL
  Expires:  FEB 2010
********************************************

Compiling and Testing

Compile and test that your program runs correctly. If your program does not compile and run, you will not receive full credit for your assignment.

Make sure you add a comment at the beginning of your program with your name, andrew id, and section.

Hand-in Instructions

See the course website for instructions on how to hand in your program. Please zip your project folder that is created in Eclipse. This makes it easier for us to grade your work.

Remember that the work you submit must be your own. Also, late hand-ins are not accepted. Please plan ahead and submit early to avoid server overload at the deadline. The deadline is based on the server's clock, not your clock. Please do not email your code to your instructor or course assistant as your official hand-in; these will not be graded.