Computer Science Fundamentals

85 views 10:11 am 0 Comments March 12, 2023
Assignment 2 Computer Science Fundamentals II CS 1027

Due date: Monday, February 27 at 11:55 pm
Learning Outcomes
In this assignment, you will get practice with:

Creating collections that work for generic types



Problem solving using sets and power sets
Working with linked lists
Using loops and conditionals
Programming according to specifications

Introduction
Cribbage is an old, but still popular, card game using a standard 52-card deck in which each
player has a hand of 4 cards. There is also one card from the remainder of the deck that is
flipped and used by all players – this card is called the “starter”. There are two phases in each
round of the game: the pegging phase and the counting phase. We will focus on the counting
phase only. In the counting phase, each player will count the number of points they should
receive from their 4 cards plus with the communal starter card.
There are a variety of ways to score points in this phase of Cribbage: Pairs, Runs, Fifteen,
Flush, and His Knobs. Each of these categories is described with examples below. Scoring can
occur from any number of these categories and the total number of points includes points from
all these categories.
Each card has two properties: its
suit and its rank. The suit is the red or black symbol on the
card: Hearts, Diamonds, Clubs, or Spades. The rank is the value of the card’s number or letter.
Most cards have a number from 2 to 10 and their rank is equal to that numeric value. Some
cards have a letter instead of a number; i.e. A, J, Q, or K. The A (ace) has a rank of 1, J (jack)
has a rank of 11, Q (queen) has a rank of 12, and K (king) has a rank of 13. When dealing with
Runs (see description below), these are the ranks to be considered to check for consecutive
sequences. For example, 10, J, Q is a run of 3 consecutive cards based on these ranks.
However, when summing to Fifteen (see description below), the face cards (J, Q, and K) have
values of 10. It’s important to remember that the rank of these cards is different for Runs than
they are for summing to Fifteen. Examine the provided Card class to see the two different rank
getter methods, getRunRank() and getFifteenRank().
Pairs
Two cards with the same number/letter label are considered a pair and it is worth 2 points. If you
have more than 2 cards with the same number/letter label, each pair would score. For example,
suppose you have 2H (2 of Hearts), 2S (2 of Spades), and 2C (2 of Clubs) among your 5 cards.

Assignment 2 Computer Science Fundamentals II CS 1027

With these cards, there are three pairs: 2H-2S, 2H-2C, and 2S-2C. Each of those three pairs
scores 2 points for a total of 6 points.
Runs
Three or more cards with consecutive ranks (remember that J, Q, and K have ranks of 11, 12,
and 13 respectively for this scoring category) are considered to form a run. Only the longest run
will count and will score points equal to the length of the run. For example, if there is 7, 8, 9, and
10, it would score 4 points as a run of 4 cards. The runs of 3 (<7, 8, 9> and <8, 9, 10>) would
not count since the run of 4 is the longest run.
If there are multiple runs of the same length, they would all count. For example, if there is 7D,
8H, 9C, 10D, and 10H, there would be two runs of 4 (<7D, 8H, 9C, 10D> and <7D, 8H, 9C,
10H>) so both runs would score for a total of 8 points for the two runs. Sometimes there are
three or four different runs and they would each score in the same manner. For example, if
there is 7D, 8D, 8H, 9S, and 9H, there would be four runs of length 3 (<7D, 8D, 9S>; <7D, 8D,
9H>; <7D, 8H, 9S>; and <7D, 8H, 9H>) so the score from these runs would be 12 points.
The above set of cards would yield the following 4 runs:

Assignment 2 Computer Science Fundamentals II CS 1027
Each of these runs would score 3 points for a total of 12 points for all the runs.
Since the runs are difficult and complex to determine, we are providing you with a private helper
method called isRun(Set<Card>
set) which you are encouraged to use to help in calculating this
scoring category. This method will return
true if the given set represents a run of 3 or more
consecutive ranked cards, and false otherwise. You still have to check if the given run is the
longest and only score it if there is no longer run in the sequence. Hint: keep track of the longest
run length and then only score the one(s) whose length is the longest.
Fifteen
Any combination of cards that add up to 15 will score 2 points. For example, 6 and 9 add up to
15 so that would score 2 points. Queen, Ace, and 4 add up to 15 and would also score 2 points.
Remember that J, Q, and K are all worth 10 in this scoring category.
A card can be used in multiple additions to fifteen. For example, if there is 2D, 2H, 5D, 8D, 8C,
there would be multiple add-ups to fifteen (<2D, 5D, 8D>, <2D, 5D, 8C>, <2H, 5D, 8D>, and
<2H, 5D, 8C>) so it would score 8 for this category of scoring.
Flush
If all 4 cards in the hand have the same suit, it is a flush worth 4 points. If the starter also has
the same suit as the 4 cards in the hand, then it scores 5 points instead of 4.

Assignment 2 Computer Science Fundamentals II CS 1027
(starter)
The above set of cards would score 4 points for a flush in the hand.
(starter)
The above set of cards would score 5 points for a flush in the hand including the starter card.
(starter)
The above set of cards would score 0 points since the 4 cards in the hand are not all the same
suit.
His Knobs
If there is a Jack within the 4 cards of the hand and its suit matches the suit of the starter card, it
is called “His Knobs” and is worth 1 point.
(starter)
The above set of cards would yield 1 point for “His Knobs” since the starter card’s suit is
Diamonds and the hand contains a Jack of Diamonds.

Assignment 2 Computer Science Fundamentals II CS 1027
(starter)
The above set of cards would not yield a point for “His Knobs” since the starter card’s suit is
Spades and the hand does not contain a Jack of Spades.
Important Reminders:


Aces represent (and have a value of) 1 when summing to Fifteen and for Runs
As explained above, Jacks, Queens, and Kings all have values of 10 when summing to
Fifteen, but are distinct ranks when it comes to Runs (for example, 8, 9, Jack is not a run
even though Jack has a value of 10 in the counting. 10, Jack, Queen is a valid run).
o To simplify the ranks of these face cards for runs, we give Jack a value of 11,
Queen a value of 12, and King a value of 13.

o For summing to Fifteen, these face cards all have a value of 10.
Examples of Scoring Totals
(starter)
Hand: 4H, 2H, KC, 3C Starter: 9D
3pts Run of 3 (2H, 3C, 4H)
2pts Fifteen (2H+3C+KC)
2pts Fifteen (2H+4H+9D)
Total points: 7

Assignment 2 Computer Science Fundamentals II CS 1027
(starter)
Hand: 10H, JS, QC, QD Starter: 5S
2pts Pair of Queens
3pts Run of 3 (10H, JS, QC)
3pts Run of 3 (10H, JS, QD)
2pts Fifteen (5S+10H)
2pts Fifteen (5S+JS)
2pts Fifteen (5S+QC)
2pts Fifteen (5S+QD)
1pt His Knobs (JS same suit as starter 5S)
Total points: 17
(starter)
Hand: AD, JC, KC, 4H Starter: 4C
2pts Fifteen (AD+4H+JC)
2pts Fifteen (AD+4C+JC)
2pts Fifteen (AD+4H+KC)
2pts Fifteen (AD+4C+KC)
2pts Pair of Fours
1pt His Knobs (JC same suit as starter 4C)
Total points: 11

Assignment 2 Computer Science Fundamentals II CS 1027

Some of the categories of scoring are dependent on multiple combinations of cards, so we will
use a
Power Set to generate all the different possible combinations of cards so that we can
examine each set in the Power Set to compute the total score. A Power Set means a series of
sets that covers every possible combinations of element(s) from the original set, including an
empty set. For example, from the set {1, 2, 3}, the Power Set would include all of the following
sets: {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}, and {}. Note that for any set with n elements, its
Power Set will contain 2
n sets. In this example, there are 3 elements, so the Power Set contains
2
3 = 8 sets.
Classes to Implement
For this assignment, you must implement three (3) Java classes: Set, PowerSet, and Counter.
Follow the guidelines for each one below.
In these classes, you may implement more private (helper) methods if you want. However, you
may not implement more public methods
except public static void main(String[] args) for testing
purposes (this is allowed and encouraged). You may not add instance variables other than the
ones specified in these instructions nor change the variable types or accessibility (i.e. making a
variable
public when it should be private). Penalties will be applied if you implement additional
instance variables or change the variable types or modifiers from what is described here.
Set.java
This class represents a simple collection that must be implemented with a singly-linked list. This
class must work for the generic type T.
The class must have the following private variable:
LinearNode<T> setStart (the front of the linked list)
The class must have the following public methods:

public Set(): constructor
Initialize setStart to null
public void add(T element)
Create a new node containing the given element and add the new node to the
linked list. The order does not really matter for a Set, so you can either add to the

end or the start of the linked list for simplicity. Make sure you update the links
properly and account for different cases (i.e. adding the first node to an empty
list, adding a node to a list with other nodes, etc.)

public int getLength()
Return the number of items in the linked list
public T getElement(int i)

 

Assignment 2 Computer Science Fundamentals II CS 1027

 

Returns the element stored in the ith node of the linked list (NOTE: remember
that order does not matter, so this method is not being tested in the autograded
tests but it may be very useful for you when implementing the Counter class and
you need to extract individual elements from a Set within a loop).
public boolean contains(T element)
Returns true if the given element is found within the linked list; false otherwise
public String toString()
Returns a string containing each of the elements in the Set separated by a space

PowerSet.java
This class represents the Power Set from a given set. This class must also work with the
generic type T.
The class must have the following private variable:
Set<T>[] set
The class must have the following public methods:

public PowerSet(T[] elements): constructor


Generate the Power Set from the given T array of elements
See the hints below (
Power Set Generation) for help on how to approach this
Store the series of sets for this Power Set in the instance variable, set.
public int getLength()
Return the number of items in the array (the number of sets in the Power Set)
public Set<T> getSet(int i)
Return the Set stored at index i of the array

Power Set Generation
Creating a Power Set might sound difficult but there is a recommended approach that simplifies
the process.


A Power Set for a set of n elements will contain 2n sets.
To generate each of the 2
n sets with the proper combination of elements, take the binary
representation of each number from 0 to 2
n-1. Hint: you can use
Integer.toBinaryString(x) to convert an int, x, to its binary representation as a String.
Ensure that all the binary numbers have the same number of digits (they will need to
have n digits) using padded zeroes at the front, i.e. 0001.
Loop through each binary number and through each digit (bit) of the binary number.

Whenever the digit (bit) is a 1, add the item from the original set at the corresponding
index into the proper set for the Power Set (see the Example table below and the
resulting sets (below the table) that would be produced from each binary number)

Assignment 2 Computer Science Fundamentals II CS 1027

Example:
Suppose the original set of elements is [“A”, “B”, and “C”]. Since there are 3 items, there will be
2
3 = 8 sets in the Power Set. Loop through 0 to 7 and take the binary representation and pad
each with zeroes so they all have the same length (3).

Decimal Number Binary Representation Binary Number with Padded Zeroes
0 0 000
1 1 001
2 10 010
3 11 011
4 100 100
5 101 101
6 110 110
7 111 111

Next, loop through each of the padded binary numbers and look for 1’s and add the elements to
the corresponding sets.
000
Nothing is added, so this is the empty set. {}
001
“C” is added because there is a 1 in the 3rd position, so the item at index 3 from the
original array is added. {“C”}
010
“B” is added. {“B”}
011
“B” and “C” are added because there are 1s at the 2nd and 3rd position. {“B”, “C”}
100
“A” is added. {“A”}
101
“A” and “C” are added. {“A”, “C”}
110
“A” and “B” are added. {“A”, “B”}
111
All three elements are added. {“A”, “B”, “C”}
Counter.java
This class will be used to calculate the number of points from a Cribbage hand.
The class must have the following private variables:


PowerSet<Card> cardps
Card starter

The class must have the following public methods:

public Counter(Card[] hand, Card starter): constructor
Initialize the starter and use the PowerSet constructor to generate the Power Set
of the cards from the hand (note that the starter card is already included in the
hand array so the Power Set will be based on all 5 cards.
public int countPoints()
Calculate the number of points for the hand that was sent into the constructor.
Use the Power Set, cardps, to do the calculations so that all the combinations are

 

Assignment 2 Computer Science Fundamentals II CS 1027

checked. Refer to the scoring explanations in the Introduction when
implementing this method.
It is recommended that you implement one or more private helper methods to
help keep the code more organized and clean. For example, you may want
private helper methods for each of the scoring categories, i.e. one for checking if
a given Set sums to 15, another one to check if the given Set forms a run of 3 or
more consecutive cards, etc.
Provided files
You are provided with the following classes to get your project started: Card.java and
LinearNode.java. Examine both classes to see the provided methods. In the Card class, there
are several getter methods that will be useful to you to use when calculating the scores of the
hand of cards, particularly the getRunRank() and getFifteenRank() that were mentioned
previously. The LinearNode class is used when building the linked list for the Set class you are
implementing. You should use the getNext() and setNext() methods when making the linked list.
TestSetPowerSet.java and TestCounter.java are tester files to
help check if your java classes
are implemented correctly. TestSetPowerSet.java tests your Set.java and PowerSet.java files,
and TestCounter.java tests your Counter.java file.
Similar files will be incorporated into Gradescope’s auto-grader.
Passing all the tests within
these files does not necessarily mean that your code is correct in all cases.
Marking Notes
Functional Specifications




Does the program behave according to specifications?
Does it produce the correct output and pass all tests?
Are the classes implemented properly?
Does the code run properly on Gradescope (even if it runs on Eclipse, it is
up to you to
ensure it works on Gradescope to get the test marks)
Does the program produces compilation or run-time errors on Gradescope?
Does the program fail to follow the instructions (i.e. changing variable types, etc.)

Non-Functional Specifications



Are there comments throughout the code (Javadocs or other comments)?
Are the variables and methods given appropriate, meaningful names?
Is the code clean and readable with proper indenting and white-space?

 

Assignment 2 Computer Science Fundamentals II CS 1027

 



Is the code consistent regarding formatting and naming conventions?
Submission errors (i.e. missing files, too many files, etc.) will receive a penalty.
Including a “package” line at the top of a file will receive a penalty.

Remember you must do all the work on your own. Do not copy or even look at the work of
another student. All submitted code will be run through similarity-detection software
.
Submission (due Monday, February 27 at 11:55 pm)
Assignments must be submitted to Gradescope, not on OWL. If you are new to this platform,
see
these instructions on submitting on Gradescope.
Rules






Please only submit the files specified below.
Do not attach other files even if they were part of the assignment.
Do not upload the .class files! Penalties will be applied for this.
Submit the assignment on time. Late submissions will receive a penalty of 10% per day.
Forgetting to submit is not a valid excuse for submitting late.
Submissions must be done through Gradescope.
If your code runs on Eclipse but not
on Gradescope, you will NOT get the marks! Make sure it works on Gradescope to
get these marks.
You are expected to perform additional testing (create your own test harness class to do
this) to ensure that your code works for a variety of cases. We are providing you with
some tests but we may use additional tests that you haven’t seen for marking.
Assignment files are NOT to be emailed to the instructor(s) or TA(s). They will not be
marked if sent by email.
You may re-submit code as many times as you wish, however, re-submissions after the
assignment deadline will receive a late penalty.

Files to submit



Set.java
PowerSet.java
Counter.java

Grading Criteria
Total Marks: 20

Assignment 2 Computer Science Fundamentals II CS 1027

15 marks Passing Tests

5 marks Functional and non-functional Specifications (implementing the methods
and data structures as required, code readability, comments, correct

names for variables and functions, meaningful names for local variables
and private methods, proper indentation, etc.)