Session 20
Session20.java
import java.util.*;
public class Session20 {
public static void main(String[] args) {
System.out.println("Session 20");
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
System.out.println("Q - Quit");
// setup Scanner
Scanner in = new Scanner(System.in);
System.out.print("Choice: ");
String choice = in.nextLine();
// switch choices
switch (choice) {
case "E1":
System.out.println("Example 1");
example1();
break;
case "E2":
System.out.println("Example 2");
example2();
break;
case "E3":
System.out.println("Example 3");
example3();
break;
case "E4":
System.out.println("Example 4");
example4();
break;
case "E5":
System.out.println("Example 5");
example5();
break;
case "E6":
System.out.println("Example 6");
example6();
break;
case "E7":
System.out.println("Example 7");
example7();
break;
case "Q":
System.out.println("Quitting..!");
break;
}
}
// example1 method
public static void example1() {
// Autoboxing: int automatically converted to Integer
// What the compiler actually does behind the scenes:
// Integer num = Integer.valueOf(42);
// Autoboxing: double automatically converted to Double
// Behind the scenes:
// Double price = Double.valueOf(19.99);
}
// example2 method
public static void example2() {
// Autoboxing lets us pass an int where Object is expected
// int 5 is autoboxed to Integer, then printed as 5
}
// printObject method
// example3 method
public static void example3() {
// Autoboxing
// Unboxing: Integer -> int
}
// example4 method
public static void example4() {
// d1 is a Double object
// d1 is UNBOXED to 7.5
}
// calculate method
// example5 method
public static void example5() {
// primitive double
// autoboxed to Double
// autoboxed to Double
// val2 and val3 are unboxed, then all three added as doubles
}
// example6 method
public static void example6() {
// Step 1: Autoboxing (int 10 -> Integer)
// Step 2: Unboxing (Integer -> int), then method returns 20
// Step 3: Autoboxing (int 20 -> Integer)
// Step 4: Prints 20
}
// timesTwo method
// example7 method
public static void example7() {
// age = 17 (int)
// gpa = 3.85 (double)
// value = 34.0 (double)
// String "34" is valid for parseDouble
}
}
S20Problem1.java
S20Problem2.java
S20Problem3.java
Session20v2.java
S20Problem4.java
S20Problem5.java
ReverseDigitsDigits.java
S20Problem7.java
S20Problem8.java
S20Problem9.java
S20Problem10.java
S20Problem11.java
S20Problem12.java
Last updated
Was this helpful?