23T3 Week 8
Monday 11AM - 2PM (M11B)
Wednesday 12PM -3PM (W12A)
Slides by Alvin Cherk (z5311001)
Please let me know if you want to assignment-iii individually, if you don't you will default into your assignment-ii pairs
No lab marking in week 10.
Means that all labs must be marked in week 9.
There will be a sample exam with the exam environment in week 10. I highly encourage you to attend if you want to be familiar with the exam layout and exam environment.
Assignment-i marks have been released. Check teams for more info.
TLDR; Mark on give, feedback on GitLab issue.
If you have any queries about your feedback or mark, or want more feedback about your design, ask me in the lab or send me an email.
What are generics?
Generics enable types to be passed when defining classes, interfaces or methods
/**
* How many different types of T here?
*
* It's all about the scope
*/
public class ConfusingClass<T> {
private T t1;
public <T> T t2(T t) {
return null;
}
public static <T> T t3(T t) {
return null;
}
public static void main(String[] args) {
ConfusingClass<String> cc = new ConfusingClass<String>();
cc.t2(3);
}
}
Example from Webster
class ConfusingClass<T1> {
private T1 t1;
// private T2 t2; // nope
// private T3 t3; // nope
public <T2> T2 t2(T2 t) {
T1 t1;
T2 t2;
// T3 t3; // nope
return null;
}
// ConfusingClass.t3(...)
public static <T3> T3 t3(T3 t) {
// T1 t1; // nope
// T2 t2; // nope
T3 t3;
return null;
}
}
Example from Webster
Stack.java
Inside src/stack
, there are a series of stubs for a Stack
class which takes in a generic type. There are a series of tests inside StackTest.java
which currently fail.
Implement the methods so that the tests pass, using an ArrayList
to store the internal data structure. Answer the following questions:
E
?
Iterable
interface? Why does it have an E
as well? What methods does it force us to implement?
.iterator()
method
When completing toArrayList
, why do we need to make a copy rather then just returning our internal ArrayList
?
Don't want to break encapsulation
.iterator()
method allow us to do? Look at StackTest.java
public static Integer sumStack(Stack<? extends Integer> stack);
<? extends Type>
and <? super Type>
mean?
extends
: the parameterized type must be a class or subclass of the given typesuper
: the parameterised type must be a class or super class of the given type?
and E
?
?
can't be referred to as a type (Type erasure)E
canWhat type of pattern?
Creational pattern
The singleton pattern ensures that a class has only one instance. It provides a global access point to this instance.
It helps avoid initialisation overhead when only 1 copy of an instance is needed.
Heist.java
No lab marking in week 10.
Means that all labs must be marked in week 9.
There will be a sample exam with the exam environment in week 10. I highly encourage you to attend if you want to be familiar with the exam layout and exam environment.
Assignment-i marks have been released. Check teams for more info.
TLDR; Mark on give, feedback on GitLab issue.
If you have any queries about your feedback or mark, or want more feedback about your design, ask me in the lab or send me an email.