COMP2511

23T2 Week 8

Wednesday 1PM - 4PM (W13A)

 Thursday 3PM - 6PM (H15B)

 

Slides by Alvin Cherk (z5311001)

This week

  • Generic programming
  • Singleton pattern

Please let me know in the lab if you want to do assignment-iii as a pair. If not, you are default solo

Admin

No lab marking in week 10.

Means that all labs must be marked in week 9.

Generic Programming

Generic Programming

What are generics?

Generics enable types to be passed when defining classes, interfaces or methods

  • Remove casting and offer stronger type checks at compile time
  • Allow implementations of generic algorithms, that work on a collection of different types
  • Adds stability to code by making more of your bugs detectable at run-time

Generic Programming

/**
 * 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

Generic Programming

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

Code Demo

Stack.java

Code Demo

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:

  1. What is E?
    • A generic type
  2. What is the Iterable interface? Why does it have an E as well? What methods does it force us to implement?
    • Iterable: Something that can be iterated over
    • Forces us to implement the .iterator() method

Code Demo

 

  1. When completing toArrayList, why do we need to make a copy rather then just returning our internal ArrayList?

    • Don't want to break encapsulation

  2.  What does the .iterator() method allow us to do?  Look at StackTest.java
    • Allows us to loop through it like a normal collection in a standardized way 

Code Demo

public static Integer sumStack(Stack<? extends Integer> stack);
  1. What does the <? extends Type> and <? super Type> mean?
    • extends : the parameterized type must be a class or subclass of the given type
    • super : the parameterised type must be a class or super class of the given type
  2. What is the difference between ? and E?
    • ? can't be referred to as a type (Type erasure)
    • E can

Singleton Pattern

Singleton Pattern

What 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.

Code Demo

Heist.java

Code Demo

Admin

No lab marking in week 10.

Means that all labs must be marked in week 9.

Feedback

COMP2511 Week 8 23T2

By kuroson

COMP2511 Week 8 23T2

  • 132