Department of Computer Science & Engineering: Jubail University College
Department of Computer Science & Engineering: Jubail University College
The class Undergrad extends the Student class. Note the overridden toString() method
System.out.println(s1);
System.out.println(s2);
System.out.println(u1);
System.out.println(g1);
}
}
Student:
ID: 97000
Name: Sameer
GPA: 3.51
Student:
ID: 98000
Name:
GPA: 3.22
Undergraduate Student:
ID: 99000
Output Name: Shahid
GPA: 2.91
Year: Junior
Graduate Student:
ID: 200000
Name: Mubin
GPA: 3.57
Thesis: Algorithms and Complexity
Press any key to continue...
Exercise1:
Consider a superclass PurchaseItem which models customer’s purchases. This class has:
- two private instance variables name (String) and unitPrice (double).
- One constructor to initialize the instance variables.
- A default constructor to initialize name to “no item”, and unitPrice to 0. use this()
- A method getPrice that returns the unitPrice.
- Accessor and mutator methods.
- A toString method to return the name of the item followed by @ symbol, then the
unitPrice.
Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional
instance variable weight (double) in Kg while CountedItem has an additional variable
quantity (int) both private.
- Write an appropriate constructor for each of the classes making use of the constructor of
the superclass in defining those of the subclasses.
- Override getPrice method that returns the price of the purchasedItem based on its unit
price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the
superclass
- Override also toString method for each class making use of the toString method of the
superclass in defining those of the subclasses.
toString should return something that can be printed on the receipt.
For example
Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)
Pens @ 4.5 10 units 45 SR (in case of CountedItem class)
Write an application class where you construct objects from the two subclasses and print
them on the screen.
Exercise 2:
Use the sample Java programs provided above and modify them to answer the following
questions.
a- Can an instance method override a static method?
b- Can a static method override (hide) an instance method?
c- Can you override a final instance method?
d- Can you override an instance method and make it final?
e- Can you override an instance method and change its return type?
f- Can you hide a final static method ?
g- Can an instance field hide a static field?
h- Can a static field hide an instance field?
i- Can an instance method with public visibility override an instance method with
default visibility?
j- Can an instance method with default visibility override an instance method with
public visibility?
k- Can an instance method with protected visibility override an instance method with
default visibility?
l- Can an instance method with default visibility override an instance method with
protected visibility?
m- Based on the last four question, order the access visibility from the widest to the
narrowest (weakest) and state the rule for overriding (instance methods) or hiding
(static methods) ?