0% found this document useful (0 votes)
27 views4 pages

Midterm Java (2 & 7) Samples

The document contains two code examples for menu selection programs in Java. Option 1 displays a menu and uses a switch statement to set a selection variable based on the user's input. It prints the selected item. Option 2 is more complex - it displays a menu, gets input, prints the selection, then asks if the user wants to continue or quit. Based on the response, it either runs the menu code again or prints goodbye. It uses multiple methods, switch statements, and object instantiation to achieve this. The second example also checks test scores entered by the user and assigns a letter grade based on ranges of scores using if/else statements. It prints the score and corresponding grade.

Uploaded by

cs141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Midterm Java (2 & 7) Samples

The document contains two code examples for menu selection programs in Java. Option 1 displays a menu and uses a switch statement to set a selection variable based on the user's input. It prints the selected item. Option 2 is more complex - it displays a menu, gets input, prints the selection, then asks if the user wants to continue or quit. Based on the response, it either runs the menu code again or prints goodbye. It uses multiple methods, switch statements, and object instantiation to achieve this. The second example also checks test scores entered by the user and assigns a letter grade based on ranges of scores using if/else statements. It prints the score and corresponding grade.

Uploaded by

cs141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Midterm Question 2:

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;

public static void main (String[]args)


{
new MenuSelection2();
}

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);
}
}

You might also like