Midterm Java (2 & 7) Samples
Midterm Java (2 & 7) Samples
Option 1:
import java.util.Scanner;
public class MenuSelection {
public static void main(String[] args) {
System.out.println("Enter a menu selection");
System.out.println ( "1) Fish \n2) Chicken \n3) Beef \n4) Vegetable \n5) Burger" );
Scanner in = new Scanner(System.in);
String selection;
switch (in.nextInt() ) {
case 1: selection = "Fish";
break;
case 2: selection = "Chicken";
break;
case 3: selection = "Beef";
break;
case 4: selection = "Vegetable";
break;
case 5: selection = "Burger";
break;
default: selection = "Invalid Input";
break;
}
System.out.println("You selected " + selection);
}
}
Option 2:
import java.util.Scanner;
public class MenuSelection2
{
public void display_menu()
{
System.out.println("1) Fish \n2) Chicken \n3) Beef \n4) Vegetable \n5) Burger");
System.out.print("\nSelection: ");
}
public void question()
{
System.out.println("Would you like to proceed or quit?");
System.out.println("To proceed enter 6.");
System.out.println("If you wish to quit enter 0.");
Scanner q = new Scanner(System.in);
switch (q.nextInt())
{
case 0:
System.out.println ("Thank you and godbye.");
break;
case 6:
System.out.println ("Please proceed.");
new MenuSelection2();
break;
default:
System.err.println ( "Invalid Input" );
break;
}
public MenuSelection2()
{
Scanner in = new Scanner(System.in);
display_menu();
switch (in.nextInt())
{
case 1:
System.out.println ( "You selected Fish" );
question();
break;
case 2:
System.out.println ( "You selected Chicken" );
question();
break;
case 3:
System.out.println ( "You selected Beef" );
question();
break;
case 4:
System.out.println ( "You selected Vegetable" );
question();
break;
case 5:
System.out.println ( "You selected Burger" );
question();
break;
default:
System.err.println ( "Invalid Input" );
break;
Midterm Question 7:
import java.util.Scanner;
public class TestScore {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Test Score: ");
double x = in.nextDouble();
char letter = ' ';
if (x>=90)
letter = 'A';
else if (x>=80)
letter = 'B';
else if (x>=70)
letter = 'C';
else if (x>=60)
letter = 'D';
else
letter = 'F';
System.out.println("The letter grade of your score " + x + " is " + letter);
}
}