csci-142 computer science 2 recitationcsci142/recitations/03/03-interfacesgenerics.pdf11 from:...
TRANSCRIPT
![Page 1: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/1.jpg)
CSCI-142 Computer Science 2
Recitation
03InterfacesGenerics
![Page 2: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/2.jpg)
2
Java Interfaces
● An interface is a bit like a class, but can only contain method signatures (and static fields)
public interface Animal { double run(int seconds); String speak();}
● As a consequence, an interface cannot be instantiated
Animal a = new Animal();
Error. Animal is abstract and cannot be instantiated
![Page 3: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/3.jpg)
3
Implementing an Interface
● When a class implements an interface it agrees to implement all of its methods
public class Camel implements Animal { ... @Override public double run(int seconds) { return (seconds * RUN_SPEED_MS) / this.numHumps; }
@Override public String speak() { return toString() + " says, \"grunt!, grunt!\""; }
![Page 4: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/4.jpg)
4
Relationship: A Camel is-an Animal
● When an Animal reference points to a Camel instance, it may only call on the Animal behaviors
Camel joe = new Camel(2);joe.run(10); // OKjoe.speak(); // OKjoe.milk(5); // OK
Animal animal = joe; // OKanimal.run(7); // OKanimal.speak(); // OKanimal.milk(2); // ERROR
![Page 5: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/5.jpg)
5
UML Class Diagram: Animal Farm
![Page 6: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/6.jpg)
6
Relationship: A Penguin is-an Animal and is-a Swimmer
public class Penguin implements Animal, Swimmer { ... @Override public double run(int seconds) {
... } @Override public String speak() {
... } @Override public double dive(int minutes) { ... }
![Page 7: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/7.jpg)
7
public class AnimalFarm { private ArrayList<Animal> animals; ... public AnimalFarm() {
... this.animals.add(new Camel(1)); this.animals.add(new Penguin(10)); this.animals.add(new Camel(2)); this.animals.add(new Penguin(20)); }
for (Animal anim : animals) { anim.speak();}
Camel Penguin Camel Penguin
animals
anim
Camel{numHumps=1, litersMilked=0.0} says, "grunt!, grunt!"Penguin{beakLength=10, fishEaten=0} says, "gak!, gak!"Camel{numHumps=2, litersMilked=0.0} says, "grunt!, grunt!"Penguin{beakLength=20, fishEaten=0} says, "gak!, gak!"
output
![Page 8: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/8.jpg)
8
Java Generics
● Java’s type system is extended by generics – Allows a type or method to operate on objects of
various types– Provides compile-time safety
ArrayList<String> strings = new ArrayList<>();strings.add("hello");strings.add(10); // Error: add(String) cannot be applied to int
ArrayList<Integer> ints = new ArrayList<>();ints.add(10);String s = ints.get(0); // Error: incompatible types // requires Integer, found String
![Page 9: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/9.jpg)
9
Generics: Type Omission
● When the generic type is omitted Object is used as the default type– In order to work with legacy code prior to Java 5
when generics were introduced
ArrayList strings = new ArrayList(); // generic type is Objectstrings.add("10"); // Warning: unchecked call to add(E)String s1 = strings.get(0); // Error: incompatible types // requires Object, found String
String s2 = (String) strings.get(0); // typecasting works (ugly!)int i = (int) strings.get(0); // Runtime Error: ClassCastException // String can't be cast to Integer
![Page 10: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/10.jpg)
10
Python: BTNode dataclass
@dataclassclass BTNode: value: Any # node can hold any value left: Union[None, 'BTNode'] # the left child right: Union[None, 'BTNode'] # the right child
def mk_btnode_leaf(value): return BTNode(value, None, None)
def mk_btnode(value, left, right): return BTNode(value, left, right)
root = mk_btnode('A', # root mk_btnode_leaf('B'), # root's left mk_btnode_leaf('C')) # root's right
root.value # 'A'root.left # BTNode(value='B', left=None, right=None)root.left.value # 'B'root.left.right # None
A
B C
root
None None None None
left right
left leftright right
![Page 11: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/11.jpg)
11
From: Python AnyTo: Java Generic Type E
@dataclassclass BTNode: value: Any # node can hold any value left: Union[None, 'BTNode'] # the left child right: Union[None, 'BTNode'] # the right child
public class BTNode<E> { /** node can hold any value */ private E value; /** the left child */ private BTNode<E> left; /** the right child */ private BTNode<E> right;
● Our Java generic class is BTNode<E>
Python
Java
![Page 12: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/12.jpg)
12
From: Python NoneTo: Java null
def mk_btnode_leaf(value): return BTNode(value, None, None)
def mk_btnode(value, left, right): return BTNode(value, left, right)
Python/** * Create a new leaf node (no children). * * @param value value for the node */public BTNode(E value) { this(value, null, null);}
/** * Create a new non-leaf node. * @param value value for the node * @param left the left child * @param right the right child */public BTNode(E value, BTNode<E> left, BTNode<E> right) { this.value = value; this.left = left; this.right = right;}
Java
● In Java we link the leaf node constructor to the non-leaf constructor with this and use null for no child
![Page 13: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/13.jpg)
13
BTNode.java: Accessors and toString
public class BTNode<E> { private E value; private BTNode<E> left; private BTNode<E> right;
… /** * Get the value in the node. * * @return node's value */ public E getValue() { return this.value; }
/** * Get the left child of the node. * * @return node's left child */ public BTNode<E> getLeft() { return this.left; }
/** * Get the right child of the node. * @return node's right child */public BTNode<E> getRight() { return this.right;}
/** * Return a string representation e.g.: * "rootA: BTNode{value=A, left=null, * right=null}" * @return the string representation */@Overridepublic String toString() { return "BTNode{" + "value=" + this.value + ", left=" + this.left + ", right=" + this.right + '}';}
![Page 14: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/14.jpg)
14
BTNode<E> Usage
public static void main(String[] args) { BTNode<String> rootA = new BTNode<>("A"); System.out.println("rootA: " + rootA); System.out.println("rootA value: " + rootA.getValue());
BTNode<String> rootB = new BTNode<>("B", new BTNode<>("C"), new BTNode<>("D")); System.out.println("rootB: " + rootB); System.out.println("rootB left: " + rootB.getLeft()); System.out.println("rootB right: " + rootB.getRight());}
rootA: BTNode{value=A, left=null, right=null}rootA value: ArootB: BTNode{value=B, left=BTNode{value=C, left=null, right=null}, right=BTNode{value=D, left=null, right=null}}rootB left: BTNode{value=C, left=null, right=null}rootB right: BTNode{value=D, left=null, right=null}
output
![Page 15: CSCI-142 Computer Science 2 Recitationcsci142/Recitations/03/03-InterfacesGenerics.pdf11 From: Python Any To: Java Generic Type E @dataclass class BTNode: value: Any # node can hold](https://reader030.vdocuments.us/reader030/viewer/2022041117/5f2d6bc13adfe320b71a5555/html5/thumbnails/15.jpg)
15
Presentation Code
● Interfaces: Animal Farm– https://www.cs.rit.edu/~csci142/Recitations/03/Code/Animal.java
– https://www.cs.rit.edu/~csci142/Recitations/03/Code/AnimalFarm.java
– https://www.cs.rit.edu/~csci142/Recitations/03/Code/Camel.java
– https://www.cs.rit.edu/~csci142/Recitations/03/Code/Penguin.java
– https://www.cs.rit.edu/~csci142/Recitations/03/Code/Swimmer.java
● Generics: BTNode– https://www.cs.rit.edu/~csci142/Recitations/03/Code/BTNode.java
– https://www.cs.rit.edu/~csci142/Recitations/03/Code/btnode.py