Source Code
Session4.java
import java.util.*;
public class Session4 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Session 4" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
// 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");
break;
case "E2":
System.out.println("Example 2");
break;
case "E3":
System.out.println("Example 3");
break;
case "E4":
System.out.println("Example 4");
break;
case "E5":
System.out.println("Example 5");
break;
case "E6":
System.out.println("Example 6");
break;
case "E7":
System.out.println("Example 7");
break;
case "E8":
System.out.println("Example 8");
break;
case "E9":
System.out.println("Example 9");
}
}
}
Session4v2.java
import java.util.*;
public class Session4v2 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Session 4v2" + " - " + myName);
// Menu
System.out.println("Menu");
System.out.println("E1 - Example 1");
// 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");
// call exampleMethod
break;
case "E2":
System.out.println("Example 2");
// call exampleMethod twice
break;
case "E3":
System.out.println("Example 3");
// call greeting method
break;
case "E4":
System.out.println("Example 4");
break;
}
}
// define exampleMethod
public static void exampleMethod() {
System.out.println("Hello from Example Method");
}
// define greeting method
public static void greeting(String name) {
System.out.println("Hello, " + name);
}
}
Last updated