CS 112
Spring 2018

Old version

This is the CS 112 site as it appeared on May 11, 2018.

Problem Set 3 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

Problem 1

  1. When drawing my memory diagrams, what happens if an object/array on the heap ends up with no remaining variables pointing to it?

    Once there are no more references to an object/array, you can omit it from your memory diagram.

Problem 4

  1. When I compile my code, I’m getting an error that says something like “non-static method cannot be referenced from a static context.” Do you know why I would be getting this?

    Don’t forget that non-static methods need a called object – i.e., an object that comes before the dot when you call the method.

    When we call a static method from another class, we put the class name before the dot. However, this does not work for non-static methods. For example, you cannot write something like Card.getValue(), because getValue() is a non-static method.

  2. For Task 2, I’m not sure how to make sure that the getPlay() method obtains a valid input from the user. Any suggestions?

    The main strategy here is to continously ask the user for a input until you get something that is either -1 or a valid index corresponding to a card in the player’s hand. What type of loop would be useful for this purpose?

  3. For Task 3, I have completed a partial implementation of my ComputerPlayer class. When I try to compile it, I get a compiler error that says “cannot find symbol: constructor Player()”. I don’t understand what I’m doing wrong.

    There are two possible explanations for this error:

    • You haven’t defined a constructor for ComputerPlayer yet.

    • You aleady have a constructor for ComputerPlayer, but you forgot to begin it with a call to the superclass constructor.

    Once you have a ComputerPlayer constructor that begins with a call to the superclass constructor, this error should go away.

  4. For Task 3, I’ve implemented one of the ComputerPlayer methods that overrides the method inherited from Player class, but it’s not being called when I run the program. Instead, the superclass version of the method is still being called. What am I doing wrong?

    First, make sure that you have changed the line in the Buno constructor that creates the computer player (players[1]), modifying it so that it uses the constructor from your new subclass instead of the Player constructor.

    If this still doesn’t fix things, check the header of your new method. In order for it to be called, it must have the same parameters as the method inherited from the superclass. That way, you will override the inherited method, and the code that we have given you will call your new method as intended.

    If you give your new method a different parameter list, the method will not override the inherited method. Rather, it will overload that method, and it will not be called by the code that we have given you.

  5. I’ve completed all of the tasks, and my Buno program compiles, but I’m getting a NullPointerException when I run the program. Do you have any idea of what might be wrong?

    Check to see if the one or both of your versions of the getPlay method (the Player version and/or the ComputerPlayer version) could be returning a number that doesn’t correspond to an actual card. If so, that would explain the exception that you’re seeing.

    Also, don’t forget that the backtrace that accompanies the exception tells you the line number of the line that threw the exception.

  6. I’ve completed all of the tasks, and my Buno program compiles, but the program gets stuck in an infinite loop that repeatedly displays the “invalid play...” message. What am I doing wrong?

    It sounds like your ComputerPlayer version of the getPlay method has faulty logic and is returning the number of a card that doesn’t match the card at the top of the discard pile. Because our Buno code repeatedly calls getPlay until it gets a valid play, you end up with an infinite loop.

  7. If I perform several runs of my program, the output eventually gets extremely slow. Am I doing something wrong?

    No. That is a known problem with using DrJava to run programs that involve lots of output. Try closing DrJava and restarting it again.

Problem 6

  1. In one of my methods for problem 6, I’m getting a StringIndexOutOfBoundsException. What am I doing wrong?

    There are several reasons this might be happening:

    • You are using the substring method with invalid index values.
    • You are failing to correctly detect the case in which the string is empty, because you are doing something like this:

      if (str == null || str == "") {
       ...
      

      Because str is a string, you need to use the equals method instead of ==, as shown below:

      if (str == null || str.equals("")) {
       ...
      
    • You are attempting to extract a character before checking for the case in which the string is null or empty. You should be sure to handle those cases first – at the very start of the method.

  2. In one of my methods for problem 6, I’m getting a NullPointerException, even though I am checking for the case in which str equals null. What am I doing wrong?

    Make sure that the first thing that your method checks for is a null value. You need to check for and handle it by doing something like this:

       if (str == null) {
           return 0;
       }
    

    although the actual return value will depend on which method you’re writing.

  3. I’m having trouble getting one of my methods for problem 6 to work correctly. Do you have any suggestions?

    Try tracing through some concrete examples of cases in which the your method is not returning the correct value. You might want to try adding some temporary printlns to see if that helps you to diagnose the problem. In particular, you could print what the method will return before it actually returns it. That will allow you to see when the wrong value is being returned.

    It can also help to write out the sequence of recursive calls that would be made for one of more concrete cases. For example:

    numOccur("banana", 'a')
       numOccur("anana", 'a')
          numOccur("nana", 'a')
             numOccur("ana", 'a')
                numOccur("na", 'a')
                   numOccur("a", 'a')
                      numOccur("", 'a')
    

    Then, once you have the sequence of method calls, you should think about what each of these separate method calls should return, treating them as if they were independent of each other. For example, what should numOccur("ana", 'a') return – i.e., how many times does 'a' appear in "ana"? Based on these return values, you should be able to figure out how a given invocation of the method should use the return value from the recursive call to form its own return value.