15-121 SPRING 2010 [CORTINA]

HOMEWORK 4 - due Thursday, February 11 - EXTENDED

WRITTEN PROBLEMS (10 points)

  1. (1 pt) Suppose an ArrayList is implemented with an array, and the array doubles in size whenever the array fills up. Explain why adding an element to the end of the arraylist is considered a constant time operation in general.

    HINT: Consider an arraylist that has a full array with n elements. Describe the amount of work done to insert the next n elements and show that this averages to a constant amount of work over time.

  2. (3 pts) Suppose we have an ArrayList of n Integer objects, sorted in increasing order. Consider the following algorithms for removing a particular Integer value from that ArrayList. It is already known that this value is in the ArrayList. Identify the best and worst case running times (using Big O) for each algorithm. Explain each of your answers.

    Algorithm I: Perform a linear search to find the value, starting from the BEGINNING of the list. When the value is found, remove it by calling the ArrayList's remove(index) method.

    Algorithm II: Perform a linear search to find the value, starting from the END of the list. When the value is found, remove it by calling the ArrayList's remove(index) method.

    Algorithm III: Perform a binary search to find the value. When the value is found, remove it by calling the ArrayList's remove(index) method.

  3. (1 pt) Write a method that reverses the order of elements in an ArrayList<E> without creating a new ArrayList. You may only use the methods remove, add, and size.

  4. (1 pt) A singly linked list of integers is shown below.

          _      -------    -------     -------    --------
         | |    |   |   |  |   |   |   |   |   |  |   |    |
    head |----->| 8 | ---->| 9 |  ---->| 1 | ---->| 3 |null|
         |_|    |   |   |  |   |   |   |   |   |  |   |    |
                 -------    -------     -------    --------
    

    1. What does the following code fragment output for the list above? What does it output in general? What must be true about this list to avoid a runtime error?

      Node p = head;
      while (p.next != null) {
          p = p.next;
      }
      System.out.println(p.data);
      

    2. The following code outputs the sum of all of the values in the linked list. Explain the unintended side effect that occurs when this code is run. Then rewrite the code to avoid this side effect.

      int s = 0;
      while (head != null) {
          s += head.data;
          head = head.next;
      }
      System.out.println(s);
      

  5. (2 pts) Consider the SinglyLinkedList class discussed in lecture. Write a method addToHead that takes an element of type E and attaches it at the head of the linked list only if it is not already in the list. Do not use any linked list methods discussed in lecture. You may assume that the class E implements its own equals method. If the list is empty, this element becomes the only element of the list. This method should return true if the list changes or false otherwise. In addition, give the best-case and worst-case runtime complexity in big-O notation if the list has n elements. Explain your answers.

  6. (2 pts) Consider the following method for the SinglyLinkedList class that works on a linked list.

    public E mystery() {
    	Node<E> p = head;
    	Node<E> q = head.next;
    	while (q != null) {
    		p = p.next;
    		q = q.next.next;
    	}
    	return p.data;
    }
    

    1. What does this method do if there are an odd number of nodes? Explain by tracing this code with a list with 9 nodes.

    2. What happens with this code if there are an even number of nodes? Explain with an example. Modify the method to deal with this situation in a reasonable manner.

    3. What is the runtime complexity of this method in big-O notation if the list has n elements?

PROGRAMMING PROJECT (10 points)

Card images from http://www.jfitz.com/cards/

In this assignment, you will complete a program to play the game Aces Up.
SPECIAL NOTE: This is Tom's version of Aces Up!!! The rules are not exactly the same as the rules you will find online.

THERE WILL BE A ONE-TIME-ONLY DEMO OF THIS GAME IN CLASS ON THURS 2/4. DON'T MISS IT!

Game Rules

The goal is to discard as many cards from four lists of cards as possible, leaving only Aces at the end.

A standard 52-card deck is shuffled and the first four cards are dealt face-up to create four one-card lists. On each turn, the player can discard a card, move a card or deal four cards.

DISCARD: The last card on a list can be discarded if there is another card of the same suit and a lower rank at the end of another list. (Ranks, from low to high, are A,2,3,4,5,6,7,8,9,10,J,Q,K.) Whenever a card is discarded, the player earns points based on the rank of the card. Number cards are worth their value in points (e.g. a 9 of Hearts is worth 9 points). Jacks are worth 11 points, Queens are worth 12 points, and Kings are worth 13 points. Aces are not worth any points since they cannot be discarded in this game.

MOVE: The last card on a list can be moved to any empty list.

DEAL 4: If a player chooses to deal four more cards from the deck, either by choice or because no cards can be discarded or moved, one card is added from the deck to the end of each list (including any empty lists).

The game ends when all cards have been discarded except the Aces or when no further play is possible.

Assignment

Download a copy of the AcesUp.zip project file.

Complete the supplied Java program to play a correct game of Aces Up.

The file contains four classes:

Programming Requirements

In the GameController class, you must use an ArrayList to store a standard deck of 52 playing cards. The arraylist for the deck of cards uses generics for this list with a type of PlayingCard as its base type. The GameController class uses an ArrayList of PlayingCard for each of the four lists of cards and for the discard pile as well. (NOTE: When you compile your code, you will get a warning message since the compiler will complain about creating an array of 4 ArrayLists of PlayingCard. This is ok for this project.)

You can shuffle the deck of cards by using the Collections.shuffle method. Its parameter should be a reference to the array list that holds your deck of cards. When you move a card from one list to another, do this in the most efficient manner possible. You may add additional private helper methods as you wish to simplify and organize your program code.

Implementation Note: The GUI has a button for DISCARD and MOVE for each pile and a button to DEAL 4 more cards. If any of these operations are invalid for the current state of the game, nothing should change in the game. Clicking NEW GAME always starts a new game.

DO NOT CHANGE ANY CODE ALREADY GIVEN TO YOU. YOUR ADDITIONAL CODE SHOULD WORK WITH THE CODE PROVIDED TO YOU.

Debugging/Testing

In your code, you must print the current cards in the deck and the four piles to the console window after each play is made. This will be helpful to you and to the CA for testing purposes.

Here is a sample console output for the game configuration above:

DECK: [4H, 11C, 2H, 10S, 12S, 13D, 8C, 11D, 9S, 3D, 6H, 12C, 6S, ...etc... 
LIST0: [5H]
LIST1: [7C, 4D]
LIST2: [2S, 13H, 1S]
LIST3: [1H, 7D]

HINT: The ArrayList class has its own toString method! Use it!

Eclipse vs. Command-Line execution - Card images

The project you downloaded contains a folder named images that contains pictures of all of the playing cards used in the game. If you use Eclipse to run this project, the folder is in its correct place (the top level of the project). If you use the command-line to compile and run the program, move this folder to the src folder where the Java source code is.

Documentation & Style

Write Javadoc comments to match the Javadoc comments given above (as best as you can). Add additional comments in your methods to explain your code where necessary. Use appropriate variable names for self-documentation and indent properly.

Additionally, write a comment for the actionPerformed method in the GamePanel class that explains in detail how this method is invoked. (Don't just say "it's invoked when an action is performed" or "it's invoked when a button is pressed". Explain why it runs when a button is pressed: how is the code set up so this happens?)

Hand-In Instructions & Late Submissions

Your submitted zip file should include the complete Java program (all Java files and the images).

See the course website for instructions on how to hand in your program and the policy for late submissions.