CSci 2101 Lab 6. Iterators, interfaces.

Tuesday March 7, due Monday March 20 at 11:59pm Work in pairs.

Part 1, 10 points: make linked list iterable

You may use your own implementation of linked list, or use the starting code that we wrote in class (linked here) since you only need add method for this work.

Change the class declaration of OurList interface so that it extends Iterable: OurList<E> extends Iterable<E>. Then add the required iterator() method to the list, and then the iterator as an inner class (follow the examples we looked at in class).
In the testing class add a "for-each" loop to print all the elements of our linked list. Test your iterator carefully with multiple examples.

Part 2, 8 points: make a deck iterable

You are given the following code for Card and Deck classes (note that they are different from what you have written in class):


import java.util.ArrayList;
import java.util.Collections;

/**
 * This class represents a deck
 *
 */
public class Deck {
	
	private ArrayList<Card> cards = new ArrayList<Card>();
	
	/**
	 * Deck constructor
	 * @param cards
	 */
	public Deck(ArrayList<Card> cards){
		for (Card c : cards){
			this.cards.add(c);
		}
	}
	
	/**
	 * @return true if the deck has at least one card, and false otherwise 
	 */
	public boolean hasCards(){
		return (cards.size() > 0);
	}
	
	/**
	 * put a Card object on the bottom
	 * @param card
	 */
	public void takeCard(Card card){
		cards.add(0, card);
	}
	

	/**
	 * Removes the last element of deck and returns it. Also changes hasCards
	 * to false if deck is now empty
	 * @return deck
	 */
	public Card playCard() {		
		return cards.remove(cards.size() - 1);
	}
	
	/**
	 * returns the size of the deck
	 * @return deck.size()
	 */
	public int getSize() {
		return cards.size();
	}
	
	public void shuffle() {
		Collections.shuffle(cards);
	}
	
}

import java.util.ArrayList;

/**
 * 
 * This class represents a card
 *
 */
public class Card {

	final private String suit;
	final private String value;
	private int numericValue = 0;
	final static private String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
	final static private String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
	
	/**
	 * Card constructor
	 * @param suit
	 * @param value
	 */
	public Card(String suit, String value){
		this.suit = suit;
		this.value = value;
		for (int i = 0; i < 13; i++) {
			if (values[i].equals(value)) {
				numericValue = i + 2;			
			}
		}
	}
	
	
	public static Deck getDeck() {
		ArrayList<Card> deck = new ArrayList<Card>();
		for (String suit: suits) {
			for (String value: values) {
				deck.add(new Card(suit, value));
			}
		}
		return new Deck(deck);
	}
	
	/**
	 * getter for suit
	 * @return suit
	 */
	public String getSuit(){
		return suit;
	}
	
	/**
	 * getter for value
	 * @return value
	 */
	public String getValue(){
		return value;
	}
	
	/**
	 * getter for numericValue
	 * @return numericValue
	 */
	public int getNumericValue(){
		return numericValue;
	}
	
	/**
	 * override toString() method
	 */
	public String toString(){
		return value + " of " + suit;
	}
	
	/**
	 * Compare the numeric values of this Card and the parameter Card
	 * @param that
	 * @return an int value
	 */
	public int compareTo(Card that){
		return this.numericValue - that.numericValue;
	}
}

Make the Deck class implement Iterable<Card> interface and then add the required iterator to return the cards in the order they appear in the array list. Write a testing class to test it.

Part 3, 12 points: using Comparable interface for sorting

Another important Java interface is Comparable. It is used, among other things, for predefined Java sorting methods. For instance, if strings is an array list of strings, the call
Collections.sort(strings)
will sort them in "lexicographic" (alphabetic, with upper case letters before lower case) order.

Write a test class to sort ArrayLists of strings, integers, and doubles using Collections.sort.

In order for this approach to work the class T to be sorted must implement Comparable<T> (so String class implements Comparable<String>, Integer class implements Comparable<Integer>, etc.).

Make the Card class given above implement Comparable<Card> and use it in sorting an ArrayList of Cards (the Card class already has a compareTo method).
You can use Card.getDeck() to get a full deck, shuffle it using the Deck shuffle method, and then get all the cards from the deck and put them into an array list. After that you need to pass the array list to Collections.sort and check that it is sorted.

Change its compareTo method to also take suits into account, in the order: Spades, Hearts, Diamonds, Clubs.

How to submit:

Send me all your code (CC your group).


CSci 2101 course web site.