Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /**
  2. * An exercise to demonstrate methods of Wrapper Classes
  3. * @Liberty Computer Science A
  4. * @version March 26, 2019
  5. */
  6. public class WrapperMethods
  7. {
  8. public static void main(String[] args)
  9. {
  10. System.out.println("\nWRAPPER CLASS METHODS\n");
  11.  
  12. // declare and initialize an int x with value = 15
  13. int x = 15;
  14. // declare and initialize an int y with value = 18
  15. int y = 18;
  16. // declare and initialize an int z with value = 5 * 3
  17. int z = 5 * 3;
  18.  
  19. // print the values
  20. System.out.println("int VALUES:");
  21. System.out.println("x = " + x + ", y = " + y + ", z = " + z);
  22.  
  23. // compare the primitive values x and y with == assign to boolean b1
  24. boolean b1 = (x == y);
  25. // print the result
  26. System.out.println("\n(x == y) is " + b1);
  27.  
  28. // compare the primitive values x and z with == assign to boolean b2
  29. boolean b2 = (x == z);
  30. // print the result
  31. System.out.println("(x == z) is " + b2);
  32.  
  33. // Autobox x, y, z into wrapper class variables xW, xW, xW
  34. Integer xW = new Integer(x);
  35. Integer yW = new Integer(y);
  36. Integer zW = new Integer(z);
  37.  
  38.  
  39.  
  40.  
  41. System.out.println("\nAUTOBOXED VALUES:");
  42. // print the values of xW, yW, zW applying the .toString() method
  43. System.out.println("The value of xW = " + xW.toString());
  44. System.out.println("The value of yW = " + yW);
  45. System.out.println("The value of zW = " + zW.toString());
  46.  
  47. // compare the values xW and yW with the .equals() method assign to boolean b3
  48. boolean b3 = xW.equals(yW);
  49. // print the result
  50. System.out.println("\nxW equals yW: " + b3);
  51.  
  52. // compare the values zW and xW with the .equals() method assign to boolean b4
  53. boolean b4 = zW.equals(xW);
  54. // print the result
  55. System.out.println("zW equals xW: " + b4);
  56.  
  57. // compare the values xW and yW with the .compareTo() method assign to int i1
  58. int i1 = xW.compareTo(yW);
  59. // print the result
  60. System.out.println("\nxW compareTo yW: " + i1);
  61.  
  62. // switch the order of yW and xW in .compareTo() method assign to int i2
  63. int i2 = yW.compareTo(xW);
  64. // print the result
  65. System.out.println("yW compareTo xW: " + i2);
  66.  
  67. // compare the values zW and xW with the .compareTo() method assign to int i3
  68. int i3 = zW.compareTo(xW);
  69. // print the result
  70. System.out.println("\nzW compareTo xW: " + i3);
  71.  
  72. // unbox the value of xW and assign it to the int a
  73. int a = xW.intValue();
  74.  
  75. // unbox the value of xW and assign it to the int b
  76. int b = yW.intValue();
  77.  
  78. // unbox the value of xW and assign it to the int c
  79. int c = zW.intValue();
  80.  
  81. // print the unboxed values
  82. System.out.println("\nUNBOXED VALUES:");
  83. System.out.println("a = " + a + ", b = " + b + ", c = " + c);
  84.  
  85. // multiply a * b, assign it to int d, and print the result
  86. int d = a * b;
  87. System.out.println("\na * b = " + d);
  88.  
  89. // cube the value of c, assign it to int e, and print the result
  90. int e = c * c *c;
  91. System.out.println("c cubed = " + e);
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement