This week we are learning about inheritance.
Question 1
Create a new class called Circle. Add an attribute called radius for circle. Add a constructor that accepts a radius as a parameter. Add a method called getArea().
Create another class called Cylinder that inherits from Circle class. Cylinder class has an additional attribute called length. Add a constructor that accepts radius and a length. Add a method called getArea()
Create a main method to create two instances of the Circle and two instances of cylinder and add all four objects into a circle array. Using a for loop print area of each object to console screen.
Question 2
Create a new class called Clock with the integer attributes hour, minute and second. Create a constructor that allows all instance variable to be initialized. Add a method called displayTime().
Create a subclass called AlarmClock. AlarmClock should have additional object attribute called alarmHour and alarmMinute. Add a constructor that accepts radius and length. Add a method called displayAlarmTime().
Create a main method to create two instance of the Clock- GrandPaClock and WallClock.
Add two instances of AlarmClock - WristWatch and DeskClock. Add all objects into an array and print the time on all clocks.
Important: Please ensure that you add adequate comments to explain your code.
Comments
Post a Comment