Make sure to create a subfolder called
lab5 within your cs112 folder, and put all of the files for this
lab in that folder.
Follow the instructions of your TA to write a logically organized class to create a Trading Game Class game!
You are welcome to collaborate and work in pairs, but you must individually upload your (paper) code or psuedocode onto the Gradescope portal by the end of lab!
Have Fun!!
As we discussed in lecture, a class can extend another class. Let’s consider another example of this together.
Begin by downloading these files, making sure to save
them all in your lab5 folder:
In VS Code, open your lab5 folder using File->Open Folder,
and then click on the name of each file in the Explorer Pane to open
an editor window for it.
Review each file and take note of how they are related.
Note: The Cat class will not compile until we first make some
changes to it, so don’t try to compile and run anything yet!
Which class is the superclass? Which is the subclass? What does it
mean that the Cat class extends the Animal class?
The Cat class cannot directly access the fields it inherits from
Animal. Why not?
The subclass constructor typically calls the superclass
constructor to initialize the inherited fields. Write a
constructor for the Cat class above. It should take as
parameters the cat’s name and a boolean indicating whether it is
short-haired, and it should call the superclass constructor to
initialize the inherited fields.
Update the test program to create an instance of class Cat.
Cat c = new Cat("Kitty", false);
To manipulate the inherited fields, the subclass can use the
inherited accessor and mutator methods. Write a toString method
for the Cat class above. It should return a string consisting of
the cat’s name followed by either " (short-haired)" or
" (long-haired)".
Update the test program to test your method:
System.out.println(c);
The subclass can override an inherited method, replacing it with a
version that is more appropriate. Write an isSleeping
method for the Cat class. It should reflect the fact that cats seem to
sleep all of the time!
Update the test program to test your method.
Let’s say that we now want to define a class called Abyssinian
for cats that belong to that particular breed of short-haired
cat. Which class should it extend?
Go ahead and create a class named Abyssinian, defining it
so that it extends the correct class. Use File->New File to create
the necessary file for it, and save it using the correct name.
Abyssinian should not have any new fields of its own. However,
it should include:
a constructor that takes only a name, and that calls the superclass constructor to initialize the inherited fields. When making that call, make sure that it reflects the fact that Abyssinians are short-haired.
an isExtroverted() method that overrides the inherited
version and replaces it with one that reflects the fact that
Abyssinian cats are known to be extroverted.
Once your class is created, go ahead and test out the methods in your main program.
Another possible class for this hierarchy of animals is the Dog
class, which you should examine now,
although you don’t need to open it in your IDE. In addition to its
inherited fields and methods, it has a boolean field isSmall,
and methods isSmall() and bark().
Let’s say that we have created an Abyssinian object and assigned
it to the variable a:
Abyssinian a = new Abyssinian("Abby");
For each of the following method calls:
Indicate whether it will compile. Because the variable a is
declared to be of type Abyssinian, a method call using a
will compile if there is a corresponding method inside
Abyssinian objects – either defined in the Abyssinian
class itself or inherited from a superclass. A method call
will not compile if there is no corresponding method in
objects of that class.
If the method call will compile, specify which version of the method will be called. In other words, in which class can we find the version of the method that will be called?
Here are the calls to test:
a.getNumLegs()
a.isExtroverted()
a.isSleeping(12, 30)
a.isSmall()
a.toString()
a.equals(a)
You will notice a static method defined in the Animal class
named printAnimalName. How can you call this method in your
main program to print out the names of all the animal objects
you have created? Note that it is a static method. How does
this differ from the other methods of the class?
Update the test program to test this method.
Your work for this task should go on the piece of paper that we give you. Please show your paper to a staff member before you leave the lab.
Thanks to a feature of Java called polymorphism, we can do something like this:
ClassA myObject = new ClassB(...);
Where ClassB extends ClassA, or equivalently, ClassB is a subclass
of ClassA. Specifying a more general type for myObject than the
actual type of the object can be useful when writing a
method that needs to take more than one type of object as a parameter,
or when creating an array of objects of different but related types.
For example, if we wanted to have an array containing different types of animal objects, we could define the array as follows:
Animal[] zoo = new Animal[10];
Then, any element of the array could be of type Animal or any subclass
of Animal. In other words, this would be allowed:
zoo[0] = new Dog(...); zoo[1] = new Cat(...); zoo[2] = new Abyssinian(...);
Consider the following class headers:
public class A extends B {
...
}
public class B extends C {
...
}
public class C {
...
}
public class D extends C {
...
}
Draw an inheritance hierarchy for these classes.
Which of these assignments would be allowed, taking into account the rules of polymorphism?
B myObj = new A();B myObj = new C();C myObj = new A();A myObj = new B();D myObj = new B();Follow the instructions of your TA to write a logically organized class to create a Trading Game Class game!
You are welcome to collaborate and work in pairs, but you must individually upload your (paper) code or psuedocode onto the Gradescope portal by the end of lab!
Have Fun!!
Last updated on February 19, 2026.