Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of...
-
Upload
claud-holt -
Category
Documents
-
view
214 -
download
0
Transcript of Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of...
![Page 1: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/1.jpg)
Problem of the Day
How can you make 16 right angles using 4 matchsticks
WITHOUT breaking any of them?
![Page 2: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/2.jpg)
Problem of the Day
How can you make 16 right angles using 4 matchsticks
WITHOUT breaking any of them?
![Page 3: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/3.jpg)
LECTURE 14:GENERIC TYPES
![Page 4: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/4.jpg)
Generic Types
Recent release of Java added generics Include type parameters in class
definition Like methods, parameters can change each
time Fields independent of types can now be
written
![Page 5: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/5.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/6.jpg)
Generics Before & After
Before Generics After Generics
public 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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/7.jpg)
Writing Generics
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();}
}
![Page 8: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/8.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/9.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/10.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/11.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/12.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/13.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/14.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/15.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/16.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/17.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/18.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/19.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/20.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/21.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/22.jpg)
public class Entry<S, TYPE> { private S 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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/23.jpg)
public class Entry<S, TYPE> { private S key; private TYPE value; // And more goes here...
}Entry<String, Integer> a;Entry<String> b; Did not specify for each type
Entry<String, String> c;Entry<String, boolean> d;Entry<Car, Boolean> e;
Can Use Multiple Generic Types
![Page 24: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/24.jpg)
public class Entry<S, TYPE> { private S key; private TYPE value; // And more goes here...
}Entry<String, Integer> a;Entry<String> b; Did not specify for each type
Entry<String, String> c;Entry<String, boolean> d; Not reference type
Entry<Car, Boolean> e;
Can Use Multiple Generic Types
![Page 25: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/25.jpg)
When To Specify Type
Whenever class name used (except constructors) Variable declarations:ArrayList<Integer> hogCount;
Object instantiation:hogCount = new ArrayList<Double>();
Return type for method :private ArrayList<Pig> transport()
Parameter listing:public void cook(ArrayList<Meat> fd)
Used as type parameter:ArrayList<ArrayList<Meat>> bacon;
![Page 26: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/26.jpg)
Type cannot be specified instantiating array Compiler error if type specified during
instantiation Can provide type theory explaining this
problem
Generics Annoyance
![Page 27: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/27.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/28.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/29.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/30.jpg)
Wildcard in Generic
public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){
myList = lst;}
public void printListSize() {System.out.println(myList.size());
}
public ArrayList<?> getList() {return myList;
}}
![Page 31: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/31.jpg)
Wildcard in Generic
public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){
myList = lst;}
public void printListSize() {System.out.println(myList.size());
}
public ArrayList<?> getList() {return myList;
}}
? matches any reference type(and String)
![Page 32: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/32.jpg)
Wildcard in Generic
public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){
myList = lst;}
public void printListSize() {System.out.println(myList.size());
}
public ArrayList<?> getList() {return myList;
}}
Any ArrayList can be passed in for lst
![Page 33: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/33.jpg)
Wildcard in Generic
public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){
myList = lst;}
public void printListSize() {System.out.println(myList.size());
}
public ArrayList<?> getList() {return myList;
}}
Can call methods as long asmissing type not important
![Page 34: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/34.jpg)
Wildcard in Generic
public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){
myList = lst;}
public void printListSize() {System.out.println(myList.size());
}
public ArrayList<?> getList() {return myList;
}}
Legal, but yucky. All type information is lost!
![Page 35: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/35.jpg)
Typecasting Explained
![Page 36: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/36.jpg)
Typecasting Explained
![Page 37: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/37.jpg)
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: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/38.jpg)
Your Turn
Get into your groups and complete activity
![Page 39: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/39.jpg)
For Midterm
You can use on this midterm: Your textbook & notes Printout of slides IF has notes on that day's
slides At the same time, you may NOT use:
Computer, calculator, cell phone, or similar Copies of daily activities and/or solutions Friends, Romans, countrymen or their ears
To be certain rules are followed, when test ends Hand in all printed material you had with
you
![Page 40: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/40.jpg)
How to Prepare for Midterm
DO DON'T
Make cheat sheets for the test
Review how parts of Java work
Add post-its to important pages
Memorize Drink case of 40s before
test Use post-its as clothing
![Page 41: Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c00d1a28abf838cc9839/html5/thumbnails/41.jpg)
For Next Lecture
Reviewing OO Programming on Monday Come prepared to ask any questions you
still have Will have some fun & interesting challenges
in class See how prepare you are for Wednesday's
midterm