From 4df0e2aeb7a0e13abd7c74d996e851e25ff624e9 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 10 May 2023 11:00:00 -0600 Subject: [PATCH] Day 3 break 3 --- Questions.txt | 32 +++++++++++++++++++ src/main/java/doublestuff/Parsing.java | 10 ++++++ src/main/java/lambdas/Demos.java | 44 ++++++++++++++++++++++++-- src/main/java/sealedtypes/Ex1.java | 25 +++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/main/java/doublestuff/Parsing.java create mode 100644 src/main/java/sealedtypes/Ex1.java diff --git a/Questions.txt b/Questions.txt index 25e1672..3256c17 100644 --- a/Questions.txt +++ b/Questions.txt @@ -630,3 +630,35 @@ B) Consumer cs = s -> { System.out.println(s); }; C) Supplier ss = (s1, s2) -> s1 + s2; D) BiPredicate bps = (String s) -> { return s.length() > 0; }; E) ToIntFunction tofs = s -> s.length(); + +Q) Given: + ???? (s, t) -> s.length() - t.length(); + +Which are correct inserted in place of ???? +A) BiFunction f = +B) BiPredicate f = +C) Comparator f = +D) Supplier f = +E) BinaryOperator f = + +Q) Given, in a single file X.java: +interface ArbInter {} +final class Y implements X {} +sealed interface X /* point x */{} +// line n1 + +Which is/are true? +A) the code compiles as it stands +B) the code compiles if the following is added at point x +permits Y, Z +AND this is added at line 1 +class Z implements X {} +C) the code compiles if the following is added at point x +permits Y, Z +AND this is added at line 1 +record Z(String name) implements X {} +D) the code compiles if the following is added at point x + permits Y extends ArbInter +E) the code compiles if the following is added at line n1 +non-sealed abstract class Z implements X permits Q {} +final class Q extends Z {} diff --git a/src/main/java/doublestuff/Parsing.java b/src/main/java/doublestuff/Parsing.java new file mode 100644 index 0000000..4c8858f --- /dev/null +++ b/src/main/java/doublestuff/Parsing.java @@ -0,0 +1,10 @@ +package doublestuff; + +public class Parsing { + public static void main(String[] args) { + System.out.println(Double.parseDouble("Infinity")); + System.out.println(Double.parseDouble("-Infinity")); + System.out.println(Double.parseDouble("+Infinity")); + System.out.println(Double.parseDouble("NaN")); + } +} diff --git a/src/main/java/lambdas/Demos.java b/src/main/java/lambdas/Demos.java index 2649844..91e69df 100644 --- a/src/main/java/lambdas/Demos.java +++ b/src/main/java/lambdas/Demos.java @@ -1,15 +1,21 @@ package lambdas; +import java.util.Comparator; import java.util.List; -import java.util.function.ToIntFunction; +import java.util.function.*; interface StudentCriterion { boolean test(Student s); } class Student { - public int getGpa() { return 0; } - public List getCourses() { return null; } + public int getGpa() { + return 0; + } + + public List getCourses() { + return null; + } } class SmartStudent implements StudentCriterion { @@ -72,5 +78,37 @@ public static void main(String[] args) { // Long lng = 99; // Nope Byte b = null; long lng = b; + + Object BiPredicate; +// BiFunction f = (s, t) -> s.length() - t.length(); +// BiPredicate f = (s, t) -> s.length() - t.length(); +// Comparator f = (s, t) -> s.length() - t.length(); +// Supplier f = (s, t) -> s.length() - t.length(); + BinaryOperator f = (s, t) -> (s.length() - t.length()) + ""; +// BinaryOperator f = (s, t) -> s + t; + +// IntFunction f1 = (f) -> f; +// f1.applyAsInt(Integer.valueOf(0)); + } + + interface BiFunction { + R apply(A1 a1, A2 a2); + } + + interface IntFunction { + E applyAsInt(int i); + } + class MyIntFunction implements IntFunction { + @Override +// public Integer applyAsInt(Integer f) { + public Integer applyAsInt(int f) { +// return Integer.valueOf(f); + return f; + } + @Override +// public boolean equals(MyIntFunction me) { return false; } + public boolean equals(Object me) { return false; } + + } } diff --git a/src/main/java/sealedtypes/Ex1.java b/src/main/java/sealedtypes/Ex1.java new file mode 100644 index 0000000..da70dad --- /dev/null +++ b/src/main/java/sealedtypes/Ex1.java @@ -0,0 +1,25 @@ +package sealedtypes; + +interface Transporter { +} + +class Truck implements Transporter { +} + +class Car implements Transporter { +} + +//class Bicycle implements Transporter {} + +public class Ex1 { + public static void main(String[] args) { + Transporter t = Math.random() > 0.5 ? new Truck() : new Car(); + + if (t instanceof Truck tr) { + // can the truck do this? + } else if (t instanceof Car c) { + // can the car do this? + } + + } +}