enums, generics, nested typespsnyder/cs342-summer2017/... · which is false of enums? a. enums are...
TRANSCRIPT
![Page 1: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/1.jpg)
Enums, Generics, Nested Types
June 23, 2017
![Page 2: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/2.jpg)
Reading Quiz
![Page 3: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/3.jpg)
What is true of Generics?
A. They allow you to maintain more type information in your code
B. They are required to store different types of objects in a container
C. They restrict you to having a single type of object in a container
D. They only work with primitive data types.
![Page 4: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/4.jpg)
Whats the Result?
A. "0"
B. "1"
C. "" (ie empty string)
D. Won't compile
E. Runtime error
ArrayList<Object> parent = new ArrayList<Object>(); Integer child = new Integer(0);
parent.add(child); System.out.println(parent.size());
![Page 5: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/5.jpg)
Whats the Result?
A. Parent is an ArrayList that can contain any type of object
B. Parent is an ArrayList that can contain only Integers (and subtypes)
C. Won't compile
D. Runtime error
ArrayList<Object> parent = new ArrayList<Integer>();
![Page 6: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/6.jpg)
Which is true of Nested Types
A. Nested types are a way of describing inheritance
B. Nested types are a way of describing classes with a more convenient syntax
C. Nested types are a way of controlling type visibility
D. Nested types are a way of handing concurrency
![Page 7: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/7.jpg)
Which is false of Enums?A. Enums are a programer convenience that allow you to give your values more meaningful names
B. Enums are a reliability mechanism that allow the type system to catch more errors
C. Enums are a reliability mechanism that allows the type system to perform additional checks
D. Enums are a performance mechanism that allow compiler optimizations
![Page 8: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/8.jpg)
Done!
![Page 9: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/9.jpg)
Housekeeping
• Homework 3 is out
• Class pace
• Feedback
![Page 10: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/10.jpg)
Enums
![Page 11: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/11.jpg)
Problem
• There are cases where we'd like to capture a finite set of values / states
• We'd like the compiler to be able to catch errors
• Invalid range values
• Typos
![Page 12: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/12.jpg)
State Range Example• NetworkRequest.state
• initialized
• sent
• acknowledged
• receiving
• finished
![Page 13: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/13.jpg)
State Range Example• NetworkRequest.state
• initialized = 0
• sent = 1
• acknowledged = 2
• receiving = 3
• finished = 4
![Page 14: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/14.jpg)
State Range Example• NetworkRequest.state
• initialized = 0
• sent = 1
• acknowledged = 2
• receiving = 3
• finished = 4
switch (aRequest.state) { case 0: // Send the request break;
case 1: // Wait for the request break;
// Other states… }
Issues?
![Page 15: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/15.jpg)
State Range Example• NetworkRequest.state
• initialized = "init"
• sent = "sent"
• acknowledged = "ack"
• receiving = "recv"
• finished = "fin"
switch (aRequest.state) { case "init": // Send the request break;
case "sent": // Wait for the request break;
// Other states… }
Issues?
![Page 16: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/16.jpg)
Java Solution
• Enums
• Variables can have a finite number of values
• Values can have human readable names
• Compiler can catch errors
![Page 17: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/17.jpg)
Java Solutionpublic enum NetworkStates { INITIALIZED, SENT, ACKNOWLEDGED, RECEIVING, FINISHED}
![Page 18: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/18.jpg)
Java Solutionpublic enum NetworkStates { INITIALIZED, SENT, ACKNOWLEDGED, RECEIVING, FINISHED}
switch (aRequest.state) { case NetworkStates.INITIALIZED: // Send the request break;
case NetworkStates.SENT: // Wait for the request break;
// Other states… }
![Page 19: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/19.jpg)
Generics
![Page 20: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/20.jpg)
Problem• Java's type system is double edged sword
• Specificity can require redundancy
• Code is written to work with one datatype
• Rewrite code for other datatypes?
• How to have specificity (types) and convenience (concision)?
![Page 21: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/21.jpg)
Randomizer.java –>
![Page 22: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/22.jpg)
Java Solution: Generics• Type variables for method signatures
• Ensure consistency of types
• Ex: If you give me an X and a Y, I'll give you a X back
• String and an Integer
• Integer and Integer
• whatever
![Page 23: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/23.jpg)
Randomizer.java –>
![Page 24: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/24.jpg)
Generics in Standard Library
• Extremely common
• Containers
• ArrayList<E>
• Set<E>
• Map<K, V>
• Documented extensively (ex, ArrayList)
![Page 25: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/25.jpg)
HashExample.java –>
![Page 26: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/26.jpg)
It can get Wild…
• Functions https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
• NestedHashMap< HashMap<K, V>, HashMap<V, K> >
![Page 27: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/27.jpg)
HashReverser.java –>
![Page 28: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/28.jpg)
Nested Types
![Page 29: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/29.jpg)
Just the Basics• `class` is overloaded in Java
• Types
• Namespace
• Data
• Functionality
• Nested types are a bandaid for this
![Page 30: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/30.jpg)
Problem Example
• Java doesn't have `function` (or anything like it)
• How to pass functionality around?
• Data-less classes
• These are one-offs
![Page 31: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/31.jpg)
NestedTypes.java –>
![Page 32: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/32.jpg)
![Page 33: Enums, Generics, Nested Typespsnyder/cs342-summer2017/... · Which is false of Enums? A. Enums are a programer convenience that allow you to give your values more meaningful names](https://reader034.vdocuments.net/reader034/viewer/2022050106/5f448fe8549cff39562da2c9/html5/thumbnails/33.jpg)
Homework 3