Title | Prac10-Generic Classes |
---|---|
Course | Intermediate Object-Oriented Programming |
Institution | La Trobe University |
Pages | 6 |
File Size | 101.5 KB |
File Type | |
Total Downloads | 346 |
Total Views | 778 |
IOO – Practice Class 10 (Week 11) Generic Classes Task 1 a) What is a generic class? b) What benefits do generic classes provide? c) Can an int be used to substitute for a type parameter? d) Can a String be used to substitute for a type parameter? e) Can an array be used to substitute for a type par...
IOO – Practice Class 10 (Week 11) Generic Classes
Task 1 a) What is a generic class? b) What benefits do generic classes provide? c) Can an int be used to substitute for a type parameter? d) Can a String be used to substitute for a type parameter? e) Can an array be used to substitute for a type parameter?
Task 2 Write the class header (declaration) for the following generic class: • The name of the class is • It has two type parameters
and
•
implements interface and
•
implements interfaces ,
and
Task 3 a) Define the class called . This class has two attributes and to store two Strings. It has a constructor that takes two Strings as arguments. It has methods to get and set the attributes, and a method. b) Convert the class above into a generic class called
.
c) Without using autoboxing and auto-unboxing, write a method to • Create a pair of Integers • Display the pair • Display the first Integer • Increase the value of the first Integer by 1 (and display the pair) d) Re-write the above method using autoboxing and auto-unboxing. 1
Task 4 Read the three classes given below. The aim is to keep track of versions of a target object which is a String. • Class keeps a copy of the target String together with the date and time. The date-and-time is stored as an instance of the class introduced in Java 8. See the appendix for a brief demo of this class • Class tracks the changes to a target String. It keeps the current copy, and all the previous copies. The previous copies have the dates and times at which they are saved • The third class is a tester program
2
3
Output:
Task 5 Convert the classes in the previous task into a generic version. Name the generic classes and
4
Task 6 a) Suppose the target for tracking is a String. Write a method to test the generic class by repeating the tests in b) Suppose the target for tracking is of type below
, whose class is defined
Write a method to keep track of the following changes Initially, the object has the following name, author and date created: Then at the first update, it is: Then at the second update, it is:
5
Appendix
6...