Docsity

Implementing IntVector Class and Blackjack Points Strategy in Java, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
CS230 Data Structures Handout # 9
Prof. Lyn Turbak Friday, September 27
Wellesley College
Problem Set 4
Due: 6pm Friday, October 4
Exam 1 Notice:
In class on Friday, October 4, the first take-home exam will be handed out. It will be due at
6pm on Friday, October 11. This is a hard deadline. No extensions will be given after
this time. The exam will cover the material in lecture through Lecture 9 (Tue. Oct. 1) and the
material in problem sets through PS4: object diagrams, iteration, recursion, arrays, vectors, lists,
enumerations, text processing, using and implementing abstract data types.
Because you should focus on the exam, it is strongly recommended that you submit PS4 on
time (6pm Friday October 4).
Overview: In this problem set, you will get experience with implementing a vector data structure
and recursive list programs.
Download: To begin this assignment, you should download a copy of the directory ~cs230/download/ps4.
In your local copy of this directory, you will:
•Modify IntVector.java in Problem 1.
•Modify BlackjackPoints.java in Problem 2.
•Modify ChangeMaker.java in Problem 3.
Submission:
•For Problem 1, your hardcopy submission should be your final version of IntVector.java.
•For Problem 2, your hardcopy submission should be your final version of BlackjackPoints.java.
•For Problem 3, your hardcopy submission should be your final version of ChangeMaker.java.
Your softcopy submission for this problem should be the entire ps4 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 [45]: An Array Implementation of Integer Vectors
The IntVector class
We have seen that the Java Vector class is a versatile data structure handy for a wide range of
uses. The fact that the elements of a vector have type Object means that vectors can store any kind
of object. However, as we have seen, this feature is a source of inconvenience and inefficiency when
the elements of a vector all have the same type. In this case, every extraction of a vector element
requires a type downcast, which not only makes the code uglier, but implies a run-time check on
the type of the object. The situation is even worse in the case of storing primitive datatypes in a
vector. In this case it is additionally necessary to package each primitive datum in an instance of a
“wrapper class” (e.g. Integer) before storing it in the vector, and unpackage it upon extraction.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementing IntVector Class and Blackjack Points Strategy in Java and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS230 Data Structures Handout # 9 Prof. Lyn Turbak Friday, September 27 Wellesley College

Problem Set 4

Due: 6pm Friday, October 4

Exam 1 Notice: In class on Friday, October 4, the first take-home exam will be handed out. It will be due at 6pm on Friday, October 11. This is a hard deadline. No extensions will be given after this time. The exam will cover the material in lecture through Lecture 9 (Tue. Oct. 1) and the material in problem sets through PS4: object diagrams, iteration, recursion, arrays, vectors, lists, enumerations, text processing, using and implementing abstract data types. Because you should focus on the exam, it is strongly recommended that you submit PS4 on time (6pm Friday October 4).

Overview: In this problem set, you will get experience with implementing a vector data structure and recursive list programs.

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

  • Modify IntVector.java in Problem 1.
  • Modify BlackjackPoints.java in Problem 2.
  • Modify ChangeMaker.java in Problem 3.

Submission:

  • For Problem 1, your hardcopy submission should be your final version of IntVector.java.
  • For Problem 2, your hardcopy submission should be your final version of BlackjackPoints.java.
  • For Problem 3, your hardcopy submission should be your final version of ChangeMaker.java.

Your softcopy submission for this problem should be the entire ps4 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 [45]: An Array Implementation of Integer Vectors

The IntVector class We have seen that the Java Vector class is a versatile data structure handy for a wide range of uses. The fact that the elements of a vector have type Object means that vectors can store any kind of object. However, as we have seen, this feature is a source of inconvenience and inefficiency when the elements of a vector all have the same type. In this case, every extraction of a vector element requires a type downcast, which not only makes the code uglier, but implies a run-time check on the type of the object. The situation is even worse in the case of storing primitive datatypes in a vector. In this case it is additionally necessary to package each primitive datum in an instance of a “wrapper class” (e.g. Integer) before storing it in the vector, and unpackage it upon extraction.

In this problem, we will explore the implementation of an IntVector class that is a template for a specialized kind of vector that can store only integers. The IntVector class has the same kinds of methods as the Vector class, except that wherever a Vector method returns an Object, an IntVector returns an int. When using the IntVector class to model a collection of integers, it is not necessary to package the integers into instances of the Integer class, nor is it necessary to use type downcasts when extracting elements from the vector. We will implement an instance of IntVector using an array of integers. Here is a diagram of one instance of IntVector that contains, in order, the three integers 17, 0, and 42:

The elts instance variable points to an integer array that stores the elements of the vector. The number of slots in the array (known as the capacity of the vector) can be any number greater than or equal to the number of elements in the vector. The size instance variable indicates the number of slots (from left to right, starting at slot 0) that contain the elements of the vector. The remaining slots are extra space for the vector to “grow into”. These extra slots may contain any integers whatsoever; their values do not matter because they are ignored by the IntVector methods. We shall refer to the values of these extra slots as “garbage”. In the above example, the size of 3 indicates that only slots 0 through 2 hold vector elements and slots 3 and 4 hold garbage. We will assume that as long as the number of elements in the vector remains less than or equal to the capacity, the array held by the elts variable does not change. In this case, vector operations that change, add, or remove elements must rearrange the elements within the slots of the existing array. For instance, performing v.add(-9) and then v.add(2,230)on the above vector representation should change the state of the objects as follows, where the fact that the array label has not changed indicates that it is the same array as in the previous diagram.

When an element is added or inserted into a vector whose size is equal to its capacity, it is nec- essary to increase its capacity. This is accomplished by creating a larger integer array, copying the vector elements from the old array to the new array, and making elts point to the new array. The number of slots by which the capacity should be increased is indicated by the capacityIncrement instance variable. For example, since the capacityIncrement is 4 in the example, performing

Implementing the IntVector Class Your task in this problem is to implement the IntVector class using the “extensible array” representation discussed above. You should flesh out the skeletons for the above constructor methods and instance methods in the IntVector class in the file IntVector.java. Invoking java IntVector tests your implementation on a suite of test cases. Each test case will be run on vectors whose capacity increments are 0, 1, and 8.

Notes

  • The case where both the capacity and capacityIncrement are 0 is problematic, since doubling 0 yields 0 rather than a larger capacity! Your code should handle this as a special case by setting the new capacity to be 1.
  • Your code will need to indicate an error when and index is not within the valid indices of the vector in the get, set, (two-argument) add, and remove methods. To do this you should use the follow statement:

throw new ArrayIndexOutOfBoundsException(message);

where message is a string indicating the specific error message.

Problem 2 [30]: Blackjack Points

Counting points in Blackjack In the game of Blackjack, the goal is to acquire a hand of cards worth as close to 21 points as possible (inclusive) without going over 21 points. The points for each card are determined as follows:

  • A card with value two through ten is worth its value.
  • Face cards (jacks, queens, kings) are worth 10 points.
  • An ace is worth either 1 point or 11 points – the player decides which. Of course, the player will attempt to assign aces values that may the hand as close as possible to 21

If an ace were to have only one value (say 1), then the point value of a hand would be easy to calculate, as show in the following handPoints method:

public static int handPoints (Hand h) { // Assume that every ace counts as a 1; int points = 0; for (int i = 0; i < h.size(); i++) { points = points + cardPoints(h.get(i), false); } return points; }

private static int cardPoints (Card c, boolean acesHigh) { // Return the number of points for the card c. // If acesHigh is true, counts aces as 11; otherwise counts them as 1. if (c.value().equals(Value.ACE)) { if (acesHigh) { return 11; } else { return 1; } } else if (c.value().toInt() >= 10) { return 10; } else { return c.value().toInt(); } } The fact that aces can have two possible values makes the calculation trickier. There are many ways to address the ace-has-two-possible-values problem. Here we shall explore one approach that is particularly elegant and flexible (in the sense that it’s easy to extend to games where many cards can each have many possible values). In this approach, we use an auxiliary method pointList that calculates a list of the possible points for a given hand. The list is such that the elements are sorted from low to high and there are no duplicates. Given such a method, the handPoints method can be rewritten to handle two-valued aces as follows:

public static int handPoints (Hand h) { return closestTo21(pointList(h)); }

Here, closestTo21 is a method that finds the value in a non-empty sorted integer list that is closest to 21, choosing any value less than or equal to 21 in preference to one greater than 21.

Your Task In this problem, your task is to flesh out several list methods in the BlackjackPoints class in ps4/BlackjackPoints.java that implement the above strategy:

a. [5] public static int closestTo21 (IntList L); Assume that L is a non-empty list of integers sorted from low to high. If L contains elements less than or equal to 21, returns the largest integer that is less than or equal to 21. If L contains only elements greater than 21, returns the smallest such element.

Problem 3 [25]: Making Change Lists of lists are useful data structures for holding lists of possibilities. For example, as seen in lecture, we can use them to hold the list of all subsequences of a list of numbers. In this problem, we will use lists of lists to represent all the possible ways of making change for a given amount of money using an unlimited number of coins from a given list of denominations. You may assume that the amount of money is a non-negative integer, and that the list of coin denominations is a list of distinct positive integers ordered in decreasing order. When making change for the given amount, you may use any number of “copies” of the coins in the denomination list. In the resulting list of lists, each element (which we shall call a change list should be a list of coin values sorted in order of decreasing value whose sum is the given amount of money. These change lists should be ordered in reverse lexicographic order (i.e., reverse dictionary order). That is, change lists with a larger first coin come before change lists with a smaller first coin. If two change lists have the same first coin, the one with the largest second coin goes first. And so on. A few simple examples are illustrated in Fig. 1

Your goal is to formulate a strategy that will list all the possible ways to make change for a given amount of money with a list of possible denominations provided in descending order of value. To do this, you will flesh out the makeChange method in the ChangeMaker.java file within the ps directory. The contract for the method is as follows:

public static static IntListList (makeChange); int amount, IntList coins Assume that amount is a non-negative integer and that coins is a list of distinct positive integers in descending order. Returns a list of all possible change lists, where a change list is a list of denominations taken from coins (potentially with duplicates) whose sum is amount. The denominations in a change list should be in descending order. The change lists themselves should be ordered in reverse lexicographic order.

Notes

  • The main() method of the ChangeMaker class (which has been provide for you) can be invoked in two ways. 1. java ChangeMaker makeChange amt coins prints the change list returned by the in- vocation makeChange(a, c), where a is the integer corresponding to amt and c is the IntList parsed from the string coins. For example: $ java ChangeMaker makeChange 16 "[10, 5, 1]" There are 6 ways to make change for 16 cents using [10, 5, 1]: [[10, 5, 1], [10, 1, 1, 1, 1, 1, 1], [5, 5, 5, 1], [5, 5, 1, 1, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ] 2. java ChangeMaker test displays the result of testing the makeChange method on a number of test cases. The correct output for the test cases is shown in Figs. 2–3.

Amount of money Denominations

Ways to Make Change

5 [5,1] (^) [[5], [1,1,1,1,1]] 1 nickel or 5 pennies

16 [10,5,1] [[10, 5, 1],

[10, 1, 1, 1, 1, 1, 1],

[5, 5, 5],

[5, 5, 1, 1, 1, 1, 1, 1],

[5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1]]

16 [10,5] []

21 [10,5,1] [[10, 10, 1],

[10, 5, 5, 1],

[10, 5, 1, 1, 1, 1, 1, 1],

[10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

[5, 5, 5, 5, 1],

[5, 5, 5, 1, 1, 1, 1, 1, 1],

[5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

[5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 1]

]

Figure 1: Some examples of change making.


There are 2 ways to make change for 15 cents using [10, 5]: [[10, 5], [5, 5, 5] ]


There are 0 ways to make change for 16 cents using [10, 5]: [ ]


There are 2 ways to make change for 5 cents using [10, 5, 1]: [[5], [1, 1, 1, 1, 1] ]


There are 6 ways to make change for 16 cents using [10, 5, 1]: [[10, 5, 1], [10, 1, 1, 1, 1, 1, 1], [5, 5, 5, 1], [5, 5, 1, 1, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ]


There are 9 ways to make change for 21 cents using [25, 10, 5, 1]: [[10, 10, 1], [10, 5, 5, 1], [10, 5, 1, 1, 1, 1, 1, 1], [10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 1], [5, 5, 5, 1, 1, 1, 1, 1, 1], [5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ]


There are 13 ways to make change for 26 cents using [25, 10, 5, 1]: [[25, 1], [10, 10, 5, 1], [10, 10, 1, 1, 1, 1, 1, 1], [10, 5, 5, 5, 1], [10, 5, 5, 1, 1, 1, 1, 1, 1], [10, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 5, 1], [5, 5, 5, 5, 1, 1, 1, 1, 1, 1], [5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ]

Figure 2: The output of java ChangeMaker test (Part 1).


There are 60 ways to make change for 56 cents using [25, 10, 5, 1]: [[25, 25, 5, 1], [25, 25, 1, 1, 1, 1, 1, 1], [25, 10, 10, 10, 1], [25, 10, 10, 5, 5, 1], [25, 10, 10, 5, 1, 1, 1, 1, 1, 1], [25, 10, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 10, 5, 5, 5, 5, 1], [25, 10, 5, 5, 5, 1, 1, 1, 1, 1, 1], [25, 10, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 10, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 5, 5, 5, 5, 5, 5, 1], [25, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1], [25, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [25, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 1, 1], [5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] [5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] [5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] [5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] [5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ..., 1] ]

Figure 3: The output of java ChangeMaker test (Part 2). Ellipses have been used to abbreviate sequences of 1 s that would otherwise fall off the right end of the page.