CPSC 111 2005/6 Winter Term 2, Assignment 1: Lotteries and Magicians


Contents: Details | Part 1 | Part 2 | Deliverables

Details

Out: Tue 17 Jan 2006
Due: Tue Jan 31 2006, 5:00pm, both hard copy and electronic handin
Value: 4% of final grade (subject to instructor discretion)

Objectives:

Note: Each assignment (and usually each part of each assignment) will include reflection questions. You should include your answers to the reflection questions in a separate text file from your code named README.txt. Please do not submit your questions in any format other than plain text. (Note that neither Microsoft Word nor PDF is plain text! Whatever editor you use to write your source code should be usable to write a plain text README.txt file.) You should answer the reflection questions for all parts of an assignment in the same README.txt file.

Part 1: Debugging LotteryWinnings Code

In this part of the assignment you are given a Java program that contains both syntax and logic errors. Your task is to identify and correct each of these errors. You will also learn a useful trick for helping to debug your programs.

Start by downloading the source file LotteryWinnings.java. This source file contains a Java program that helps the fictitious lottery CPSC49 Prize organizers determine ticket cost.

Specification:

Your task in this part of the assignment is to hand in a corrected version of the source code.


Reflection Questions:

Be sure to answer these reflection questions about Part 1 after you finish the part. There are no right answers to these questions, and we don't expect more than a couple of sentences for each question. However, we will grade on your effort to answer thoughtfully.

  1. Which kind of error was harder to find and fix: syntax or logical errors? Why?
  2. Pick one logical error you found and propose a way that you could write code to help you avoid ever making that error in the first place. Would you rather use your technique or debug errors after they're made?
  3. OPTIONAL: What was especially interesting or frustrating about this part of the assignment? What could we have done to make it better for you?
Hints:

Part 2: Writing AmateurMagician Code

Specification:

In this part of the assignment, you will write your own Java program, AmateurMagician.java, impersonating a tongue-tied magician who doesn't have a very impressive act. The user picks a card, and the magician says what that card is. Eventually.

Here's an example session with the game:

Welcome to the Amateur Magician Card Tricks Night!
--------------------------------

Pick a card, any card, any card will do
No strings up my sleeve - well, maybe a few

Glease pive... no! 
Leasepay ivegay... that's not it!
Pl33s3 g1ve... ok, deep breath...
Pwease give... uh...
Please give

a card name (ace, one-nine, jack, queen, or king): three
a card suit (clubs, diamonds, hearts, spades): diamonds

You picked:
dhree of tiamonds... no!
hreetay of iamondsday... that's not it!
thr33 0f di@m0ndz... ok, deep breath...
dwee of diamonds... uh...

three of diamonds!

Whew. Play again?
(Note that the user's input is in green for clarity. You do not have to make the user's input green in your program!)

Here's a detailed list of tasks for your program:

  1. Print a banner identifying the game
  2. Print a brief introduction from the magician
  3. Print a prompt for the card name
  4. Read the card name
  5. Print a prompt for the card suit
  6. Read the card suit
  7. Print a message saying "You picked:"
  8. Print the Spoonerism version of the card designation
  9. Print the Pig Latin version of the card designation
  10. Print the LeetSpeak version of the card designation
  11. Print the Elmer Fudd version of the card designation
  12. Print the card designation correctly
  13. Sign off with a final message to the user

For the sanity of the markers, please make all your prompts and output look as similar as possible to the example above.

You must create the card designation by combining the name and suit, with the word "of" surrounded by spaces in between them. The rules for the four ways that the magician gets it wrong are:

Reflection Questions:

Be sure to answer these reflection questions about Part 2 after you finish the part. There are no right answers to these questions, and we don't expect more than a couple of sentences for each question. However, we will grade on your effort to answer throughtfully.

  1. Did you end up implementing and testing the assignment piece-by-piece or all-at-once? (You won't be penalized for either answer!) In either case, what were the biggest frustrations you encountered with your approach? Which approach (or combination) do you think you'll use next time?
  2. Which confused string rules were the hardest to implement? Why? Propose something (a method, library, or statement) that Java could provide you that would make that rule easier to implement.
  3. OPTIONAL: What was especially interesting or frustrating about this part of the assignment? What could we have done to make it better for you?

Hints:

Do not try to write the whole program in one go. Instead, try writing and testing it bit by bit. For example, you might follow these steps, ensuring at each step that you have a working program:

  1. Create a nearly-empty program shell for AmateurMagician.java, make sure you can compile and run it.
  2. You will probably find the Java object String to be very useful, make sure you are familiar with its methods replace and substring.
  3. You will probably find the Java object Scanner very useful, make sure you are familiar with its method next.
  4. Make your program just print the game's banner message, and then test your code!
  5. Print out the signoff message to the user and end the program, and then test your code!
  6. After the banner message section, ask the user for the card name, and then test your code!
  7. Read the card name, and then test your code! (You may want to print out the card name you read, just to verify that your code is working correctly.)
  8. Ask the user for the card suit, and then test your code!
  9. Read the card suit, and then test your code!
  10. You may want to break your name and suit strings up into new smaller strings. For instance, consider splitting a string into two new strings: one with just the first character and one with all the rest. Or splitting a string into two different new strings: one with everything up until the last character, and one with just the last character.
  11. You may want to combine your name and suit strings into a longer strong, with spaces and " of " in between them.
  12. Create and print out the correct version string. It's the last thing that should print out, but it's the easiest one to do, so add that code first, and then test your code!
  13. Create and print out a Spoonerism version string, and then test your code!
  14. Create and print out a Pig Latin version string, and then test your code!
  15. Create and print out an Elmer Fudd version string, and then test your code!
  16. Create and print out a LeetSpeak version string, and then test your code! This may be the hardest one, because one of the substitutions should only happen at the end of a word.
  17. Test the full program to make sure that you've implemented the complete specification.
  18. Make sure you're not redoing work unnecessarily. If you find yourself doing the same operation more than once consider creating temporary variables.
  19. Make sure your code is fully commented, and then test your code! We're not kidding. It's easy to accidentally mess something up when you think all you're doing is adding comments. (True, comments have no effect on the function of your code, but what about that accidental extra character you typed into an identifier name without noticing it?)
  20. Declare victory and hand in!
When you have difficulty making a piece work, write as small a program as possible that uses just that piece. That way, you can isolate the problems you're having in a small, simple piece of code.

Challenges:

Do you feel the need to do more? Here are a few (completely optional!) suggestions:


Deliverables


Last modified: Sun Apr 18 16:26:10 PDT 2010