Problem Set 3 for CS230 Data Structures Course, Assignments of Data Structures and Algorithms

This handout provides the problem set 3 for the cs230 data structures course at wellesley college. It includes implementing enumerations, card abstractions, and vector methods. The problem set covers the implementation of a new card class based on abby's idea, which reduces the space per card instance from two variables to one, and the implementation of five methods that use a vector representation of sets.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-vl9
koofers-user-vl9 🇺🇸

4

(1)

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS230 Data Structures Handout # 7
Prof. Lyn Turbak Tuesday, September 24
Wellesley College
Problem Set 3
Due: Friday, September 27
Overview: In this problem set, you will get experience with implementing enumerations, card
abstractions, and vectors methods.
Download: To begin this assignment, you should download a copy of the directory ~cs230/download/ps3.
In your local copy of this directory, you will:
create a file named MyStringWords.java or MyFileWords.java for Problem 1;
create a file named Cards/MyCard.java for Problem 2;
edit the file named VectorSet/VectorSet.java for Problem 3.
Submission:
For Problem 1, your hardcopy submission should be your final version of either MyStringWords.java.
or MyFileWords.java.
For Problem 2, your hardcopy submission should be your final version of Cards/MyCard.java.
For Problem 3, your hardcopy submission should be your final version of VectorSet/VectorSet.java.
Your softcopy submission for this problem should be the entire ps3 directory.
Remember to include a signed cover sheet (found at the end of this problem set description) at the
beginning of your hardcopy submission.
Problem 1 [30]: Enumerations
Do Problem 3 from Problem Set 2, which is now worth 30 points in the context of PS3. You
should put your MyStringWords.java or MyFileWords.java file in the ps3 directory.
Problem 2 [20]: Card Abstraction
The Contendo game company is attempting to implement a number of card games in Java on a
hand-held computer with a small amount of memory. Because memory is such a limited resource,
they are looking for any way they can to reduce the amount of memory their program uses. They
hire Abby Stracksen, one of the world’s top data abstraction experts, to improve the memory usage
of their implementation.
One of the files they show to Abby is their implementation of the Card abstraction, which is
shown in Appendix A. You should study this appendix now.
Abby notes that every Card instance requires space for two variables: one to hold a pointer to
the card’s value, and another to hold a pointer to the card’s suit. Abby observes that the amount of
space per Card instance can be reduced from two variables to one by using a single integer variable
whose contents ranges between 0 and 51 to encode which of the 52 cards in a deck is denoted by
the card. In particular, she settles on the following correspondence between integers and cards:
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Problem Set 3 for CS230 Data Structures Course and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS230 Data Structures Handout # 7 Prof. Lyn Turbak Tuesday, September 24 Wellesley College

Problem Set 3

Due: Friday, September 27

Overview: In this problem set, you will get experience with implementing enumerations, card abstractions, and vectors methods.

Download: To begin this assignment, you should download a copy of the directory ~cs230/download/ps3. In your local copy of this directory, you will:

  • create a file named MyStringWords.java or MyFileWords.java for Problem 1;
  • create a file named Cards/MyCard.java for Problem 2;
  • edit the file named VectorSet/VectorSet.java for Problem 3.

Submission:

  • For Problem 1, your hardcopy submission should be your final version of either MyStringWords.java. or MyFileWords.java.
  • For Problem 2, your hardcopy submission should be your final version of Cards/MyCard.java.
  • For Problem 3, your hardcopy submission should be your final version of VectorSet/VectorSet.java.

Your softcopy submission for this problem should be the entire ps3 directory. Remember to include a signed cover sheet (found at the end of this problem set description) at the beginning of your hardcopy submission.

Problem 1 [30]: Enumerations Do Problem 3 from Problem Set 2, which is now worth 30 points in the context of PS3. You should put your MyStringWords.java or MyFileWords.java file in the ps3 directory.

Problem 2 [20]: Card Abstraction The Contendo game company is attempting to implement a number of card games in Java on a hand-held computer with a small amount of memory. Because memory is such a limited resource, they are looking for any way they can to reduce the amount of memory their program uses. They hire Abby Stracksen, one of the world’s top data abstraction experts, to improve the memory usage of their implementation. One of the files they show to Abby is their implementation of the Card abstraction, which is shown in Appendix A. You should study this appendix now. Abby notes that every Card instance requires space for two variables: one to hold a pointer to the card’s value, and another to hold a pointer to the card’s suit. Abby observes that the amount of space per Card instance can be reduced from two variables to one by using a single integer variable whose contents ranges between 0 and 51 to encode which of the 52 cards in a deck is denoted by the card. In particular, she settles on the following correspondence between integers and cards:

0 : 2C

1 : 2D

2 : 2H

3 : 2S

4 : 3C

5 : 3D

6 : 3H

7 : 3S

44 : KC

45 : KD

46 : KH

47 : KS

48 : AC

49 : AD

50 : AH

51 : AS

In this problem, you will use Abby’s idea as the basis for a new implementation of the Card class that will replace the one shown above. To begin this problem, examine the ps3/Cards directory, which contains implementations of all of the card abstractions discussed in class. Before you make any modifications, do the following:

  • Study the main() method and the other class methods within the Card class in the file Card.java. These methods are used to test if the implementation of the Card class is correct. Invoke the main() method of the Card class by executing java Card. Study the output and make sure you understand why it is the correct output. You will be using this main() method later in this problem to test your changes to the Card class.
  • Make a copy of Card.java (call it Card-orig.java say) so that you will always have the original version to refer to if you should need it.
  • Make a copy of Card.java called MyCard.java defining the Card class that you will modify for this problem.

You should begin by removing the instance variables value and suit from the Card class and replacing them by a single integer instance variable named num:

private int num; // New instance var for Card. Should be in range 0--51.

Next, modify the definitions of the constructor method and all the instance methods (but not the class methods) of the Card class so that they work correctly with the new num instance variable in place of the old ones. You should not change the interfaces to any of these methods; you should just change their implementations! What you are doing is changing the internals of the Card black-box abstraction in such a way that it still appears to work as it did before from the outside world. This is the essence of data abstraction. For instance, the Card constructor should still take two arguments: a Value v and a Suit s. Your job is to figure out how to generate a number between 0 and 51 that corresponds to the card that has Value v and Suit s. Similarly the value() and suit() methods should return the value and suit of the card, just as before.

a. [5]

public static boolean member (Comparable x, Vector v); Returns true if x is a member of the set represented by v, and false otherwise.

b. [5]

public static void union (Vector v1, Vector v2); Destructively modifies the set represented by v1 to be a representation of the set that is the set union of the elements from the sets represented by v1 and v2 – i.e., a set that contains all the elements that appear in either the set represented by v1 or the set represented by v2.

c. [10]

public static void intersection (Vector v1, Vector v2); Destructively modifies the set represented by v1 to be a representation of the set that is the set intersection of the elements from the sets represented by v1 and v2 – i.e., a set that contains all the elements that appear in both the set represented by v1 and the the set represented by v2.

d. [10]

public static void difference (Vector v1, Vector v2); Destructively modifies the set represented by v1 to be a representation of the set that is the set difference of the sets represented by v1 and v2 – i.e., a set that contains all the elements of the set represented by v1 that do not appear in the set represented by v2.

e. [20]

public static void canonicalize (Vector v); Destructively modifies v so that it represents the same set as the set initially represented by v but additionally guarantees that the vector representation (1) contains no duplicates (2) has elements arranged in sorted order from low to high.

You should flesh out the bodies of the above methods in the file ps3/VectorSet/VectorSet.java. You should test your methods using the main method of the VectorSetTest class which has been provided for you in the file ps3/VectorSetTest.java (see Fig. 1). This class provides a simple interpreter that executes vector set commands from a file on an initially empty vector v. The format of the testing file is described in the comments at the beginning of the VectorSetTest class. An example of such a testing file is the test.txt file presented in Fig. 2. The result of executing java VectorSetTest test.txt is shown in Fig. 3. Note that because the same set can be reprented by many different vectors, the output shown in Fig. 3 is only one example of a correct output. Any output in which each line beginning v = is a vector denoting the same mathematical set (i.e., has the same elements, but perhaps in a different order and with a different number of occurrences of some elements) would also be correct.

Notes:

  • The test.txt file presented here is only an example. You should develop and use your own testing file(s).
  • You are welcome to define any auxiliary methods you find helpful for this problem in VectorSet.java.
  • You may find it helpful to use enumerations to write some of the methods.
  • Beware of the gotchas you were warned about in class. They are waiting to bite you in some of the methods!

A Implementation of the Card class

The original implementation of the Card class (before the modifications in Problem 2) is shown in Figs. 4–6.

union [b,a,c] member a member b member c member d union [b,f,e,g,c,e,d,h] intersection [c,g,a,i,f,h,a,e] difference [i,f,j,e,a,f] canonicalize union [c,g,k,c,f,g,b] canonicalize

Figure 2: The contents of test.txt, one example of a file to test vector sets.

java VectorSetTest test.txt v = [] union(v, [b,a,c]); v = [b, a, c] member (a, v) = true v = [b, a, c] member (b, v) = true v = [b, a, c] member (c, v) = true v = [b, a, c] member (d, v) = false v = [b, a, c] union(v, [b,f,e,g,c,e,d,h]); v = [b, a, c, b, f, e, g, c, e, d, h] intersection(v, [c,g,a,i,f,h,a,e]); v = [a, c, f, e, g, c, e, h] difference(v, [i,f,j,e,a,f]); v = [c, g, c, h] canonicalize(v); v = [c, g, h] union(v, [c,g,k,c,f,g,b]); v = [c, g, h, c, g, k, c, f, g, b] canonicalize(v); v = [b, c, f, g, h, k]

Figure 3: One possible output of java VectorSetTest test.txt. Any output in which each line beginning v = contains the same elements, with possible reorderings and different number of occurrences of the same elements, would also be correct.

public class Card {

// Instance Variables: private Value value; // The value of this card. private Suit suit; // The suit of this card.

// Constructor Method: public Card (Value v, Suit s) { // Returns a new card with the given value and suit. value = v; suit = s; } // Instance Methods:

public Value value() { // Returns the value of this card. return value; }

public Suit suit() { // Returns the suit of this card. return suit; }

public boolean equals (Card c) { // Returns true if c has the same value and suit as this card; false otherwise. return ((value.equals(c.value)) && (suit.equals(c.suit))); }

public int compare (Card c) { // Returns -1 if this card precedes c in the card ordering; // 0 if this card is equal to c in the card ordering; // and 1 if this card follows c in the card ordering. if (value.equals(c.value)) { return suit.compare(c.suit); } else { return value.compare(c.value); } }

public String toString() { // Returns a string representation of this card. return value.toString() + suit.toString(); }

public static Card fromString (String s) { if (s.length() != 2) { throw new RuntimeException ("Card.fromString: unrecognized string -- " + s); } else { return new Card (Value.fromString((new Character(s.charAt(0))).toString()), Suit.fromString((new Character(s.charAt(1))).toString())); } }

public static Card intToCard (int n) { return new Card(Value.intToValue((n / 4) + 2), Suit.intToSuit(n % 4)); }

Figure 4: The Card class (Part 1).

public static String formatInt (int n, int val) { if (n == 0) { return " = "; } else if (n < 0) { return " < "; } else { return " > "; } }

public static String formatBool (boolean b, int val) { if (b) { return " + "; } else { return " - "; } } }

Figure 6: The Card class (Part 3).

Problem Set Header Page Please make this the first page of your hardcopy submission.

CS230 Problem Set 3

Due Friday September 27

Name:

Date & Time Submitted:

Collaborators (anyone you worked with on the problem set):

By signing below, I attest that I have followed the collaboration policy as

specified in the Course Information handout.

Signature:

In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.

Part Time Score

General Reading

Problem 1 [30]

Problem 2 [20]

Problem 3 [50]

Total