Homework 7

Skills: Interfaces, Abstract Classes, Polymorphism, Class Design, Serialization, Collections, Algorithms, References

Classes: Reading documentation, String, ArrayList, Graphics, HashMap, LinkedList, Stack, StringBuffer

Due Week 11: Thursday by 10pm.

  1. Written: In a file called BallSwap.pdf write an algorithm to solve the ball-swap puzzle described below.

    Re-read Chater 4 in your book to review algorithms and pseudocode.

    You will describe your algorithm twice:

    1. Section A: in an outline format.
    2. Section B: in pseudocode.

    Ball-Swap Puzzle:

    In this puzzle there are N red and blue balls where N is an even number and N/2 is the number of red balls and N/2 is the number of blue balls. The balls are arranged as follows:

    The balls start out in cells as shown above, separated by one blank cell in the center. The goal of this puzzle is to swap the positions of the blue balls and the red balls. To swap the red and blue balls there are two kinds of moves you can make. You can move a ball to an adjacent empty square (forwards or backwards), or you can jump a single adjacent ball into an empty square (forwards or backwards).

    Do NOT write code, and do not write your algorithms with class-design in mind. You should abstract away unnecessary details and focus on the essence of the algorithm. If you have it correct, someone should be able to follow your algorithm with any number of balls and solve it.


    Note: a 5-point penalty will be given for not working with Lucy-Anna and Jessica.

    You must have a first-draft of your outline version of the algorithm and make an appointment during the next week for review. Dates are posted in the sign-up sheet. If they recommend a follow-up, you must make an appointment for a 2nd review before the end of Week 10.

    Sign up here: Sign-up Sheet for Ball Swap Review

    Put your name in the time-slot you want; make sure there are at most 2 students per 15-minute time-slot.


  2. Design and code a program for creating and playing MadLibs. MadLibs is a game where specific words in a short story are replaced with blanks. One player prompts the other for the missing words, based on a type of speech, writes in the words, then reads the story aloud.

    Below are a couple of examples, pair up and try them:

    You program will allow a user to

    1. Create a MadLib.
    2. Play a MadLib.
    3. Generate a printable MabLib.

    Step 1: Create a MadLib

    Here is an example

    Original Story (in a text file)

    MadLib: Sick Note

    Hello [boss], this is [person's name].

    I think I've got that [stomach] virus that is going around [campus]. I'm [sorry], but I can't come into [school] today. My [doctor] says I should stay at [home] and [rest] for up to [2] weeks.

    I will try to stop in by [2pm] tomorrow, but if not, I'll [sleep] a bit and get some [work] done at [home]. If you need me, you can reach me at [MadEmail]. I hope I will be [well] enough to come in by [tomorrow]. Thanks!

    MadLib Story that has been set up (stored in your program as an object of class MadStory)

    MadLib: Sick Note

    Hello [person at work], this is [person's name].

    I think I've got that [body part] virus that is going around [a place]. I'm [a feeling], but I can't come into [place] today. My [medical person] says I should stay at [a place] and [verb] for up to [a number] weeks.

    I will try to stop in by [time] tomorrow, but if not, I'll [verb] and get some [an activity] done at [a place]. If you need me, you can reach me at [silly email]. I hope I will be [adjective] enough to come in by [time in the future]. Thanks!

    Phase 1: Get Started:

    1. Type in this code for testing your MadLib Dictionary MadDictionaryTest and make the code changes specified in the comments.

      Make sure to comment out the part that reads in from the datafile, run it once, then uncomment it out - your MadDictionary should persist.

      After that is working, investigate and add the code to determine if the file exists before you read it in. (Once you determine the class and method to use, submit it on Canvas - this is due by Tuesday morning.)

      Test this by removing the datafile and running it - your code should not crash!

    2. Start on your MadLibs program:

      1. Create an object of class MadDictionary.
      2. Create a LinkedList object that holds Strings to store your story (please call this variable story).
      3. Read in the MadDictionary (if the file exists).
      4. Read in a story by prompting the user for the file name.
      5. For each word in the input that is surrounded by [], check if the word is in the MadDictionary. If not, add it (like the MadDictionaryTest program).

        To do this, follow this algorithm:

        • Read in a line of data.
        • Use split to put it into an array of strings called words.
        • Loop through the array looking for words or phrases in square brackets [].
        • If the word or phrase is not in your MadDictionary, add it (prompt the user like the MadDictionaryTest program).
        • Next, convert your String array to a List of Strings, using the code:
          List wordList = Arrays.asList(words);
        • Then add wordList to your story.

      6. When you are all done, print story - all the words should be there.

    Phase 2: Write the Classes and Final Program

    Step 2: Play a MadLib

    • First, the game must be setup, then you play by prompting the user with each MadPrompt and replace the MadWord in the story with what the user entered. When finished, print it.

    • See the comments in the MadStory code for more details.

    Step 3: Print a MadLib

    Step 4: Finish MadLibs

      Complete the final version of main(): *** Final MadLibs program Turn this one in! ***