Homework 4

Skills: designing, coding, and using your own classes, methods, dot-notation, loops

Classes: Color, Random, String, ArrayList, Graphics

For this assignment, you must work in teams of 2. You may decide how your team works together (we will discuss it in class too), but please explain it to me in a readme.txt file that will be part of the submission for this assignment. Answer any other questions in the write-up in the file also (unless it's already been turned in).

You will only submit 1 set of files; make sure to include both your names, Andrew IDs, and the honor statement in all your code, using the format given in class.

Due Week 7: Tuesday by 8pm.
Note: Associated written assignments will be assigned and due before the final due-date.

Important: for any graphical classes that need to draw make sure you use this exact signature:

    public void paintComponent(Graphics g)

This method should execute all the code to draw in g, including calling any other methods (they will also need to take g as an argument).

Both LineArt.java (HW2) and ConnectTheDots.java (HW3) show the basic idea.

Reminder: for any Helper class, make sure your instance variables are private and that you write toString()!

Written exercise, due Friday by the end of lab (so this is what you should work on during the Week 4 lab). Submit your team's answers via Canvas no later than the end of the day on Friday of Week 4.

  1. How does your team plan to work together?
  2. Which classes are programs rather than Helper classes? List them by name and the assignment number.
  3. Which classes are graphical? List them by name and the assignment number.
  4. Which classes are NOT graphical? List them by name and the assignment number.
  5. For the SecretString class, what should toString() return?
  6. For program #2 below, complete the following:
    • Perform the nouns analysis. What would you name your class? What are the instance variables and their data types?
    • Suppose that based on your verbs analysis, you have decided to write a method called change() that causes the light to change. Write the algorithm for this method. Specify what the method's signature would be and how it would properly change the light.
    • Should this class have any set-methods? Explain why or why not.

Write the following code:

  1. Design and code a class called GraphicalCircle that represents a circle. A circle has a center point, a diameter, and a color. You will use this class in your RunLight program (below), but use this program: TestCircle.java to test your basic circle class (and you can copy it for any other graphical shapes that you code).

  2. Design and code a class that that that represents a Kigali (or American) traffic light. It has 3 circles, one for each of the traffic light colors (red, yellow, and green). It has 3 true/false attributes that represent if the red light is on or off, if the yellow light is on or off, and if the green light is on or off.

    Note: make sure you get your basic design of the attributes reviewed.

    The traffic light starts out indicating stop. When it changes, it goes to go; from go, it goes to caution. After caution it returns to stop.

    When drawing each of the lights off, the following colors work nicely:

    new Color(187, 0, 0)   // red off
    new Color(187, 187, 0) // yellow off
    new Color(0, 100, 0))  // green off
    

    The size of your traffic light should be based on the width and height of the drawing area - have a look at your LineArt.java from HW2. Why is this calculation done in paintComponent()?

    Code your class to finish this program: RunLight. It will use your TrafficLight class to show the light, let the user click on it to have it change state, and then it will redraw your TrafficLight.

  3. Design a class called SecretString. This SecretString consists of the secret phrase and the corresponding string used to display the phrase. The string used to display the phrase is as long as the phrase and starts out as all underscores.

    For instance, if the secret phrase is Java is Great!, the display string would be ____ __ _____!

    The SecretString class can check if a certain string matches the secret phrase.

    The SecretString class has the ability to reveal letters - you can give it a set of letters (as a string), and it will "uncover" each one that matches; it will also tell you how many letters it uncovered.

    • For instance, if I asked the SecretString object to uncover "a", the result to display would become this: _a_a __ ___a_! and it would indicate that it found 3 letters.

    • If on the other hand, if I asked the SecretString object to uncover "aj", the result to display would become this: Ja_a __ ___a_! and it would indicate that it found 4 letters.

    • If I asked the SecretString object to show "a" again, what would the result be?

    • You MUST use the algorithm covered in class to uncover the letters.

  4. Design a class called PhraseList. This class stores a list of phrases. It can read in the data from a datafile - it should treat each line as a phrase and add it to the list. Other phrases can be manually added to the the list too. Your PhraseList class can return a specific phrase from the list - for instance asking for the first phrase (1) will return the 0th elemnt of the list. Your PhraseList class can also return a random phrase from the list. The phrase list can be cleared if the programer wants to get rid of all existing phrases.

    Re-read CH 7.16 in your book and use an ArrayList to store the phrases inside the PhraseList class. Please read, but do not turn-in, the version shown in your book that uses the inferred element type syntax.

  5. Once you have PhraseList and SecretString coded, use them to write an application called GuessWord. It should read the file passed in as a command-line argument to get the phrase list. After reading in the file, it should add one more hard-coded phrase, and then play the game; each time the game is played, the phrase is randomly selected from the PhraseList object. The selected phrase is then used as the SecretString in your main().

    Below is a sample dialog for the game (user input is shown as bold):

    ***********************************************
    Welcome to Cathy's fabulous word-guessing game!
    ***********************************************
    
    Would you like to play? yes
    
    ---- -- -----!
    Guess the phrase: Hello
    
    No, that's not it.
    
    Guess a letter: a
    
    There are 3 "a"s
    
    _a_a __ ___a_!
    Guess the phrase: Hello World
    
    No, that's not it.
    
    Guess a letter: j
    
    There is 1 "j"
    
    Ja_a __ ___a_!
    Guess the phrase: Java World
    
    No, that's not it.
    
    Guess a letter: x
    
    There are 0 "x"s
    
    Ja_a __ ___a_!
    Guess the phrase: java is great
    
    **Yes, that's it! You guessed it in  4 tries!
    
    ***********************************************
    Would you like to play again? yep
    
    _ ____ __ ____.
    
    Guess the phrase: java is great
    
    No, that's not it.
    
    Guess a letter: e
    
    There are 2 "e"s
    _ ___e __ ___e.
    
    Guess the phrase: i love to code!
    
    **Yes, that's it! You guessed it in  2 tries!
    
    ***********************************************
    Would you like to play again? no
    
    ***********************************************
    Thanks for playing Cathy's word-guessing game!
    ***********************************************
    

    Note: You must write a test plan for GuessWord submit it on Canvas. You should also have it reviewed by a TA during office hours. Make sure you think about error tests.

  6. Code and design 2 classes and a write a program called RunElevator based on the following description:

    All elevators should start out on the bottom floor. The elevator for your program will have a maximum of 20 floors. Your program should prompt the user for the floor they would like the elevator to go to, then move the elevator to this floor.

    When the elevator moves, it must first close the door and print the message shown below. As the elevator goes passed a floor, it should print a message that says "Going up to floor N" or "Going down to floor N". When it reaches the destination floor, it should print the message "Opening door at floor N". Your program should continue to prompt the user for the floor and go there until they just press enter, then the program should end.

    Your output should look like this (user input is shown in bold):

        The elevator is on floor 1.
    
        Please enter the new floor (Enter to quit): 4
    
        Closing door.
        Going up to floor 2.
        Going up to floor 3.
        Opening door at floor 4.
    
        The elevator is on floor 4.
    
        Please enter the new floor (Enter to quit): 1
    
        Closing door.
        Going down to floor 3.
        Going down to floor 2.
        Opening door at floor 1.
    
        The elevator is on floor 1.
        Please enter the new floor (Enter to quit): -3
        Invalid floor!
    
        The elevator is on floor 1.
        Please enter the new floor (Enter to quit): 1
        Closing door.
        Opening door at floor 1.
    
        The elevator is on floor 1.
        Please enter the new floor (Enter to quit): 
    
        Bye!
    
    Make sure you test it well!

Make sure your names, Andrew IDs, and the Academic Integrity statement are in all of your submitted code.