Week 4: Exercises

1. Hands of Cards

Consider this class:

enum Suit { Clubs, Diamonds, Hearts, Spades };

class Card {
    public int rank;    // 1 = Ace, 2 .. 10, 11 = Jack, 12 = Queen, 13 = King 
    public Suit suit;

    public Card(int rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

}

a) Add a method string describe() that returns a string description such as "7 of Diamonds" or "Jack of Hearts".

b) Write a class Deck with these members:

2. Polynomials

Design and implement a C# class Polynomial representing a polynomial of a single variable. Your class should include the following:

3. Double-Ended Queue

Write a class Deque implementing a double-ended queue of integers:

Your class should have these members:

All operations should run in O(1). In dequeueFirst() and dequeueLast(), you may assume that the queue is not empty.

4. Dictionary

Write a class SIDict that represents a dictionary from strings to integers. It should have the following members:

For example, your class could be used as follows:

SIDict d = new SIDict()
d["red"] = 4
d["blue"] = 5
d["red"] += 1
WriteLine(d["red"])

Use a linked list of key-value pairs.