Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. // ************************************************************************************************************************
  2. // what's wrong with this code?
  3. // ************************************************************************************************************************
  4.  
  5. public class Person {
  6.  
  7. private String firstName;
  8. private String lastName;
  9.  
  10. public void setName(String firstName, String lastName) {
  11. this.firstName = firstName;
  12. this.lastName = lastName;
  13. }
  14.  
  15. public String getName() {
  16. return this.firstName + " " + this.lastName;
  17. }
  18.  
  19. public static void printName() {
  20. System.out.println(this.firstName + " " + this.lastName);
  21. }
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28. // ************************************************************************************************************************
  29. // rewrite the following as a lamba statement
  30. // ************************************************************************************************************************
  31.  
  32. new Thread(new Runnable() {
  33. @Override
  34. public void run() {
  35. System.out.println("Hello world!");
  36. }
  37. });
  38.  
  39.  
  40.  
  41.  
  42. // ************************************************************************************************************************
  43. // what is the value of the variable "i"?
  44. // ************************************************************************************************************************
  45.  
  46. byte[] bytes = {(byte)0x82, (byte)0x19, (byte)0x02};
  47. int i = (0xF & bytes[0]) + (bytes[1] >>> 4) + (bytes[2] | 0x01);
  48.  
  49.  
  50.  
  51.  
  52. // ************************************************************************************************************************
  53. // how would you rewrite the following with a method reference?
  54. // ************************************************************************************************************************
  55.  
  56. Arrays.asList("one", "two", "three").forEach(n -> System.out.println(n));
  57.  
  58.  
  59.  
  60.  
  61. // ************************************************************************************************************************
  62. // a. what's wrong with this code?
  63. // ************************************************************************************************************************
  64.  
  65. @FunctionalInterface
  66. public interface FooBar {
  67.  
  68. public void foo(int num);
  69.  
  70. public void bar(String str);
  71.  
  72. default void xar(String str) {
  73. System.out.println(str);
  74. }
  75.  
  76. }
  77.  
  78. // =============================================================================
  79. // b. explain the default keywork
  80. // =============================================================================
  81.  
  82.  
  83.  
  84.  
  85. // ************************************************************************************************************************
  86. // use the stream API to produce the desired result
  87. // ************************************************************************************************************************
  88.  
  89. public class Person {
  90.  
  91. @Getter
  92. private String name;
  93.  
  94. @Getter
  95. private int age;
  96.  
  97. Person(String name, int age) {
  98. this.name = name;
  99. this.age = age;
  100. }
  101.  
  102. }
  103.  
  104. List<Person> people = Arrays.asList(
  105. new Person("David", 22);
  106. new Person("Nancy", 37);
  107. new Person("Julie", 23);
  108. new Person("Joe", 15);
  109. new Person("Julie", 12);
  110. new Person("David", 38);
  111. new Person("Nancy", 22);
  112. new Person("Vicky", 72);
  113. new Person("Steve", 20);
  114. new Person("Ed", 89);
  115. );
  116.  
  117. // use the list of Person objects to create an ArrayList of sorted names
  118. // Conditions:
  119. // - age is greater than or equal to 20
  120. // - age is less than or equal to 80
  121. // - names are sorted alphbetically
  122. // - no name appears more than once
  123. // Result should be:
  124. // - ["David", "Julie", "Nancy", "Steve", "Vicky"];
  125.  
  126.  
  127.  
  128.  
  129. // ************************************************************************************************************************
  130. // a. what's wrong with this code?
  131. // ************************************************************************************************************************
  132.  
  133. int totalOdds = 0;
  134. IntStream.range(1, 9)
  135. .filter(n -> !(n % 2 == 0))
  136. .forEach(n -> totalOdds += n);
  137.  
  138. // =============================================================================
  139. // b. how would you refactor to maintain the lambda statements but make it
  140. // compile?
  141. // =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement