Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * An exercise to demonstrate methods of Wrapper Classes
- * @Liberty Computer Science A
- * @version March 26, 2019
- */
- public class WrapperMethods
- {
- public static void main(String[] args)
- {
- System.out.println("\nWRAPPER CLASS METHODS\n");
- // declare and initialize an int x with value = 15
- int x = 15;
- // declare and initialize an int y with value = 18
- int y = 18;
- // declare and initialize an int z with value = 5 * 3
- int z = 5 * 3;
- // print the values
- System.out.println("int VALUES:");
- System.out.println("x = " + x + ", y = " + y + ", z = " + z);
- // compare the primitive values x and y with == assign to boolean b1
- boolean b1 = (x == y);
- // print the result
- System.out.println("\n(x == y) is " + b1);
- // compare the primitive values x and z with == assign to boolean b2
- boolean b2 = (x == z);
- // print the result
- System.out.println("(x == z) is " + b2);
- // Autobox x, y, z into wrapper class variables xW, xW, xW
- Integer xW = new Integer(x);
- Integer yW = new Integer(y);
- Integer zW = new Integer(z);
- System.out.println("\nAUTOBOXED VALUES:");
- // print the values of xW, yW, zW applying the .toString() method
- System.out.println("The value of xW = " + xW.toString());
- System.out.println("The value of yW = " + yW);
- System.out.println("The value of zW = " + zW.toString());
- // compare the values xW and yW with the .equals() method assign to boolean b3
- boolean b3 = xW.equals(yW);
- // print the result
- System.out.println("\nxW equals yW: " + b3);
- // compare the values zW and xW with the .equals() method assign to boolean b4
- boolean b4 = zW.equals(xW);
- // print the result
- System.out.println("zW equals xW: " + b4);
- // compare the values xW and yW with the .compareTo() method assign to int i1
- int i1 = xW.compareTo(yW);
- // print the result
- System.out.println("\nxW compareTo yW: " + i1);
- // switch the order of yW and xW in .compareTo() method assign to int i2
- int i2 = yW.compareTo(xW);
- // print the result
- System.out.println("yW compareTo xW: " + i2);
- // compare the values zW and xW with the .compareTo() method assign to int i3
- int i3 = zW.compareTo(xW);
- // print the result
- System.out.println("\nzW compareTo xW: " + i3);
- // unbox the value of xW and assign it to the int a
- int a = xW.intValue();
- // unbox the value of xW and assign it to the int b
- int b = yW.intValue();
- // unbox the value of xW and assign it to the int c
- int c = zW.intValue();
- // print the unboxed values
- System.out.println("\nUNBOXED VALUES:");
- System.out.println("a = " + a + ", b = " + b + ", c = " + c);
- // multiply a * b, assign it to int d, and print the result
- int d = a * b;
- System.out.println("\na * b = " + d);
- // cube the value of c, assign it to int e, and print the result
- int e = c * c *c;
- System.out.println("c cubed = " + e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement