Java Programming Assignment: 1- Write an Animal class that has two member variab
ID: 3884823 • Letter: J
Question
Java Programming Assignment:
1- Write an Animal class that has two member variables called weight and height. It should have two member methods called makeNoise and eat. Both of these methods should only have a print statement saying what that method is. Make the Animals comparable using the weight as the deciding factor of who is bigger. Have setters for both member variables.
2- Write 3 more classes called Dog, Cat and Pig. Each of these classes should have a method called makeNoise. The makeNoise of Dog should have a print statement saying Bark. The makeNoise of Cat should have a print statement saying MEOW. The makeNoist of pig should have a print statement saying OUNK.
3- In main class create two objects of type Cat, two objects of type Dog and two objects of type Pig. Set the height and weight of each one as you like. Create an array of type Animal and save all these 6 objects in this array. Name this array arrayA.
4- Write a for loop in main that repeats 6 times and each time call the method makeNoise by having arrayA[i].makeNoise(). You will see that the same line makes different sounds depending what object is stored in that location of the array. We call this Polymorphism. So you have written a polymorphic code.
Explanation / Answer
Now that our base class has an explicit constructor, we need to change the subclasses Dog and Cat so that they call it in their own constructors:
The syntax super(args...) allows the constructors in subclasses to invoke a constructor for their immediate superclass.
Because the subclasses inherit methods from the superclass, you can call superclass methods on an instance of a subclass. For example:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.