Landing : Athabascau University

assignment2: Revision

Last updated December 3, 2022 - 2:24am by Yuyang Liu

Reflections

Problem 1:

This question is about class and inheritance, it contains a super class animal and four subclasses, pig, sheep, duck, and cow. In each subclass, the sound() method should print the sound made by the child class animal. Nothing is particularly hard for this implementation. For the main() method, I choose to implement it inside the animal class.

Another class is AnimalTest that reads the command line user input and creates the appropriate Animal child class based on the type of animal, and then calls the animal’s sound() method. I use numbers to represent different classes, for example 1 for pig ... 5 for quit. A switch statement is used here to separate different cases.

The tricky thing here is that option 5 is used as quit, so it needs to be the handled differently from other options.

 

Problem 2:

This problem relatively easier. I just created a book class and add the attributes including: title, ISBN number, author, edition, publisher, and year of publication. What I leaned here is that attributes are typically private and getters and setters are public for accessing the attributes. In the main method, I create several books, set their attributes by using setters and print the attributes to the console.

 

Problem 3:

This problem includes a class called Elevator, what is special here is that it has two constructors. In the default constructor, I initialize the number of floors to 5. Elevator also uses a constructor to initialize the number of floors (N) in the building when the object is instantiated.

The finalize() method that satisfies this termination condition and verifies the condition is a little bit confusing for me at the beginning. I thought I need create a method named finalize. However when I investigate “garbage collection” in java, I found that finalize() is a provided method and here we need to override it. What the method does is to clean up the objects created. I override the method accordingly by setting the current floor to 1, and then use super().finalize() to clean up the object.

 

 

 

Problem 4:

This problem is similar to the problem1 when creating multiple subclasses. I have a super class Rodent and four child classes: mouse, gerbil, hamster, and guinea pig. In my implementation, there are four behaviors: eat, sleep, move, groom. I choose eating as the behavior that varies by child class. For example, hamster eats peanuts whereas gerbil eats lettuce. The other behaviors are kept the same as the parent class.

 

Problem 5

This problem is more complex than the previous ones. Three methods are included in the base class Shape, area() which calculates the area, circumference() which calculates the circumference, and display() that displays the information of the specific shape. Since these methods need to be overloaded in each child class. The method signature needs to be the same. The return type should be double for the area() and circumference(). However, there’s nothing to be returned in the base class Shape for these methods. So, I decided to return a 0 because I have to return something.

 

Error-checking is added to the constructors, such that they print a warning if the arguments do not conform to the appropriate shape. For triangle, sum of two sides needs to be larger than the other side. For rectangle, length of parallel sides need to be the same. Also, if the width and height of a rectangle are the same, I prints a message indicating this is a square as requested by the instruction.

History