S-111
  • Home
  • Lectures
  • Problem Sets
  • Sections
  • Syllabus
  • Schedule
  • Staff
  • Resources
  • Canvas
  • Ed Discussion
  • Gradescope

Problem Set 5 FAQ

  1. When I compile my code, I’m getting an error that begins “cannot make a static reference to the non-static method...” Do you know why I would be getting this?

    Don’t forget that non-static methods need a calling 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. Instead, you need to do one of the following:

    • Put a calling object before the dot – the object that you want to operate on. For example, if c is the Card object whose value you need, you would write c.getValue().
    • If the line you are writing is part of another non-static method, and the method you are calling will operate on the same object as the current method, you can make the method call without dot notation. For example, inside a non-static method in the Card class, it would be possible to call the getValue() method by just writing getValue(). However, you could also use the implicit parameter in such cases: this.getValue().
  2. For Task 2 and/or Task 3, the compiler says that Scanner cannot be resolved to a type. What am I doing wrong?

    You need to import java.util at the top of the file.

  3. For Task 2, I’m having trouble getting my getHandValue() method to value the Aces correctly. For example, in the last round of the first sample run, the second card in the user’s hand is an Ace of Spades. The first valuation has it with a value of 11, and the second valuation has it with a value of 1. My code always gives it a value of 11. Do you have any suggestions?

    There is a hint for how to handle the Aces in the description of getHandValue() – see Task 2, part 7. Take another look at that hint, and see if you can figure out how to fix your logic. It might also help to think through the differences between the two hands that you mention from the sample run – the hand where the Ace has a value of 11, and the hand where the Ace has a value of 1 – and make sure that your logic is able to detect and act on those differences. Trace through your code on each of those hands, and see where your current logic is falling short. And finally, here’s one other hint: You can’t know for sure whether one of your Aces can be valued at 11 until you’ve seen all of the cards in the hand!

  4. For Task 2, part 11, I’d like to use the constructor within my discardCards method. Is there a way to do this?

    No. Once an object has been created, you can’t “re-construct” it using the constructor, because the constructor is only used to create a new object. As a result, your discardCards() method will need to perform the necessary changes itself.

  5. For Task 3, I’m having trouble writing the constructor. Do you have any suggestions?

    You may find it helpful to look at two of our vehicle classes as a model. For example, the Taxi class extends the Automobile class. It inherits most of its fields, but it has one new field (taxiID). Take a look at how the Taxi constructor works – how it uses the superclass constructor to take care of the inherited fields, and how it takes care of the taxiID by itself. You will need to take a similar approach in your Dealer class.

    Also, don’t forget that the constructor in the superclass (the Player class) takes one parameter. Thus, when you call that constructor at the start of your subclass constructor, you will need to specify a value for that one parameter.

  6. For Task 3, I have completed a partial implementation of my Dealer class, and I’m getting a compiler error that begins “Implicit super constructor Player() is undefined...” I don’t understand what I’m doing wrong.

    There are two possible explanations for this error:

    1. You haven’t defined a constructor for Dealer yet. As a result, the compiler is trying to create a default constructor that begins with a call to a no-parameter constructor of the superclass. And because Player doesn’t have a no-parameter contructor, the compiler issues an error.

    2. You aleady have a constructor for Dealer, but you forgot to begin it with a call to the superclass constructor. If you omit this call, the compiler tries to add a call to a no-parameter constructor of the superclass. And because Player doesn’t have a no-parameter contructor, the compiler issues an error.

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

  7. For Task 3, I’m overriding one of the inherited methods. Inside the method, I’m trying to use one of the inherited fields, but the compiler is giving me an error message. How can I access the field?

    Because the inherited field is declared to be private in the superclass, you can’t access it directly in the subclass. Instead, you need to use one of the methods provided in the superclass. For example, to access the number of cards, you can use the getNumCards method.

  8. For Task 3, I’ve implemented one of the Dealer methods that overrides a method inherited from the 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 Blackjack.java that creates the dealer, modifying it as instructed in the problem set 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 you will be able to take advantage of polymorphism and dynamic binding, 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.

Last updated on July 14, 2026.