Object Oriented Programming (ECEG-3162) Laboratory Exercises
Addis Ababa University
Addis Ababa Institute of Technology
School of Electrical and Computer Engineering
Object Oriented Programming(ECEG-3162)
Laboratory Exercise #4
(Java Object Oriented Concepts (Static and Final Modifiers and Inheritance concepts))
Objective
Understanding the static and final modifiers through practical programming exercises
Writing reusable code by applying the concepts of inheritance
Preparation Task
Revising/understanding the theoretical concepts which are dealt in the lecture session
Activities
A. Racecar Class OOP implementation:
Create a new class called Racecar. Add two fields to Racecar: a field of type String to store the
name of the car and a field of type Color (from java.awt.Color) to store the color of the car. Each car
can have a different name and color. Should the fields be static or non-static?
Every one of our racecars will have the same top speed. Add a private constant of type double to
the Racecar class to store the top speed and initialize it to any number you want. Should this field
be static or non-static?
Add a constructor to Racecar which accepts a name and color argument and assigns the arguments
to the name and color fields of the class.
Add a method to Racecar called race which accepts two Racecars as arguments, simulates a race
between the two, and returns the car that wins the race, or null if the race is a tie. The method
should calculate a random speed for each car between 0 and the top speed. The car with the higher
speed wins the race, but if they both have the same speed they tie. The method random in
java.util.Random returns a random double between 0 and 1. If you multiply this random number by
the top speed, the product will be a random number between 0 and the top speed. Should this
method be static or non-static?
Add a main method to Racecar which creates two Racecars, races them against one another, and
prints out the winner’s name.
Nov 2019 Compiled by: Hailemelekot D.
B. Students Class OOP Implementation for demonstrating the Concept of inheritance:
Create a package called students. All the classes you create in this will be created in this package
Create a class called Student, which should have two properties, a name and a year and methods
to get the name and get the year of the student. Initialize these properties to arguments passes into
the constructor.
Create a subclass of Student called Undergrad. The Undergrad constructor should accept name and
year arguments. Add a method to Undergrad called description which returns a String containing
the name of the undergrad, then a space, then a capital ’U’, then a space, and then the year of the
undergrad. For example, the description method of an Undergrad instance with the name
”Michael” and the year 2006, should return the String “Michael U 2006”.
Create a subclass of Student called Grad. The Grad constructor should accept only the name of the
Grad as an argument, and it should always initialize the Grad’s year to 5. Add a description method
to Grad which returns a String containing the name of the Grad, followed by a space and then the
letter ’G’. The description method of a Grad named Jennifer should return the String say Jennifer G.
Create a subclass of Undergrad called Intern. In addition to the name and year properties, Intern
should have a wage and a number of hours that are initialized in the constructor. Add a getPay
method to Intern which returns the wage times the number of hours. Add a description method to
Intern which returns a String containing the result of calling Undergrad’s description method
followed by the return value of the getPay method. The description method of an Undergrad named
Elizabeth” whose year is 2005 and worked 20 hours at $10.32/hour, should return the String
“Elizabeth U 2005 206.4”
Create a class called StudentTest that has a main method. Use the main method to test the class
hierarchy you just built. Create some instances of Undergrad, Grad and Intern. Print out the result of
their description methods.
Nov 2019 Compiled by: Hailemelekot D.