In problem 4, how can the Limousine
constructor check for bad
values for the inherited fields if it needs to call the superclass
constructor on the very first line?
If your Limousine
constructor calls the superclass constructor,
that other constructor will take care of the necessary
error-checking of the values passed in for the inherited fields,
and thus the Limousine
constructor doesn’t need to perform any
error-checking on those fields. (Note: The Vehicle
class from
lecture doesn’t actually perform any error-checking on things like
the make and the model, but you don’t need to try to correct for
this failing.)
When I compile my code for Part II, 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.
Instead, you need to do one of the following:
c
is the Card
object whose
value you need, you would write c.getValue()
.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()
.For Task 2 and/or Task 3, the compiler says that it can’t find the
class Scanner
. What am I doing wrong?
You need to import java.util
at the top of the file.
Last updated on July 14, 2025.