Old version
This is the CS 112 site as it appeared on May 12, 2023.
Problem Set 3
due by 11:59 p.m. on Tuesday, February 14, 2023
Preliminaries
In your work on this assignment, make sure to abide by the collaboration policies of the course.
If you have questions while working on this assignment, please come to office hours, post them on Piazza, or email instructor.
Make sure to follow the instructions outlined at the end of Part I and Part II when submitting your assignment.
Part I
40 points total
Creating the necessary folder
-
If you haven’t already created a folder named
cs112for your work in this course, follow these instructions to do so. -
Then create a subfolder called
ps3within yourcs112folder, and put all of the files for this assignment in that folder.
Creating the necessary file
Problems in Part I will be completed in a single PDF file. To create it, you should do the following:
-
Access the template that we have created by clicking on this link and signing into your Google account as needed.
-
When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.
-
Select File->Rename, and change the name of the file to
ps3_partI. -
Add your work for the problems from Part I to this file.
-
Once you have completed all of these problems, choose File->Download->PDF document, and save the PDF file in your
ps3folder. The resulting PDF file (ps3_partI.pdf) is the one that you will submit. See the submission guidelines at the end of Part I.
Problem 1: Our Rectangle class revisited
15 points total; individual-only
Put your answers to these questions in the Problem 1 section of
your copy of ps3_partI (see above).
Recall the Rectangle class that we defined in lecture. (The relevant
version of this class is available here.)
-
Consider a potential instance method named
tallerThanWidethat would returntrueif aRectangleobject has a height that is larger than its width, andfalseotherwise.- (1 point) What type of instance method would
tallerThanWidebe, an accessor or a mutator? - (3 points) Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method.
- (1 point) What type of instance method would
-
Now consider a potential instance method named
adjustthat would take two integers specifying a width and height and adjust the dimensions of aRectangleas needed so that its dimensions are at least as big as the ones specified by the parameters. For example, if aRectangle‘s dimensions are 100 x 50, then calling theadjustmethod with parameters of 80 and 70 would leave theRectangle‘s width alone (because 100 is already greater than 80), but change its height to 70 (because 50 is smaller than 70).- (1 point) What type of instance method would
adjustbe, an accessor or a mutator? - (3 points) Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method.
- (1 point) What type of instance method would
-
Consider the following client code — i.e., code from another class that uses a
Rectangleobject:Rectangle r1 = new Rectangle(30, 50); System.out.println(r1.width); r1.height = r1.height * 2; System.out.println(r1); // print the new dimensions
Because our
Rectangleclass employs appropriate encapsulation, this code fragment will not compile.- (2 points) Explain what problems are present in the code fragment that prevent it from compiling.
- (5 points) Rewrite the fragment to eliminate those problems while maintaining the intended functionality of the original version. Don’t make any unnecessary changes.
Problem 2: A class that needs your help
11 points total; individual-only
Consider the following class, which is intended to serve as a blueprint for objects that encapsulate two pieces of data: a street number (which must be positive) and a street name (which must be no more than 30 characters long).
public class StreetAddress { int num; String name; public boolean onEvenSide() { return (this.num % 2 == 0); } }
-
(7 points) This class does not employ appropriate encapsulation. In section 2-1 of
ps3_partI, revise the class to prevent direct access to the internals of aStreetAddressobject while allowing indirect access through appropriate methods. Your revised version should include:- whatever steps are needed to prevent direct access to the fields
- accessor methods that can be used to obtain the value of each field
- methods that can be used to change the value of each field.
These methods should ensure that
numis always greater than 0, and thatnameis no more than 30 characters long. Any attempt to assign an invalid value should produce anIllegalArgumentException. - a constructor that initializes the fields of a newly created
StreetAddressobject to the values specified by the parameters of the constructor. Any attempt to specify an invalid value should produce anIllegalArgumentException. Take advantage of the error-checking code that is already present in the methods that you defined for changing the values of the fields.
No other methods are required.
-
(4 points) Now imagine that you’re writing client code for your revised
StreetAddressclass – i.e., code in a different class that usesStreetAddressobjects.For each of the following tasks, write a single line of client code to accomplish the task:
-
Construct a
StreetAddressobject in which the number is650and the name is"Commonwealth Avenue"and assign it to a properly declared variable namedccds(short for “Center for Computing and Data Sciences”, the new home of the CS department!). -
Your friend points out that the correct street number for the new building is
665, not650. Change the value of thenumfield in the object that you created in part (a), giving it a value of665. You should not create a new object; you should change the internals of the existing object.Important: As a result of your changes from part 1, clients no longer have direct access to the fields in a
StreetAddressobject. As a result, you will need to make a method call to change the appropriate field. -
Get the
namecomponent of theStreetAddressobject that you created in part (a), and assign it to a properly declared variable namedstreetName. Here again, you will need to make an appropriate method call. -
Now imagine that you are given a second
StreetAddressobject that has been created by someone else and has been assigned to the variableaddr. Change the value of thenumfield inaddr, giving it a value that is 1 more than its current value.
-
Problem 3: Static vs. non-static
14 points total; individual-only
When designing a blueprint class, we have seen that we can include both static and non-static variables. Static variables (also known as class variables) belong to the class as a whole. Non-static variables (also known as instance variables or fields) belong to individual objects of the class; each object gets its own copy of those variables.
We have also seen that a blueprint class can include both static and non-static methods. A non-static method (also known as an instance method) is required if the method must have access to the fields of a particular called object. However, if a method doesn’t need a called object – i.e., if all of the information that it needs is supplied by its parameters or by the static variables of the class – then we typically make it static. In the same way that static variables are also known as class variables, static methods are sometimes referred to as class methods.
Whether a method is static or non-static has an impact on how we call
it. Non-static methods must be called on an object of the class (e.g.,
r1.area(), where r1 is a reference to a Rectangle
object). Static methods may be called on an object of the class, but
it is better style to call them using the name of the class instead.
In Part II, you will be implementing a Card class that
serves as a blueprint for objects that encapsulate the rank and
suit of a given playing card. Each Card will have two fields:
-
an integer field called
rankthat stores the rank of the card; for example, in aCardobject that represents the 10 of Diamonds, this field would have the value 10 -
a field called
suitthat stores a single character representing the suit of the card; for example, in aCardobject that represents the 10 of Diamonds, this field would have the value'D'.
In the rest of this problem, you will be thinking through some
possible changes to the Card class. Note that these changes are
hypothetical; you won’t actually be making these changes when you
implement the class in Part II!
-
(5 points) Imagine that after creating the version of the
Cardclass that is described in Part II, we decide that:-
We want to keep track of how many
Cardobjects have been created for each of the four suits (Clubs, Diamonds, Hearts, and Spades). -
In addition to its rank and suit, we want each
Cardobject to store its value (an integer like 4 or 11). Note that the value of aCardmay not be the same as its rank. For example, a Queen has a rank of 12, but its value in a game like Blackjack is 10.
What variables (static and/or non-static) would we need to add to the
Cardclass as part of our implementation of these changes?In section 2-1 of
ps3_partI, we have included a table that you should complete to describe the necessary variables. For each of your proposed variables, you should specify:- its type and name (make it descriptive!)
- whether it will be static or non-static
- a brief description of its purpose, and why it needs to be static or non-static.
As an example, we have filled in the first row of the table to describe the field called
rankthat you will be defining in Part II. You should add the descriptions of your proposed new variables. You may not need all of the rows in the table. -
-
(6 points) The version of the
Cardclass that you will implement in Part II will not have any mutator methods, so it won’t be possible to change the suit of an existingCardobject. Imagine that we want to add a mutator method calledsetSuitthat takes in acharand uses it to change the suit of the calledCardobject. For example, you might change the suit of aCardfrom'D'(for Diamonds) to'C'(for Clubs).-
What type of method should
setSuitbe — static or non-static? Explain briefly. -
Assume we have a
Cardobject whose current suit is'H'(i.e., it has the suit character that corresponds to Hearts). We decide to change the suit of theCardto'S'(for Spades) by making the appropriate call to our newsetSuitmethod. During that method call, what changes will the method need to make to the values of the variables in the class? You should include (1) any changes to the fields that were already present in the original version of the class (see above), and (2) any changes to the variables that you proposed above. Be as specific as possible. -
Give an example of how you would call this method from outside the
Cardclass. If you need aCardobject to call the method, assume that the variablecrepresents that object, and that the object has already been created. However, you should only usecif doing so is absolutely necessary.
-
-
(3 points) Finally, let’s say that we want to add another method called
numFaceCards. It takes a parameter that represents an array ofCardobjects, and it computes and returns the number of face cards in that array.-
What type of method should
numFaceCardsbe — static or non-static? Explain briefly. -
Give an example of how you would call this method from outside the
Cardclass to determine the number of face cards in an array ofCardobjects calledmyCards. If you need aCardobject to call the method, assume that the variablecrepresents that object, and that the object has already been created. However, you should only usecif doing so is absolutely necessary.
-
Submitting your work for Part I
Note: There are separate instructions at the end of Part II that you should use when submitting your work for that part of the assignment.
Submit your ps3_partI.pdf file using these steps:
-
If you still need to create a PDF file, open your
ps3_partIfile on Google Drive, choose File->Download->PDF document, and save the PDF file in yourps3folder. -
Login to Gradescope by clicking the link in the left-hand navigation bar, and click on the box for CS 112.
-
Click on the name of the assignment (
PS 3: Part I) in the list of assignments on Gradescope. You should see a pop-up window labeled Submit Assignment. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.) -
Choose the Submit PDF option, and then click the Select PDF button and find the PDF file that you created. Then click the Upload PDF button.
-
You should see a question outline along with thumbnails of the pages from your uploaded PDF. For each question in the outline:
- Click the title of the question.
- Click the page(s) on which your work for that question can be found.
As you do so, click on the magnifying glass icon for each page and doublecheck that the pages that you see contain the work that you want us to grade.
-
Once you have assigned pages to all of the questions in the question outline, click the Submit button in the lower-right corner of the window. You should see a box saying that your submission was successful.
Important
-
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
-
If you are unable to access Gradescope and there is enough time to do so, wait an hour or two and then try again. If you are unable to submit and it is close to the deadline, email your homework before the deadline to
cs112-staff@cs.bu.edu
Part II
60 points total
Problem 4: Array-processing methods
30 points; pair-optional
This is the only problem of the assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
Getting started
-
If you haven’t already done so, create a folder named
ps3for your work on this assignment. You can find instructions for doing so here. -
In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment.
-
Select File->New File, which will open up an empty editor window.
-
Select File->Save, and give the new file the name
ArrayMethods.java. -
In the new file, create a class called
ArrayMethods. In that class, take the steps described below. -
In addition, we highly recommend adding a
mainmethod to your class that makes test calls to your methods. Don’t forget to importjava.utilso that you can use theArrays.toStringmethod in your tests.
Important guidelines:
-
Your methods must have the exact headers that we have specified, or we won’t be able to test them. Note in particular that the case of the letters matters.
-
If you are asked to write a method that returns a value, make sure that it returns the specified value, rather than printing it.
-
You may not use any Java classes that we have not covered in the lecture notes.
-
While you are encouraged to use
Arrays.toString()when testing your methods, you must not use it inside the methods themselves. In addition, you must not use any other methods from theArraysclass. -
Each of your methods should be preceded by a comment that explains what your methods does and what its inputs are. You should also include a comment at the top of the file, similar to the one that we included in
Methods6.javafrom Problem Set 2. -
More generally, use good programming style. Use appropriate indentation, select descriptive variable names, insert blank lines between logical parts of your program, and add comments as necessary to explain what your code does. See the coding conventions for more detail.
The methods
-
Write a method with the header
public static void process(int[] vals)
that takes a reference to an array of integers and modifies its internals as follows:
-
If an element is even, it should be doubled.
-
If an element is odd, it should be negated (i.e., multiplied by -1).
For example, if you add the following test code to your
mainmethod:int[] a1 = {1, 2, 6, 5, -8, -10, -11}; ArrayMethods.process(a1); System.out.println(Arrays.toString(a1));
you should see the following output:
[-1, 4, 12, -5, -16, -20, 11]
Special case: If the parameter is
null, the method should throw anIllegalArgumentException. -
-
Write a method with the header
public static String[] extractBU(String[] words)
that takes a reference to an array of strings, and that creates and returns a new array that contains the strings from the original array whose first letter is either
'b','B','u'or'U'.For example:
String[] a2 = {"Build", "up", "your", "best", "self"}; String[] a3 = ArrayMethods.extractBU(a2); System.out.println(Arrays.toString(a3));
should display:
[Build, up, best]
Important:
-
You may not use any of the built-in methods for copying or extracting a portion of an array.
-
You must not modify the original array.
-
The new array (the one that you will return) should not have any “extra” unused elements. As a result, we recommend making an initial pass through the original array to determine how long the new array should be. Then you can create the new array and make a second pass through the original array to find and copy the appropriate strings.
Special case: If the parameter is
null, the method should throw anIllegalArgumentException. -
-
Write a method with the header
public static void moveToEnd(int[] vals, int n)
that takes a reference to an array of integers
valsand a non-negative integern, and that modifies the internals ofvalsso its firstnelements are moved to the end of the array and the remaining elements are shifted to the beginning.Important: You may not use any of the built-in methods for copying or extracting a portion of an array.
For example:
int[] a4 = {1, 2, 3, 4, 5, 6, 7, 8}; ArrayMethods.moveToEnd(a4, 3); System.out.println(Arrays.toString(a4));
should display:
[4, 5, 6, 7, 8, 1, 2, 3]
Special cases:
-
If the first parameter is
null, ifnis negative, or if the length of the array is less thann, the method should throw anIllegalArgumentException. -
If
nis 0 or the length of the array is equal ton, the method can simply return without making any changes.
Hint: We recommend creating one or more temporary arrays that you can use to store values from the original array.
-
-
Write a method with the header
public static boolean hasXOfAKind(int[] vals, int x)
that takes a reference to an array of integers
valsand an integern, and that determines if there is at least one number that appears at leastxtimes (i.e.,xor more times) invals, returningtrueif there is such a number andfalseotherwise.For example, consider the following arrays:
int[] vals1 = {2, 8, 4, 8, 5, 6, 8}; int[] vals2 = {9, 6, 9, 7, 9, 7, 9, 9};
Given these arrays:
-
The method call
hasXOfAKind(vals1, 3)should returntrue, because there is a number (8) that appears 3 times invals1. Similarly,hasXOfAKind(vals1, 2)should returntrue, because8appears at least 2 times invals1. -
The method call
hasXOfAKind(vals1, 4)should returnfalse, because there is no number that appears 4 or more times invals1. -
The method call
hasXOfAKind(vals2, 5)should returntrue, because there is a number (9) that appears 5 times invals2. Similarly,hasXOfAKind(vals2, 4),hasXOfAKind(vals2, 3), andhasXOfAKind(vals2, 2)should all returntrue, buthasXOfAKind(vals2, 6)should returnfalse.
Special cases:
-
If the first parameter is
nullor the second parameter is less than 1, the method should throw anIllegalArgumentException. -
If the first parameter refers to an array of length 0, the method should return
false. -
If
valshas at least one element andxis1, the method should always returntrue.
Important: You must not modify the original array.
-
-
Write a method with the header
public static int[] combine(int[] vals1, int[] vals2)
that takes two arrays of integers
vals1andvals2as parameters, and that creates and returns a new array in which each element is the sum of the corresponding elements fromvals1andvals2.For example:
int[] a5 = {1, 2, 3, 4}; int[] a6 = {5, 6, 7, 8}; int[] a7 = ArrayMethods.combine(a5, a6); System.out.println(Arrays.toString(a7));
should display:
[6, 8, 10, 12]
If one of the input arrays is longer than the other, its “extra” elements should appear at the end of the new array. For example:
int[] a5 = {1, 2, 3, 4}; // same array as above int[] a8 = {5, 6, 7, 8, 9, 11}; int[] a9 = ArrayMethods.combine(a5, a8); System.out.println(Arrays.toString(a9));
should display:
[6, 8, 10, 12, 9, 11]
Special case: If either parameter is
nullor has a length of 0, the method should throw anIllegalArgumentException.Important: You must not modify the original arrays.
Problem 5: A blueprint class for Card objects
30 points total; individual-only
In this problem, you will create a class called Card that serves as a
blueprint for objects that can be used to represent a single playing
card in a card game.
Each Card object will have two characteristics:
-
a rank (e.g., 5, 8, Ace, or Queen). This will be stored as an integer – even when the card is non-numeric card. For example, we will use a rank of 1 for an Ace card.
-
a suit (e.g., Clubs or Diamonds). This will be stored as a single character. For example, a suit of Clubs will be stored as the character
'C'.
The sections below outline the steps that you should take to implement this class.
The guidelines from Problem 4 also apply here.
Getting started
-
Download the following file: Card.java
Make sure to put it in your
ps3folder. If your browser doesn’t allow you to specify where a file should be saved, try right-clicking on its link above and choosing Save as... or Save link as..., which should produce a dialog box that allows you to choose the correct folder for the file. -
In VS Code, select the File->Open Folder or File->Open menu option, and use the resulting dialog box to find and open the folder that you created for this assignment. (Note: You must open the folder; it is not sufficient to simply open the file.)
The name of the folder should appear in the Explorer pane on the left-hand side of the VS Code window, along with the name of the file that you downloaded.
-
Click on the name
Card.java, which will open an editor window for that file. You will see that we’ve given you the beginnings of the class. -
Read over the starter code in
Card.javabefore continuing — including all of the comments that we have provided.You will note that we have provided several class constants – global variables whose values cannot change. The definition of these variables includes several key components:
-
the keyword
static, which means that the variable belongs to the class as a whole. This distinguishes it from a field, which is a non-static variable that is inside every object of the class. Every object gets its own separate set of the fields, but there is only one copy of astaticvariable, and it is shared by all objects of the class. In addition, it has class-level scope and can be accessed by any method written in the class. -
the keyword
final, which is what makes a variable a constant. The value that we assign to a class constant is its final value, and we cannot assign a new value to it somewhere else. -
the type declaration, like that of any other variable
-
the variable name, which we capitalize so that it will be obvious that it is a constant
-
the initialization of the variable, which we must do here, because we can’t change a class constant outside of its definition.
-
Your tasks
-
Although the rank of a card will be stored as an integer, we will sometimes need a string representation of the rank. For example, when printing a Queen (which has a rank of 12), we will use the string
"Q"instead of the integer 12. This is why we have provided you with theRANK_STRINGSarray, which is a class constant that contains string representations of all of the card ranks.As the comments that we’ve included above the array indicate, if a card has a rank of
r, the corresponding rank string can be obtained using the expressionRANK_STRINGS[r]. For example, 12 is the numeric rank of a Queen card, soRANK_STRINGS[12]gives us the string"Q".Your first task is to add a static method called
rankNumForthat goes in the reverse direction – taking a rank string as its only parameter and returning the corresponding integer rank. In other words, the method should find and return the index of the specified rank string in theRANK_STRINGSarray. For example:rankNumFor("Q")should return12, because"Q"has an index of12in theRANK_STRINGSarray.rankNumFor("A")should return1, because"A"has an index of1in theRANK_STRINGSarray.rankNumFor("10")should return10, because"10"has an index of10in theRANK_STRINGSarray.
If the value of the parameter is
nullor if it doesn’t appear in theRANK_STRINGSarray, the method should return -1. For example,rankNumFor("B")should return-1.Important notes:
-
Because the smallest possible rank is 1, we have put an empty string in position 0 of the
RANK_STRINGSarray. When processing the array, you should ignore position 0. -
Your method should work even if we change the contents of the
RANK_STRINGSarray. In other words, you should write code that processes any array ofStringobjects that is assigned to the class constantRANK_STRINGS. The only assumption you should make about the array is that element 0 will be an empty string.
This method (unlike most of the others you will write for this problem) is static, because it doesn’t need to access the fields of a
Cardobject. Rather, it gets all of the information that it needs from its parameter and from theRANK_STRINGSarray. -
Add another static method called
isValidSuitthat takes a single-character representation of a card’s suit and returnstrueif that suit is valid (i.e., if it is one of the values in theSUITSarray), andfalseotherwise. For example:isValidSuit('D')should returntrue, because'D'appears in theSUITSarrayisValidSuit('B')should returnfalse, because'B'does not appear in theSUITSarray.
Important: Your method should work even if we change the contents of the
SUITSarray. In other words, you should write code that processes any array ofcharvalues that is assigned to the class constantSUITS, and you should not make any assumptions about thechars found in that array.Here again, this method is static, because it doesn’t need to access the fields of a
Cardobject. Rather, it gets all of the information that it needs from its parameter and theSUITSarray.Testing your static methods
Before you proceed with the remaining steps, we highly recommend adding amainmethod to yourCardclass that makes test calls to the two static methods that you just implemented. Take whatever steps are needed to ensure that they work correctly. -
Define the fields
EachCardobject should encapsulate two pieces of state:-
the card’s rank (an integer). For numeric cards, the rank is simply the number itself (e.g., 5 cards have a rank of 5). Aces have a rank of 1, Jacks a rank of 11, Queens a rank of 12, and Kings a rank of 13. Ranks less than 1 or greater than 13 will not be allowed.
-
the card’s suit (a single character). The only allowable suits are the ones found in the
SUITSarray.
For example, here is what a
Cardobject representing an Ace of Hearts would look like in memory:+----------------+ | +-----+ | | rank | 1 | | | +-----+ | | +-----+ | | suit | 'H' | | | +-----+ | +----------------+
For now, you only need to define the fields. Make sure to:
-
use the field names shown above
-
prevent them from being directly accessed by client code.
In subsequent sections, you will write constructors that assign values to the fields, and that ensure that only valid values are allowed.
-
-
Implement the constructors
In Java, a class can have more than one constructor; this provides clients of the class with different options for creating a new object of the class. All of the constructors must be named after the class, but their parameter lists must differ from each other in some way.You should add the following two constructors to the
Cardclass:-
a constructor that takes two parameters: an integer specifying the card’s rank, and a single character (a
char) specifying the card’s suit (in that order). It should ensure that only valid values are assigned to the object’s fields, as specified in part 3. Attempts to assign invalid values should produce anIllegalArgumentException. -
a constructor that takes a single parameter: a two- or three-character string that specifies the card to be created. For example, the client could pass in the string
"KC"for a King of Clubs or the string"10S"for a 10 of Spades.The last character of the input string represents the card’s suit (e.g.,
'C'or'S') and the first one or two characters represent its rank (e.g., “K” or “10”).This constructor should use the specified string to determine the field values of the new object, and it should ensure that only valid values are assigned to the fields. Attempts to assign invalid values should produce an
IllegalArgumentException. In addition, a parameter value ofnullshould produce anIllegalArgumentException.
For example, here is some code that uses the two constructors:
Card c1 = new Card(1, 'H'); // Ace of Hearts Card c2 = new Card(12, 'D'); // Queen of Diamonds Card c3 = new Card("KC"); // King of Clubs Card c4 = new Card("10S"); // 10 of Spades
Hint: Your constructors should take advantage of the static methods that you wrote for parts 1 and 2.
-
-
Implement some basic accessor methods
Next, define the following initial set of instance methods. (Note that all of these methods are accessor methods. We will not implement any mutator methods, because we assume that a givenCardobject’s rank and suit remain fixed once the object is created. The constructor will take care of assigning the initial values of those fields, and those values will not change.)Make sure that your methods are non-static, because they need access to the fields in the called object. In addition, none of these methods should take an explicit parameter, because all of the information that they need is inside the called object.
Here are the methods:
-
getRank, which returns the integer representing theCardobject’s rank. For example, ifc1is the card shown in the diagram above,c1.getRank()should return 1. -
getSuit, which returns thecharrepresenting theCardobject’s suit. For example, ifc1is the card shown in the diagram above,c1.getSuit()should return thechar'H'. -
isAce, which returnstrueif theCardis an Ace andfalseif it is not. To make your code more readable, we encourage you to use the class constant that we have provided for the rank of an Ace. The relevant constant is at the very beginning of the class. -
isFaceCard, which returnstrueif theCardis a face card (Jack, Queen, or King) and andfalseif it is not. To make your code more readable, we encourage you to use the class constants that we have provided for the ranks of Jack, Queen and King. -
getValue, which returns theCardobject’s value. If the card is a face card, then it should return a value of 10. Otherwise, it should return the card’s rank. Hint: Use theisFaceCard()method to check if it is a face card!
Once you have completed your methods for parts 4 and 5, you can test them using the first client program that we’ve given you. See below for more detail.
-
-
Define a
toStringmethod
Write atoStringmethod that returns aStringrepresentation of theCardobject that can be used when printing it or concatenating it to aString. We will discuss this type of method in lecture, and we provide an example in ourRectangleclass. The returnedStringshould consist of the card’s rank string followed immediately by its suit. For example, if aCardobject represents a 10 of Diamonds, this method should return"10D". If aCardobject represents a Queen of Spades, this method should return"QS". This method must make use of theRANK_STRINGSarray that we have given you. -
Define methods for comparing
Cardobjects
Finally, define the following two instance methods for comparingCardobjects:-
sameSuitAs, which takes aCardobject as a parameter and determines if it is has the same suit as the called object, returningtrueif they have the same suit andfalseif they do not have the same suit. If a value ofnullis passed in for the parameter, the method should returnfalse. -
equals, which takes aCardobject as a parameter and determines if it is equivalent to the called object, returningtrueif it is equivalent andfalseif it is not equivalent. TwoCardobjects should be considered equivalent if their rank and suit are the same. Ifnullis passed in for the parameter, the method should returnfalse.
-
Client programs and testing your code
To help you in testing your Card class, we have created two sample
client programs:
-
- make sure to save it in your
ps3folder - tests the methods from parts 4 and 5
- You should only download it and try to run it after completion of part 5.
- If you get compiler errors in
Client1.java, check your method headers inCard.java. - expected output is given here
- make sure to save it in your
-
- make sure to save it in your
ps3folder - tests the methods from parts 6 and 7
- You should only download it and try to run it after completion of part 7.
- If you get compiler errors in
Client2.java, check your method headers inCard.java. - expected output is given here
- make sure to save it in your
In addition to using the client programs, we recommend that you
perform additional testing on your own. You can do so by adding code
to one of the clients, or by adding test calls to a main method of
the Card class.
If you are unable to get a given method to compile, you should comment out the body of the method (keeping the method header and possibly a dummy return value) so that we’ll still be able to test your other methods.
Submitting your work for Part II
Note: There are separate instructions at the end of Part I that you should use when submitting your work for that part of the assignment.
Pair-optional problem
If you chose to work on Problem 4 with a partner, both you and your partner should submit your own copy of your joint work, along with your individual work on Problem 5.
You should submit only the following files:
ArrayMethods.javaCard.java
Make sure that you do not try to submit a .class file or a file
with a ~ character at the end of its name.
Here are the steps:
-
Login to Gradescope as needed by clicking the link in the left-hand navigation bar, and then click on the box for CS 112.
-
Click on the name of the assignment (
PS 3: Part II) in the list of assignments. You should see a pop-up window with a box labeled DRAG & DROP. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.) -
Add your files to the box labeled DRAG & DROP. You can either drag and drop the files from their folder into the box, or you can click on the box itself and browse for the files.
-
Click the Upload button.
-
You should see a box saying that your submission was successful. Click the
(x)button to close that box. -
The Autograder will perform some tests on your files. Once it is done, check the results to ensure that the tests were passed. If one or more of the tests did not pass, the name of that test will be in red, and there should be a message describing the failure. Based on those messages, make any necessary changes. Feel free to ask a staff member for help.
Note: You will not see a complete Autograder score when you submit. That is because additional tests for at least some of the problems will be run later, after the final deadline for the submission has passed. For such problems, it is important to realize that passing all of the initial tests does not necessarily mean that you will ultimately get full credit on the problem. You should always run your own tests to convince yourself that the logic of your solutions is correct.
-
If needed, use the Resubmit button at the bottom of the page to resubmit your work. Important: Every time that you make a submission, you should submit all of the files for that Gradescope assignment, even if some of them have not changed since your last submission.
-
Near the top of the page, click on the box labeled Code. Then click on the name of each file to view its contents. Check to make sure that the files contain the code that you want us to grade.
Important
-
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
-
If you are unable to access Gradescope and there is enough time to do so, wait an hour or two and then try again. If you are unable to submit and it is close to the deadline, email your homework before the deadline to
cs112-staff@cs.bu.edu