From b54039db302d31b7e5af476425f3c1413017ee78 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 9 May 2023 12:03:51 -0600 Subject: [PATCH] Day 2 Break 4 --- Questions.txt | 82 ++++++++++++++++++++- src/main/java/enumstuff/Ex1.java | 30 ++++++++ src/main/java/innerclasses/Access.java | 39 ++++++++++ src/main/java/innerclasses/AnonymousEx.java | 31 ++++++++ src/main/java/recordstuff/Example.java | 42 +++++++++++ 5 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 src/main/java/enumstuff/Ex1.java create mode 100644 src/main/java/innerclasses/Access.java create mode 100644 src/main/java/innerclasses/AnonymousEx.java create mode 100644 src/main/java/recordstuff/Example.java diff --git a/Questions.txt b/Questions.txt index b90a0ae..5fe81b2 100644 --- a/Questions.txt +++ b/Questions.txt @@ -420,4 +420,84 @@ C) method C is called D) method D is called E) method E is called -Q) \ No newline at end of file +Q)Given: +enum Day { + MONDAY(1), TUESDAY; + public Day(int d) {} + public Day() {} +} +and: +sout(Day.MONDAY) + +What is the result? +A) 1 +B) MONDAY +C) Day.MONDAY +D) Exception at runtime +E) Compilation fails + +Q) Given: +public void doStuff() { + int len = 3; + Predicate ps = new Predicate() { + @Override + public boolean test(String s) { + return s.length() > len; // line n1 + } + }; + // line n2 +} + Which are true? + A) The code compiles + B) Compilation fails due to a problem at line n1 + C) adding the code +System.out.println("testing lines " + (++len)); + at line n2 is OK + +Q) Given: +class Outer1 { + class Inner1 { + private int y = 100; + // line n1 + } + private int x = 99; + // line n2 +} + +and these method proposals: +void showX() { sop("x is " + x); } +void showY() { sop("y is " + y); } +void showAnotherY(Inner1 another) { sop("y is " + another.y); } +static Inner1 makeOne() { return new Inner1(); } + +Which is/are true? +A) showX can be added at line n1 +B) showY can be added at line n2 +C) showAnotherY can be added at line n2 +D) makeOne can be added at line n1 +E) makeOne can be added at line n2 + +Q) Given: +record Client(String name, int creditLimit) { + line n1 +} +Which can be added: +A) At line n1 + public Client(String name) { + this(name, 0); + } +B) at line n1 + public Client(String name, int creditLimit) { + super(); + this.name = name; + this.creditLimit = creditLimit; + } +C) as a separate declaration: + record SpecialClient(String name, int creditLimit, String greeting) + extends Client(name, creditLimit) {} +D) at line n1 + private Client() { + this("Unknown", 0); + } +E) at line n1 + Client {} diff --git a/src/main/java/enumstuff/Ex1.java b/src/main/java/enumstuff/Ex1.java new file mode 100644 index 0000000..3d4202b --- /dev/null +++ b/src/main/java/enumstuff/Ex1.java @@ -0,0 +1,30 @@ +package enumstuff; + +enum Day { + MONDAY(1), TUESDAY{}; + // enum constructors default to private and must be private + /*public*/ Day(int d) {} + /*public*/ Day() {} + + + @Override + public String toString() { + return "I'm an enum value is " + name(); + } +// public String name() {} //NOPE, name is final :) +} + +public class Ex1 { + public static void main(String[] args) { + System.out.println(Day.MONDAY); + System.out.println(Day.MONDAY.getClass()); + System.out.println(Day.TUESDAY.getClass()); + + Day m = Day.valueOf("MONDAY"); + + for (var d : Day.values()) { + System.out.println("> " + d); + System.out.println("ordinal is " + d.ordinal()); + } + } +} diff --git a/src/main/java/innerclasses/Access.java b/src/main/java/innerclasses/Access.java new file mode 100644 index 0000000..233ce8c --- /dev/null +++ b/src/main/java/innerclasses/Access.java @@ -0,0 +1,39 @@ +package innerclasses; + +class Outer1 { + class Inner1 { +// int x = 100; + private int y = 100; + // line n1 + Inner1 makeOne() { // this would work, Outer1.this is implicit +// static Inner1 makeOne() { // fails, no enclosing instance +// return new Inner1(); // fails with static context +// return new Outer1().new Inner1(); // OK with static or instance context + return /*Outer1.this.*/new Inner1(); // OK with static or instance context + } + void showX() { + System.out.println("x is " + /*Outer1.this.*/x); + } + } + class Inner2 { + void showAnotherY(Inner1 another) { + System.out.println("y is " + another.y); + } + } + private int x = 99; + // line n2 +// static Inner1 makeOne() { return new Inner1(); } +// void showY() { +// System.out.println("y is " + y); // NOPE, which Inner?? +// } + void showAnotherY(Inner1 another) { + System.out.println("y is " + another.y); + } +} + +// void showX() { sop("x is " + x); } +// void showY() { sop("y is " + y); } +// void showAnotherY(Inner1 another) { sop("y is " + another.y); } +//static Inner1 makeOne() { return new Inner1(); } +public class Access { +} diff --git a/src/main/java/innerclasses/AnonymousEx.java b/src/main/java/innerclasses/AnonymousEx.java new file mode 100644 index 0000000..c144b5c --- /dev/null +++ b/src/main/java/innerclasses/AnonymousEx.java @@ -0,0 +1,31 @@ +package innerclasses; + +import java.util.function.Predicate; + +public class AnonymousEx { + public void doStuff() { + int len = 3; + // parent type can be class, abstract class, or interface + // if parent is a class (incl abstract) we can pass + // constructor args + // Lifetime (also for lambdas) is governed by reachability and GC + // But, lifetime of len (above) would normally be method invocation + Predicate ps = new Predicate() { + // declare arbitrary fields, methods, etc. + + private int x = 99; + + @Override + public String toString() { + return "Hah, I'm a anonymous"; + } + + @Override + public boolean test(String s) { +// len ++; + return s.length() > len; // line n1 + } + }; + // line n2 + } +} diff --git a/src/main/java/recordstuff/Example.java b/src/main/java/recordstuff/Example.java new file mode 100644 index 0000000..91584de --- /dev/null +++ b/src/main/java/recordstuff/Example.java @@ -0,0 +1,42 @@ +package recordstuff; + +// purpose is "immutable" data carrier (named tuples? with extras) +// accessor methods created for each "component" +// has name of the component with empty parens, throws no checked exceptions +// has the type of the component (if varags then array) +// components create final fields with the same name +// components cannot have the name of an Object method (e.g. wait, toString :) +record Client(String name, int creditLimit) { // these two are "components" + // line n1 + // not permitted to add any instance fields +// private final int x = 99; + // are permitted static fields, don't even have to be final + private static int x; + // are permitted instance, "overriding", and static methods + + @Override // Not really "overriding" -- it's replacing + public String toString() { +// return super.toString(); // NOPE, there is no super... + return "I'm a client with credit " + creditLimit; + } +} + +public class Example { + public static void main(String[] args) { + Client c1 = new Client("Fred", 99); + Client c2 = new Client("Fred", 99); + Client c3 = new Client("Jim", 99); + System.out.println(c1); // toString :) + System.out.println(c1.name()); + System.out.println("equality"); + System.out.println(c1.equals(c2)); + System.out.println(c1.equals(c3)); + + System.out.println("hashCodes"); + System.out.println(c1.hashCode()); + System.out.println(c2.hashCode()); + System.out.println(c3.hashCode()); + + + } +}