Question of the Day

39
Question of the Day Move one matchstick to produce a square

description

Question of the Day. Move one matchstick to produce a square. Question of the Day. Move one matchstick to produce a square. CSC 212 – Data Structures. Lecture 19: Generic Types. Generic Types. Starting with 1.5 release, Java uses generics Include type parameters in class definition - PowerPoint PPT Presentation

Transcript of Question of the Day

Page 1: Question of the Day

Question of the Day

Move one matchstick to produce a square

Page 2: Question of the Day

Question of the Day

Move one matchstick to produce a square

Page 3: Question of the Day

LECTURE 19:GENERIC TYPES

CSC 212 – Data Structures

Page 4: Question of the Day

Generic Types

Starting with 1.5 release, Java uses generics

Include type parameters in class definition Like methods, parameters can change each

time Fields independent of types can now be

written

Page 5: Question of the Day

Generic Types

On allocating instance, actual type is specified Must be reference type or String as actual

type Code runs as if were written using that type Type used by instance cannot be changed Type parameter becomes part of variable’s

type

Page 6: Question of the Day

Generics Before & After

Before Generics After Genericspublic class ONode {private Object data;ONode(Object d) { data = d;}Object getData() { return data;}void setData(Object d){ data = d;}

}

public class Node<T> {private T data;public Node(T d) { data = d;}T getData() { return data;}void setData(T d){ data = d;}

}

Page 7: Question of the Day

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

Page 8: Question of the Day

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

Page 9: Question of the Day

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

Page 10: Question of the Day

public class Bag<T> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

Page 11: Question of the Day

Bag<Integer> earth = new Bag<Integer>(4, “The Answer”);public class Bag<T> {

private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

Page 12: Question of the Day

Bag<Integer> earth = new Bag<Integer>(4, “The Answer”);public class Bag {

private Integer data;private String name;

public Bag(Integer d, String newName) { data = d; name = newName;}

public Integer getData() { return data; }

public void setData(Integer d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

For earth, class written as if T were replaced by Integer

Page 13: Question of the Day

Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”);public class Bag<T> {

private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

Page 14: Question of the Day

Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”);public class Bag {

private Car data;private String name;

public Bag(Car d, String newName) { data = d; name = newName;}

public Car getData() { return data; }

public void setData(Car d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

For matchbox, T is Car

This can be at same time T is Integer when for earth

Page 15: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 16: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 17: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 18: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 19: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 20: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 21: Question of the Day

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Page 22: Question of the Day

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b;Entry<String, String> c;Entry<String, boolean> d;Entry<Car, Boolean> e;

Can Use Multiple Generic Types

Page 23: Question of the Day

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b; Did not specify for each typeEntry<String, String> c;Entry<String, boolean> d;Entry<Car, Boolean> e;

Can Use Multiple Generic Types

Page 24: Question of the Day

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b; Did not specify for each typeEntry<String, String> c;Entry<String, boolean> d; Not reference typeEntry<Car, Boolean> e;

Can Use Multiple Generic Types

Page 25: Question of the Day

When To Specify Type

Whenever class name used (except constructors) Variable declarations:Stack<Integer> hogCount;

Object instantiation:hogCount = new ArrayStack<Double>();

Return type for method :private Stack<Pig> transport()

Parameter listing:public void cook(Stack<Meat> fd)

Used as type parameter:Stack<Stack<Meat>> bacon;

Page 26: Question of the Day

Type cannot be specified instantiating array Compiler error if type specified during

instantiation Can provide type theory explaining this

problem

Generics Annoyance

Page 27: Question of the Day

Type cannot be specified instantiating array

Can use generics with arrays, but need typecast Only needed once, use generics after instantiation Still checks when compiling, so get most benefits

public class Farm<T> { private ArrayList<Feed>[] troughs; private T[] animals; public Farm() { troughs = (ArrayList<Feed>[])new ArrayList[10]; animals = (T[])new Object[1034821]; }}

Generics Annoyance

Page 28: Question of the Day

Single method or class may not care about type Different than using typecasts to get to

work Only will work if does not need to

instantiate object Do not skip specifying type -- that is BAD

IDEA™ Instead use the generic wildcard ?

In Case of Unknown Type

Page 29: Question of the Day

Single method or class may not care about type Different than using typecasts to get to

work Only will work if does not need to

instantiate object Do not skip specifying type -- that is BAD

IDEA™ Instead use the generic wildcard ?

In Case of Unknown Type

Page 30: Question of the Day

Wildcard in Generic

public class StackHolder {private Stack<?> myStack; public void setStack(Stack<?> lst){myStack = lst;}

public void printStackSize() {System.out.println(myStack.size());}

public Stack<?> getStack() {return myStack;}

}

Page 31: Question of the Day

Wildcard in Generic

public class StackHolder {private Stack<?> myStack; public void setStack(Stack<?> lst){myStack = lst;}

public void printStackSize() {System.out.println(myStack.size());}

public Stack<?> getStack() {return myStack;}

}

? matches any reference type(and String)

Page 32: Question of the Day

Wildcard in Generic

public class StackHolder {private Stack<?> myStack; public void setStack(Stack<?> lst){myStack = lst;}

public void printStackSize() {System.out.println(myStack.size());}

public Stack<?> getStack() {return myStack;}

}

Any Stack can be passed in for lst

Page 33: Question of the Day

Wildcard in Generic

public class StackHolder {private Stack<?> myStack; public void setStack(Stack<?> lst){myStack = lst;}

public void printStackSize() {System.out.println(myStack.size());}

public Stack<?> getStack() {return myStack;}

}

Can call methods as long asmissing type not important

Page 34: Question of the Day

Wildcard in Generic

public class StackHolder {private Stack<?> myStack; public void setStack(Stack<?> lst){myStack = lst;}

public void printStackSize() {System.out.println(myStack.size());}

public Stack<?> getStack() {return myStack;}

}

Legal, but yucky. All type information is lost!

Page 35: Question of the Day

Typecasting Explained

Page 36: Question of the Day

Typecasting Explained

Page 37: Question of the Day

99% of typecasts are incorrect 90% fix missing generic type specification

on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%

Typecasting Explained

Page 38: Question of the Day

Your Turn

Get into your groups and complete activity

Page 39: Question of the Day

For Next Lecture

Week#7 assignment due Wednesday Extra day given to you, since no classes on

Tuesday Help still available Tuesday; I will be in my

office Project #1 available from Angel; due in

2.5 weeks Based on time give, assume it to takes

much longer Do not wait until last second; cannot be

done in 1 hour Bring calendar Friday; will sign up for

templates Worth 8% of final grade, so need to do good

job