Professional Documents
Culture Documents
Objective
Pre-lab Exercise
class Paint {
// all methods have same name
public void Color(int x) {
}
1|Page
4. Which of the following is true about abstract method?
A. Must be implemented in a subclass
B. Must be declared as abstract
C. The method has only the heading with no body
D. None
5. What is wrong with the following block of code?
interface Shape {
void print();
}
Class Test{
public static void main() {
Shape s1=new Shape();
}
}
class Father {
2|Page
public static void main(String[] args) {
class A
{ int i;
public A(){ i=10;}
}
class B extends A
{ int i;
public B(){ i=20;}
}
3|Page
In-lab Exercise
8. Design an inheritance hierarchy with Employee base class for HourlyEmployee and
SalaryEmployee subclasses. The common fields and methods to these classes are name
and job title of the employee along with accessors and mutators. The salary employees
need an attribute for weekly salary, and the corresponding methods for accessing and
changing its sate. The HourlyEmployess class possesses pay rate and hours worked fields.
The superclass contains an abstract method called calculateWeeklyPay(), which must
be implemented in classes derived from Employee class. The weekly salary worker’s pay
is just the weekly salary, whereas pay for an hourly employee is simply hours worked times
pay rate.
9. Define an abstract class Shape with abstract method CalculateSurface() and fields
width and height. Define two additional classes for a triangle and a rectangle, which
implement CalculateSurface(). This method has to return the areas of the rectangle
(height*width) and the triangle (height*width/2). Define a class for a circle with an
appropriate constructor, which initializes the two fields (height and width) with the same
value (the radius) and implement the abstract method for calculating the area. Create
three different shapes (rectangle, triangle, circle) and calculate the area of each shape.
4|Page