CS 112
Spring 2023

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

  1. If you haven’t already created a folder named cs112 for your work in this course, follow these instructions to do so.

  2. Then create a subfolder called ps3 within your cs112 folder, 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:

  1. Access the template that we have created by clicking on this link and signing into your Google account as needed.

  2. When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.

  3. Select File->Rename, and change the name of the file to ps3_partI.

  4. Add your work for the problems from Part I to this file.

  5. Once you have completed all of these problems, choose File->Download->PDF document, and save the PDF file in your ps3 folder. 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.)

  1. Consider a potential instance method named tallerThanWide that would return true if a Rectangle object has a height that is larger than its width, and false otherwise.

    1. (1 point) What type of instance method would tallerThanWide be, an accessor or a mutator?
    2. (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.
  2. Now consider a potential instance method named adjust that would take two integers specifying a width and height and adjust the dimensions of a Rectangle as needed so that its dimensions are at least as big as the ones specified by the parameters. For example, if a Rectangle‘s dimensions are 100 x 50, then calling the adjust method with parameters of 80 and 70 would leave the Rectangle‘s width alone (because 100 is already greater than 80), but change its height to 70 (because 50 is smaller than 70).

    1. (1 point) What type of instance method would adjust be, an accessor or a mutator?
    2. (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.
  3. Consider the following client code — i.e., code from another class that uses a Rectangle object:

    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 Rectangle class employs appropriate encapsulation, this code fragment will not compile.

    1. (2 points) Explain what problems are present in the code fragment that prevent it from compiling.
    2. (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);
    }
}
  1. (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 a StreetAddress object 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 num is always greater than 0, and that name is no more than 30 characters long. Any attempt to assign an invalid value should produce an IllegalArgumentException.
    • a constructor that initializes the fields of a newly created StreetAddress object to the values specified by the parameters of the constructor. Any attempt to specify an invalid value should produce an IllegalArgumentException. 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.

  2. (4 points) Now imagine that you’re writing client code for your revised StreetAddress class – i.e., code in a different class that uses StreetAddress objects.

    For each of the following tasks, write a single line of client code to accomplish the task:

    1. Construct a StreetAddress object in which the number is 650 and the name is "Commonwealth Avenue" and assign it to a properly declared variable named ccds (short for “Center for Computing and Data Sciences”, the new home of the CS department!).

    2. Your friend points out that the correct street number for the new building is 665, not 650. Change the value of the num field in the object that you created in part (a), giving it a value of 665. 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 StreetAddress object. As a result, you will need to make a method call to change the appropriate field.

    3. Get the name component of the StreetAddress object that you created in part (a), and assign it to a properly declared variable named streetName. Here again, you will need to make an appropriate method call.

    4. Now imagine that you are given a second StreetAddress object that has been created by someone else and has been assigned to the variable addr. Change the value of the num field in addr, 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:

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!

  1. (5 points) Imagine that after creating the version of the Card class that is described in Part II, we decide that:

    • We want to keep track of how many Card objects have been created for each of the four suits (Clubs, Diamonds, Hearts, and Spades).

    • In addition to its rank and suit, we want each Card object to store its value (an integer like 4 or 11). Note that the value of a Card may 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 Card class 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 rank that 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.

  2. (6 points) The version of the Card class 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 existing Card object. Imagine that we want to add a mutator method called setSuit that takes in a char and uses it to change the suit of the called Card object. For example, you might change the suit of a Card from 'D' (for Diamonds) to 'C' (for Clubs).

    1. What type of method should setSuit be — static or non-static? Explain briefly.

    2. Assume we have a Card object whose current suit is 'H' (i.e., it has the suit character that corresponds to Hearts). We decide to change the suit of the Card to 'S' (for Spades) by making the appropriate call to our new setSuit method. 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.

    3. Give an example of how you would call this method from outside the Card class. If you need a Card object to call the method, assume that the variable c represents that object, and that the object has already been created. However, you should only use c if doing so is absolutely necessary.

  3. (3 points) Finally, let’s say that we want to add another method called numFaceCards. It takes a parameter that represents an array of Card objects, and it computes and returns the number of face cards in that array.

    1. What type of method should numFaceCards be — static or non-static? Explain briefly.

    2. Give an example of how you would call this method from outside the Card class to determine the number of face cards in an array of Card objects called myCards. If you need a Card object to call the method, assume that the variable c represents that object, and that the object has already been created. However, you should only use c if 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:

  1. If you still need to create a PDF file, open your ps3_partI file on Google Drive, choose File->Download->PDF document, and save the PDF file in your ps3 folder.

  2. Login to Gradescope by clicking the link in the left-hand navigation bar, and click on the box for CS 112.

  3. 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.)

  4. 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.

  5. 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.

  6. 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

  1. If you haven’t already done so, create a folder named ps3 for your work on this assignment. You can find instructions for doing so here.

  2. 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.

  3. Select File->New File, which will open up an empty editor window.

  4. Select File->Save, and give the new file the name ArrayMethods.java.

  5. In the new file, create a class called ArrayMethods. In that class, take the steps described below.

  6. In addition, we highly recommend adding a main method to your class that makes test calls to your methods. Don’t forget to import java.util so that you can use the Arrays.toString method 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 the Arrays class.

  • 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.java from 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

  1. 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 main method:

    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 an IllegalArgumentException.

  2. 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 an IllegalArgumentException.

  3. Write a method with the header

    public static void moveToEnd(int[] vals, int n)
    

    that takes a reference to an array of integers vals and a non-negative integer n, and that modifies the internals of vals so its first n elements 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, if n is negative, or if the length of the array is less than n, the method should throw an IllegalArgumentException.

    • If n is 0 or the length of the array is equal to n, 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.

  4. Write a method with the header

    public static boolean hasXOfAKind(int[] vals, int x)
    

    that takes a reference to an array of integers vals and an integer n, and that determines if there is at least one number that appears at least x times (i.e., x or more times) in vals, returning true if there is such a number and false otherwise.

    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 return true, because there is a number (8) that appears 3 times in vals1. Similarly, hasXOfAKind(vals1, 2) should return true, because 8 appears at least 2 times in vals1.

    • The method call hasXOfAKind(vals1, 4) should return false, because there is no number that appears 4 or more times in vals1.

    • The method call hasXOfAKind(vals2, 5) should return true, because there is a number (9) that appears 5 times in vals2. Similarly, hasXOfAKind(vals2, 4), hasXOfAKind(vals2, 3), and hasXOfAKind(vals2, 2) should all return true, but hasXOfAKind(vals2, 6) should return false.

    Special cases:

    • If the first parameter is null or the second parameter is less than 1, the method should throw an IllegalArgumentException.

    • If the first parameter refers to an array of length 0, the method should return false.

    • If vals has at least one element and x is 1, the method should always return true.

    Important: You must not modify the original array.

  5. Write a method with the header

    public static int[] combine(int[] vals1, int[] vals2)
    

    that takes two arrays of integers vals1 and vals2 as parameters, and that creates and returns a new array in which each element is the sum of the corresponding elements from vals1 and vals2.

    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 null or has a length of 0, the method should throw an IllegalArgumentException.

    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:

The sections below outline the steps that you should take to implement this class.

The guidelines from Problem 4 also apply here.

Getting started

  1. Download the following file: Card.java

    Make sure to put it in your ps3 folder. 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.

  2. 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.

  3. 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.

  4. Read over the starter code in Card.java before 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 a static variable, 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

  1. 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 the RANK_STRINGS array, 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 expression RANK_STRINGS[r]. For example, 12 is the numeric rank of a Queen card, so RANK_STRINGS[12] gives us the string "Q".

    Your first task is to add a static method called rankNumFor that 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 the RANK_STRINGS array. For example:

    • rankNumFor("Q") should return 12, because "Q" has an index of 12 in the RANK_STRINGS array.
    • rankNumFor("A") should return 1, because "A" has an index of 1 in the RANK_STRINGS array.
    • rankNumFor("10") should return 10, because "10" has an index of 10 in the RANK_STRINGS array.

    If the value of the parameter is null or if it doesn’t appear in the RANK_STRINGS array, 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_STRINGS array. When processing the array, you should ignore position 0.

    • Your method should work even if we change the contents of the RANK_STRINGS array. In other words, you should write code that processes any array of String objects that is assigned to the class constant RANK_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 Card object. Rather, it gets all of the information that it needs from its parameter and from the RANK_STRINGS array.

  2. Add another static method called isValidSuit that takes a single-character representation of a card’s suit and returns true if that suit is valid (i.e., if it is one of the values in the SUITS array), and false otherwise. For example:

    • isValidSuit('D') should return true, because 'D' appears in the SUITS array
    • isValidSuit('B') should return false, because 'B' does not appear in the SUITS array.

    Important: Your method should work even if we change the contents of the SUITS array. In other words, you should write code that processes any array of char values that is assigned to the class constant SUITS, and you should not make any assumptions about the chars found in that array.

    Here again, this method is static, because it doesn’t need to access the fields of a Card object. Rather, it gets all of the information that it needs from its parameter and the SUITS array.

    Testing your static methods
    Before you proceed with the remaining steps, we highly recommend adding a main method to your Card class that makes test calls to the two static methods that you just implemented. Take whatever steps are needed to ensure that they work correctly.

  3. Define the fields
    Each Card object 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 SUITS array.

    For example, here is what a Card object 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.

  4. 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 Card class:

    • 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 an IllegalArgumentException.

    • 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 of null should produce an IllegalArgumentException.

    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.

  5. 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 given Card object’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 the Card object’s rank. For example, if c1 is the card shown in the diagram above, c1.getRank() should return 1.

    • getSuit, which returns the char representing the Card object’s suit. For example, if c1 is the card shown in the diagram above, c1.getSuit() should return the char 'H'.

    • isAce, which returns true if the Card is an Ace and false if 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 returns true if the Card is a face card (Jack, Queen, or King) and and false if 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 the Card object’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 the isFaceCard() 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.

  6. Define a toString method
    Write a toString method that returns a String representation of the Card object that can be used when printing it or concatenating it to a String. We will discuss this type of method in lecture, and we provide an example in our Rectangle class. The returned String should consist of the card’s rank string followed immediately by its suit. For example, if a Card object represents a 10 of Diamonds, this method should return "10D". If a Card object represents a Queen of Spades, this method should return "QS". This method must make use of the RANK_STRINGS array that we have given you.

  7. Define methods for comparing Card objects
    Finally, define the following two instance methods for comparing Card objects:

    • sameSuitAs, which takes a Card object as a parameter and determines if it is has the same suit as the called object, returning true if they have the same suit and false if they do not have the same suit. If a value of null is passed in for the parameter, the method should return false.

    • equals, which takes a Card object as a parameter and determines if it is equivalent to the called object, returning true if it is equivalent and false if it is not equivalent. Two Card objects should be considered equivalent if their rank and suit are the same. If null is passed in for the parameter, the method should return false.

Client programs and testing your code
To help you in testing your Card class, we have created two sample client programs:

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:

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:

  1. Login to Gradescope as needed by clicking the link in the left-hand navigation bar, and then click on the box for CS 112.

  2. 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.)

  3. 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.

  4. Click the Upload button.

  5. You should see a box saying that your submission was successful. Click the (x) button to close that box.

  6. 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.

  7. 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.

  8. 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