Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. class Box {
  2. int size;
  3. Box (int s) {
  4. size = s;
  5. }
  6. }
  7. public class Laser {
  8. public static void main(String[] args) {
  9. Box b1 = new Box(5);
  10. Box[] ba = go(b1, new Box(6));
  11. ba[0] = b1;
  12. for(Box b : ba)
  13. System.out.println(b.size + " ");
  14. }
  15.  
  16. static Box[] go (Box b1, Box b2) {
  17. b1.size = 4;
  18. Box[] ma = {b2, b1};
  19. return ma;
  20. }
  21. }
  22.  
  23. The important TWIST that you missed here is;
  24.  
  25. public static void main(String[] args) {
  26. Box b1 = new Box(5); // b1 == location A
  27. Box[] ba = go(b1, new Box(6)); // ba == location B which stores Location A, D
  28.  
  29. // PLEASE NOTE HERE
  30. // After the function go() is executed;
  31. // ba[] will have {D, A}
  32. // So ba[0] will have object b1 which is at location A.
  33.  
  34. ba[0] = b1; // location B will now store A and A
  35. for(Box b : ba)
  36. System.out.println(b.size + " "); // Output: 4 and 4
  37. }
  38.  
  39. static Box[] go (Box b1, Box b2) { // go(location A, location D )
  40. b1.size = 4; // A's object.size = 4
  41. Box[] ma = {b2, b1}; // position is interchanged here (D and A)
  42. return ma; // return the location of ma
  43. }
  44.  
  45. Box** go (Box *b1, Box *b2) {
  46. b1->size = 4;
  47. /* allocate an array of Box*, set it up, return it */
  48. }
  49.  
  50. b1 = &some_other_box;
  51.  
  52. public static void main(String[] args) {
  53. Box b1 = new Box(5);
  54. Change(b1);
  55. System.out.println(b1.size + " ");
  56.  
  57. static void Change(Box b1) {
  58. b1 = new Box(6);
  59. }
  60.  
  61. public static void main(String[] args) {
  62. Box b1 = new Box(5);
  63. Change(b1);
  64. System.out.println(b1.size + " ");
  65.  
  66. static void Change(Box b1) {
  67. b1.size = 6;
  68. }
  69.  
  70. public class Laser {
  71. public static void main(String[] args) {
  72. //Create a Box of size 5.
  73. Box b1 = new Box(5);
  74. //Create an array of Box objects, which are the results of the go method
  75. Box[] ba = go(b1, new Box(6));
  76. // ba now looks like {new Box(6), b1 /*b1 is size 4 now*/}
  77. // assign reference to b1 at index 0 in ba.
  78. ba[0] = b1;
  79. // array now looks like {b1, b1}
  80. for(Box b : ba)
  81. System.out.println(b.size + " ");
  82. }
  83.  
  84. static Box[] go (Box b1, Box b2) {
  85. //set the size of the first box to 4
  86. b1.size = 4;
  87. //create an array of boxes, with b2 in index 0 and b1 in index 1
  88. Box[] ma = {b2, b1};
  89. return ma;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement