Skip to content

Commit

Permalink
Day 3 break 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 10, 2023
1 parent 995e08b commit eac32a2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,28 @@ B) Exception at runtime
C) Hello!, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye!
E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye!

Q) Given:
interface StudentCriterion {
boolean test(Student s);
}

and:
class Student {
public int getGpa() { return 0; }
public List<String> getCourses() { return null; }
}

Which are legal (individually):
A) StudentCriterion sc = Student s -> { return s.getGpa() > 3;};
B) StudentCriterion sc = s -> { s.getGpa() > 3 };
C) Object sc = s -> {return s.getGpa() > 3;};
D) StudentCriterion sc = (s) -> s.getGpa() > 3;
E) StudentCriterion sc = s -> s.getCourses().size();

Q) Which are legal individually:
A) Function<String, String> fss = s -> { System.out.println(s); };
B) Consumer<String> cs = s -> { System.out.println(s); };
C) Supplier<String> ss = (s1, s2) -> s1 + s2;
D) BiPredicate<String> bps = (String s) -> { return s.length() > 0; };
E) ToIntFunction<String> tofs = s -> s.length();
76 changes: 76 additions & 0 deletions src/main/java/lambdas/Demos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package lambdas;

import java.util.List;
import java.util.function.ToIntFunction;

interface StudentCriterion {
boolean test(Student s);
}

class Student {
public int getGpa() { return 0; }
public List<String> getCourses() { return null; }
}

class SmartStudent implements StudentCriterion {
@Override
public boolean test(Student s) {
return s.getGpa() > 3;
}
}

public class Demos {
public static void main(String[] args) {
StudentCriterion sc = new SmartStudent();
// lambda MUST implement an interface
// ** context MUST make that interface clear
// and that interface MUST have ONE abstract method
// we MUST ONLY desire to provide implementation of THAT ONE method
// compiler will:
// write the class using this method
// instantiate it :)
// StudentCriterion sc2 = (Student s) -> { return s.getGpa() > 3; };
// if the argument type is also inferrable, we can leave it out
// StudentCriterion sc2 = (s) -> { return s.getGpa() > 3; };
// sometimes, we want to annotate the formal parameter
// StudentCriterion sc2 = (@Deprecated Student s) -> { return s.getGpa() > 3; };
// this does NOT work (annotation goes with the TYPE not the formal param
// StudentCriterion sc2 = (@Deprecated s) -> { return s.getGpa() > 3; };
// StudentCriterion sc2 = (@Deprecated var s) -> { return s.getGpa() > 3; };
// StudentCriterion sc2 = (var s) -> { return s.getGpa() > 3; };
// BUT, you must choose:
// All formal params, with no type, with explicit type, or with var

// IF (but only if) you have a SINGLE formal param, with NO TYPE specifier
// we can leave off the parens (there's nothing to "group")
// StudentCriterion sc2 = s -> { return s.getGpa() > 3; };

// OFTEN, our method body simply returns an expression
// In which case we can leave out "clutter"
// curlies "group" stuff, but we have only one thing-- leave out
// return is unnecessary
// must do both changes at once :)
// StudentCriterion sc2 = s -> s.getGpa() > 3 ;
// StudentCriterion sc2 = s -> Boolean.valueOf(s.getGpa() > 3) ;
StudentCriterion sc2 = s -> {
Boolean b = null;
return b;
};

// CANNOT DO THIS!
// Object obj = (Student s) -> {
// return s.getGpa() > 3;
// };


// statement expression, value we can ignore
// boolean b = List.of().add(0);
// List.of().add(0);
// 3 + 2;\

ToIntFunction<String> tofs = s -> Integer.valueOf(s.length());
// Long lng = 99; // Nope
Byte b = null;
long lng = b;
}
}

0 comments on commit eac32a2

Please sign in to comment.