From 6cf3c6871d2ede5bc07f541fe958e2f576c7d7ea Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 9 May 2023 08:54:40 -0600 Subject: [PATCH] Day 2 Break 1 --- Questions.txt | 34 +++++++++ src/main/java/arrowswitch/Examples.java | 94 +++++++++++++++++++++++++ src/main/java/casting/Pattern2.java | 25 +++++++ src/main/java/casting/PatternOne.java | 14 +++- src/main/java/casting/Q.java | 14 ++++ 5 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 src/main/java/arrowswitch/Examples.java create mode 100644 src/main/java/casting/Pattern2.java create mode 100644 src/main/java/casting/Q.java diff --git a/Questions.txt b/Questions.txt index 76d5a2a..1c8ba27 100644 --- a/Questions.txt +++ b/Questions.txt @@ -270,3 +270,37 @@ C) if (obj instanceof String str if str.length() > 3) System.out.println(str) D) System.out.println(obj instanceof String.class str && str.length() > 3 ? str : "Nope"); E) if (!(obj instanceof String str) || str.length() <= 3) System.out.println("Nope"); else System.out.println(str); + +Q) Given: + String s = "Hello"; + switch (s) { + case "Hello" -> + System.out.print("Bonjour "); // line n1 + System.out.print("Guten Tag "); + default -> System.out.print("Bye"); + } + +Which is true? +A) The code prints: Bonjour Guten Tag Bye +B) The code prints: Bonjour Guten Tag +C) If line n1 is removed the code prints: Guten Tag Bye +D) If line n1 is removed the code prints: Guten Tag +E) The code fails to compile + +Q) + int x = 1; + // line n1 + switch(x) { + case 1: LocalDate.of(2023, x, 1); + default: LocalDate.of(2022, 12, 1); + }; + +Which are true? +A) The code compiles +B) The code throws an exception at runtime +C) The code compiles if line n1 is replaced with + var d = +D) The code compiles if the word "yield" is inserted + after the colon in both the case and default +E) The code compiles if the changes of both C and D + are applied diff --git a/src/main/java/arrowswitch/Examples.java b/src/main/java/arrowswitch/Examples.java new file mode 100644 index 0000000..525eb58 --- /dev/null +++ b/src/main/java/arrowswitch/Examples.java @@ -0,0 +1,94 @@ +package arrowswitch; + +import java.sql.SQLOutput; +import java.time.LocalDate; +import java.util.List; + +public class Examples { + public static void main(String[] args) { + String s = "Hello"; + switch (s) { + case "Hello": + case "Goodbye": + System.out.println("hello"); + System.out.println("really!"); + break; + default: + System.out.println("hmm"); + break; + } + System.out.println("---------------"); + switch (s) { +// case "Hello", "Goodbye": // comma works with colon too now :) + case "Hello", "Goodbye" -> { + System.out.println("hello"); + System.out.println("really!"); + if (Math.random() > 0.5) break; + System.out.println("Still here"); + } + default -> System.out.println("hmm"); + } + + System.out.println("----------------"); + switch (s) { + case "Hello" -> System.out.println("really!"); + default -> System.out.println("hmm"); + } + + } +} + +class MoreExamples { + public static void main(String[] args) { + + int x = 1; + // line n1 + // use a switch in a place where an expression is needed... + // it becomes an expression!!! + // it MUST cover all input values + // right side of -> must now be either single *expression* + // or we yield (NOT RETURN!!!!) + // CAN do this with colon form, but must use yield always. + // Take care... Colon form STILL falls through! + var y = switch(x) { + case 1 -> { + int year = 2023; +// if (Math.random() > 0.5) return; // NOPE!!!! + yield LocalDate.of(year, x, 1); + } + // expressions must not be void (i.e. must not be statements) +// default -> System.out.println(LocalDate.of(2022, 12, 1)); + default -> LocalDate.of(2022, 12, 1); + }; + System.out.println("y is " + y); + + // "Statement-expression" expression that is known to have potential + // side effects + // boolean expression! java allows us to ignore result of function calls + // so we can treat it as a statement. + // statement expressions generally are any expression with side effects + // function calls + // constructor calls + // increment/decrement operators + // assignment expressions + // can use these in for loops +// boolean f = false; +// for (System.out.println("Hello"), System.out.println("goodbye"); +// f; +// System.out.println("Loop increment")) +// ; + +// List ls = null; +// ls.add(""); + int a = 3, b = 2; +// a + b; // NOT legal in isolation + + + // line n1 + var r2 = switch(x) { + case 1: yield LocalDate.of(2023, x, 1); + default: yield LocalDate.of(2022, 12, 1); + }; + } + +} diff --git a/src/main/java/casting/Pattern2.java b/src/main/java/casting/Pattern2.java new file mode 100644 index 0000000..036591c --- /dev/null +++ b/src/main/java/casting/Pattern2.java @@ -0,0 +1,25 @@ +package casting; + +class Pair { + private E left; + private E right; + + public Pair(E left, E right) { + this.left = left; + this.right = right; + } + + public boolean equals(Object other) { +// if (other instanceof Pair otherP) { +// return this.left == otherP.left && +// this.right == otherP.right; +// } +// else return false; + return other instanceof Pair otherP && + this.left == otherP.left && + this.right == otherP.right; + } +} + +public class Pattern2 { +} diff --git a/src/main/java/casting/PatternOne.java b/src/main/java/casting/PatternOne.java index ff181b7..bb93835 100644 --- a/src/main/java/casting/PatternOne.java +++ b/src/main/java/casting/PatternOne.java @@ -1,5 +1,8 @@ package casting; +import java.util.Collection; +import java.util.List; + public class PatternOne { public static void main(String[] args) { Object obj = "Hello"; @@ -17,12 +20,17 @@ public static void main(String[] args) { // this is still a boolean expression!!! // Look into this Tuesday :) - if (obj instanceof String str) { - if (str.length() > 3) { +// if (obj instanceof String str) { +// if (str.length() > 3) { + if (obj instanceof String str && str.length() > 3) { System.out.println(str); answered = true; - } } if (!answered) System.out.println("Nope"); + +// Object obj2 = List.of("X", "Y"); + Collection obj2 = List.of("X", "Y"); + if (obj2 instanceof List ls) + System.out.println("yes"); } } diff --git a/src/main/java/casting/Q.java b/src/main/java/casting/Q.java new file mode 100644 index 0000000..689f02a --- /dev/null +++ b/src/main/java/casting/Q.java @@ -0,0 +1,14 @@ +package casting; + +public class Q { + public static void main(String[] args) { + Object obj = "Hel"; + System.out.println(obj instanceof String str && str.length() > 3 ? + str : "Nope"); + + if (!(obj instanceof String str) || str.length() <= 3) + System.out.println("Nope"); + else + System.out.println(str); + } +}