Skip to content

Commit

Permalink
Day 2 Break 4
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 9, 2023
1 parent 40d6f14 commit b54039d
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 1 deletion.
82 changes: 81 additions & 1 deletion Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,84 @@ C) method C is called
D) method D is called
E) method E is called

Q)
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<String> ps = new Predicate<String>() {
@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 {}
30 changes: 30 additions & 0 deletions src/main/java/enumstuff/Ex1.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/innerclasses/Access.java
Original file line number Diff line number Diff line change
@@ -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 {
}
31 changes: 31 additions & 0 deletions src/main/java/innerclasses/AnonymousEx.java
Original file line number Diff line number Diff line change
@@ -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<String> ps = new Predicate<String>() {
// 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
}
}
42 changes: 42 additions & 0 deletions src/main/java/recordstuff/Example.java
Original file line number Diff line number Diff line change
@@ -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());


}
}

0 comments on commit b54039d

Please sign in to comment.