Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Corrections for SetHand Assignment in CS230, Assignments of Data Structures and Algorithms

Corrections for the sethand assignment in cs230, including typographical errors in method specifications and incorrect orderings. The assignment involves implementing various methods for the sethand class, which models hands consisting of three cards in the game of set.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-o8m
koofers-user-o8m 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS230 Data Structures Handout # 8
Prof. Lyn Turbak February 1, 2004
Wellesley College Revised February 7, 2004
Problem Set 1
Due: 6pm Friday, February 6
Revisions:
Feb 7 : (1) The 2ghs in the SetHand toString specification should have been 2hgs; (2) The 2gft
and 2ghs in the SetHand compareTo specification should have been 2fgt and 2hgs (3) The previous
ordering in Fig. 1 was incorrect: ’h’ was consistently shown before ’f’, when it should have been
the other way around.
Overview:
The purpose of this assignment is to give you practice writing some simple Java methods using
Emacs and Java on the Linux workstations. Since learning a new programming environment takes
time, it is strongly recommended that you (1) start early and (2) work with a partner. Allocate
time over several days to work on the problems; it is very unwise to start the assignment only a
day or two before it is due. Don’t hesitate to ask for help if you hit a roadblock.
Reading:
Handouts #1 #7;
Read Chapters 1–7 of Downey’s online Java book (accessible from the Resource Links section
of the CS230 home page).
Submission:
Each team should turn in a single hardcopy submission packet for all problems by slipping it
under Lyn’s office door by 6pm on the due date. The packet should include:
1. a team header sheet (see the end of this assignment for the header sheet) indicating the time
that you (and your partner, if you are working with one) spent on the parts of the assignment.
2. your final version of SetHand.java.
Each team should also submit a single softcopy (consisting of your final ps1 directory) to the
drop directory ~cs230/drop/ps1/username, where username is the username of one of the team
members (indicate which drop folder you used on your hardcopy header sheet). To do this, execute
the following commands in Linux in the account of the team member being used to store the code.
cd /students/username/cs230
cp -R ps1 ~cs230/drop/ps1/username/
Problem 0: Getting Started
a. : Puma Account
You should begin this assignment by requesting a Puma account by following the directions on
Handout #3.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Corrections for SetHand Assignment in CS230 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS230 Data Structures Handout # 8 Prof. Lyn Turbak February 1, 2004 Wellesley College Revised February 7, 2004

Problem Set 1

Due: 6pm Friday, February 6

Revisions: Feb 7 : (1) The 2ghs in the SetHand toString specification should have been 2hgs; (2) The 2gft and 2ghs in the SetHand compareTo specification should have been 2fgt and 2hgs’ (3) The previous ordering in Fig. 1 was incorrect: ’h’ was consistently shown before ’f’, when it should have been the other way around.

Overview: The purpose of this assignment is to give you practice writing some simple Java methods using Emacs and Java on the Linux workstations. Since learning a new programming environment takes time, it is strongly recommended that you (1) start early and (2) work with a partner. Allocate time over several days to work on the problems; it is very unwise to start the assignment only a day or two before it is due. Don’t hesitate to ask for help if you hit a roadblock.

Reading:

  • Handouts #1 – #7;
  • Read Chapters 1–7 of Downey’s online Java book (accessible from the Resource Links section of the CS230 home page).

Submission: Each team should turn in a single hardcopy submission packet for all problems by slipping it under Lyn’s office door by 6pm on the due date. The packet should include:

  1. a team header sheet (see the end of this assignment for the header sheet) indicating the time that you (and your partner, if you are working with one) spent on the parts of the assignment.
  2. your final version of SetHand.java.

Each team should also submit a single softcopy (consisting of your final ps1 directory) to the drop directory ~cs230/drop/ps1/username, where username is the username of one of the team members (indicate which drop folder you used on your hardcopy header sheet). To do this, execute the following commands in Linux in the account of the team member being used to store the code.

cd /students/username/cs cp -R ps1 ~cs230/drop/ps1/username/

Problem 0: Getting Started

a. : Puma Account You should begin this assignment by requesting a Puma account by following the directions on Handout #3.

b. : Linux

Once you have your Puma account, you should log in to a Linux workstation and learn some simple Linux commands, as described in Handouts #3 and #4.

c. : Emacs

Next you should learn how to use Emacs. See the information in Handouts #3 and #5 on using Emacs. Learning to execute all cursor-motion and editing commands via keystrokes (rather than via mouse and menus) is an important skill that will save you lots of time over the semester. It will also make it easier for you to work remotely via telnet/ssh. A good way to begin learning the keystroke commands is taking the online interactive Emacs tutorial (see Handout #3 for how to do this).

d. : CVS

To do the rest of the problems on this assignment, you will need to use several files that are in the CVS-controlled CS230 repository. Follow the directions in Handout #7 for how to install your local CVS filesystem. You only need to install it once.

Once you have installed your local CVS filesystem, you can access all CVS-controlled files by executing the following in a Linux shell:

cd ~/cs cvs update -d

Indeed, every time you log in to a Linux machine to work on a CS230 assignment, you should execute the above commands to ensure that you have the most up-to-date versions of the problem set materials.

On this assignment, executing the above commands will create the local directory ~/cs230/ps containing three files:

  1. SetHand.java: This file contains skeletons for each of Java methods you need to write for this assignment. It also contains code for testing these methods. This is the only one of the three files that you should edit as part of this assignment. Note that for several problems it is helpful to define your own helper methods.
  2. SetCard.java: This file contains an implementation of the SetCard abstraction discussed in class. You do not need to study it, but may learn how to concisely solve certain problems if you do (such as how to implement the equals and compareTo methods of the SetHand class).
  3. Tester.java: This file contains an implementation of the testing framework used in this and other assignments. You do not need to study it.

e. : Compiling SetHand.java

Next you should compile the SetHand.java program. To do this, first make sure that your current directory is set appropriately by executing the following command in a Linux shell (you only need to do this once):

cd "~/cs230/ps1"

Now you can compile it by executing the following Linux command:

javac SetHand.java

The rest of this assignment involves the implementation of some simple aspects of the Game of Set^1 , which was introduced in class. There are two Java classes you need to understand:

  1. The SetCard class models cards in the game. A contract for SetCard is presented in Appendix A. In addition to the methods presented in lecture, it also includes an equals method and a compareTo method. You have been provided with a working version of this class in the file SetCard.java.
  2. The SetHand class models hands consisting of three cards. A contract for SetHand is presented in Appendix B. The file SetHand.java contains stubs for each of the methods in the contract, along with appropriate testing methods. Your goal is to replace the stubs by versions of the method that work in all cases. For some methods there are simple and concise solutions as well as verbose and complex solutions. You should strive to make all your method definitions as simple as possible. It is often the case that method definitions can be simplified by using one or more helper methods. In particular, if you find yourself using the same coding idiom more than once, you should try to abstract over that idiom by introducing a helper method.

Problem 1 [25]: Constructors and Selectors Begin by implementing the following two constructor methods and three selector methods for SetHand:

public SetHand (SetCard sc1, SetCard sc2, SetCard sc3); // card constructor public SetHand (String s1, String s2, String s3); // string constructor public SetCard card1 (); public SetCard card2 (); public SetCard card3 ();

Your implementation should use a single instance variable cards that holds an array of three cards. The specification requires that cards1(), cards2(), and cards3() should return the three cards from smallest to largest according to the ordering on cards (see the definition of compareTo in Appendix A). There are many ways to do this, but it is recommended that you store the three cards into cards in sorted order. If you don’t know how to do this right now, you can implement simple constructors that ignore order. Later, you can return to this problem to consider order. The constructor specifications also require that RuntimeExceptions be thrown in certain cir- cumstaces. You can do this as follows:

throw new RuntimeException (message );

where message is a string indicatin the nature of the error. You can test your methods via the following Linux commands: java SetHand cardConstructor java SetHand stringConstructor

Here and in the other problems, you should extend the set of test cases to better test your methods.

Problem 2 [20]: toString and fromString Implement the toString and fromString methods for SetHand. A fully working version of fromString filters out whitespace. If you don’t know how to do this right now, you can implement

(^1) See http://www.setgame.com/set/index.html for more details about the game. Our version of the game differs from the “official” version in a two ways: our shapes are circles, squares, and triangles rather than diamonds, ovals, and squiggles; and our colors are blue, green, and red rather than green, purple, and red.

a simple version of fromString that assumes that its input has no whitespace. Later, you can return to handle whitespace filtering. You can test your methods via the following Linux commands: java SetHand toString java SetHand fromString

As above, you are expected to extend the suite of test cases.

Problem 3 [10]: equals and compareTo Implement the equals and compareTo methods for SetHand. For each of these methods there is a very simple solution. Hint: Study the implementation of the equals and compareTo methods of the SetCard method. You can test your methods via the following Linux commands: java SetHand equals java SetHand compareTo

As above, you are expected to extend the suite of test cases.

Problem 4 [25]: isSet Implement the isSet method for SetHand. Aim for a simple and concise solution. If you find yourself entangled in a rat’s nest of conditionals, you are going down the wrong path. A working solution can be implemented in ≤ 12 lines of code without using a single occurrence of if. Hints: (1) each of the four card attributes can be handled (almost) uniformly, with a slight tweak for the number attribute; (2) use one or more helper methods. You can test your method via the following Linux command: java SetHand isSet

As above, you are expected to extend the suite of test cases.

Problem 5 [25]: completeSet Implement the completeSet method for SetHand. Again, a remarkably simple and concise solution is possible. Hints: (1) each of the four card attributes can be handled (almost) uniformly, with a slight tweak for the number dimension; (2) it may help to convert ASCII characters to integers (this will be discussed in lecture on Tue. Feb. 3); (3) use one or more helper methods. You can test your methods via the following Linux command: java SetHand completeSet

As above, you are expected to extend the suite of test cases.

finally by shape. Fig. 1 shows the ordering of the string representation all 81 cards in the Game of Set ordered from least to greatest according to the card ordering. The ordering has been chosen so that it coincides with the lexicographic ordering of the strings representing the cards. For example, two green filled triangles is “less than” two green hatched squares because "2fgt" is less than "2hgs" in dictionary order.

Public Class Methods:

public static SetCard fromString (String s); Returns a card whose string representation is s. E.g., SetCard.fromString("2ert") returns a card with two empty red triangles. Throws a RuntimeException if the string s is not a valid string representation of a card.

1ebc, 1ebs, 1ebt, 1egc, 1egs, 1egt, 1erc, 1ers, 1ert, 1fbc, 1fbs, 1fbt, 1fgc, 1fgs, 1fgt, 1frc, 1frs, 1frt, 1hbc, 1hbs, 1hbt, 1hgc, 1hgs, 1hgt, 1hrc, 1hrs, 1hrt, 2ebc, 2ebs, 2ebt, 2egc, 2egs, 2egt, 2erc, 2ers, 2ert, 2fbc, 2fbs, 2fbt, 2fgc, 2fgs, 2fgt, 2frc, 2frs, 2frt, 2hbc, 2hbs, 2hbt, 2hgc, 2hgs, 2hgt, 2hrc, 2hrs, 2hrt, 3ebc, 3ebs, 3ebt, 3egc, 3egs, 3egt, 3erc, 3ers, 3ert, 3fbc, 3fbs, 3fbt, 3fgc, 3fgs, 3fgt, 3frc, 3frs, 3frt, 3hbc, 3hbs, 3hbt, 3hgc, 3hgs, 3hgt, 3hrc, 3hrs, 3hrt

Figure 1: String representations of the 81 cards in the Game of Set, ordered from least to greatest.

Appendix B: SetHand Contract

The SetHand class models a hand of three distinct cards in the Game of Set.

Public Constructor Methods:

public SetHand (SetCard sc1, SetCard sc2, SetCard sc3); Creates a hand with the three given cards. If the three cards are not pairwise distinct, a RuntimeException is thrown.

public SetHand (String s1, String s2, String s3); Creates a hand whose three cards are specified by the three string representations s1, s2, and s3. For example, new SetHand("2fgs", "3hrt", "1ebc") creates a set whose cards are: 2 filled green squares, 3 hatched red triangles, and 1 empty blue circle. If any of the cards is not a valid string representation of a card, or if the three cards are not pairwise distinct, a RuntimeException is thrown.

Public Instance Methods:

public SetCard card1 (); Returns the least card in this hand according to the ordering on cards. For example, if sh is new SetHand("2fgs", "3hrt", "1ebc"), then sh.card1().toString() is "1ebc".

public SetCard card2 (); Returns the middle card in this hand according to the ordering on cards. For example, if sh is new SetHand("2fgs", "3hrt", "1ebc"), then sh.card2().toString() is "2fgs".

public SetCard card3 (); Returns the greatest card in this hand according to the ordering on cards. For example, if sh is new SetHand("2fgs", "3hrt", "1ebc"), then sh.card3().toString() is "3hrt".

public String toString (); Returns a string representation of this hand. A string representation of a hand consists of the string representations of the three cards, in sorted order, separated by commas and delimited by square brackets. For example, if sh is new SetHand("2fgs", "3hrt", "1ebc"), then sh.toString() is "[1ebc,2fgs,3hrt]".

public boolean equals (Object x); Returns true if x is a SetHand with the same three cards as this hand, and false otherwise.

public int compareTo (Object x); If x is a SetHand instance, returns a negative integer if this hand comes before x in the hand ordering, 0 if this hand is equal to x in the hand ordering, and a positive number if this hand is greater than x in the hand ordering. If x is not a SetCard instance, throws a ClassCastException. The ordering on two hands h1 and h2 is the lexicographic ordering on the three cards in each hand, considered from smallest to largest. That is, h1 and h2 are first compared by card1(), then by card2(), and finally by card3(). This ordering coincides with the dictionary ordering on the string representations of the hands. For example, the following string representations of hands are shown from smallest to largest in the hand ordering: [1fgc,1fgs,1fgt], [1fgc,1fgs,2hgc], [1frc,2hrt,3ers], [1frc,3hrs,3hrt], [2ebs,2egt,2erc], [3hbc,3hrs,3hrt]

public boolean isSet (); Returns true if this hand is a set in the Game of Set – i.e., if for each of the four card attributes, the three cards in this hand either have the same value of the attribute or have pairwise distinct values of the attribute. An alternative characterization is that the three cards in this hand are a set as long as there is no attribute for which two cards have the same value, but the other card has a different value. For example, in the above list of six hands, there are three sets: [1fgc,1fgs,1fgt] [1frc,2hrt,3ers], and [2ebs,2egt,2erc].

Public Class Methods:

public static SetHand fromString (String s); Returns a hand whose string representation is s. For example, SetHand.fromString("[1ebc,2fgs,3hrt]") is equivalent to SetHand("1ebc", "2fgs", "3hrt"). The order of cards in the string s does not matter, and whitespace in s is ignored. E.g., the following strings are interpreted in the same way by fromString:

public static void testToString () { Tester t = new Tester() { public String answer (String input) { SetCard sc1 = SetCard.fromString(input.substring(0,4)); SetCard sc2 = SetCard.fromString(input.substring(5,9)); SetCard sc3 = SetCard.fromString(input.substring(10,14)); SetHand sh = new SetHand(sc1,sc2,sc3); return sh.toString(); } }; t.test("Testing toString() method:", new String [] [] { // Each test entry is of form input, expected output { "1ebc,2fgs,3hrt", "[1ebc,2fgs,3hrt]" }, { "3hrt,1ebc,2fgs", "[1ebc,2fgs,3hrt]" }, { "1ebc,1ers,2frs", "[1ebc,1ers,2frs]" } // Add more entries here. }); }

Figure 2: Implementation of the testing method testToString.

indicates that for input string "1ebc,2fgs,3hrt" it is expected that the output string will be "[1ebc,2fgs,3hrt]". When the test method is invoked (on zero arguments), it first displays a dotted line, followed by name, followed by a sequence of lines showing each test case, and ends with a dotted line. For example, here is the testing output displayed when testToString() is invoked on the initial implementation of SetHand:

[lyn@jaguar ps1] java SetHand toString

Testing toString() method: 1ebc,2fgs,3hrt => [1ebc,2fgs,3hrt] : OK! 3hrt,1ebc,2fgs => [1ebc,2fgs,3hrt] : OK! 1ebc,1ers,2frs => ********************ERROR******************** Expected: [1ebc,1ers,2frs] Actual: [1ebc,2fgs,3hrt]


Each entry is processed by invoking the answer method of the tester instance t on the input string to produce an actual output string. If the actual output string is the same as the expected output string, the line

input-string => actual-output-string : OK!

is displayed. This means that the test represented by the entry has succeeded. However, if the actual output string is different from the expected output string, the test represented by the entry has failed, and the following output is displayed:

input-string => ********************ERROR******************** Expected: expected-output-string Actual: actual-output-string


This output highlights the mismatch between the expected output string and the actual output string. On this assignment, you have been provided with testing methods for each of the methods you are supposed to write. In future assignments, you will be expected to write your own testing methods from scratch. Each of the testing methods provided in SetHand.java contains only a few test case entries. As part of doing this assignment, you are expected to add more entries to each tester to better test the method for that tester.