In: Computer Science
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters should display the following choices in this order: 1, 2, 6, 5. Words with an even number of letters should display the following choices in this order: 7, 8, 4, 3. Once this choice is made the appropriate “fortune”is displayed.
program Requirements: 1.You MUST use methods for this assignment. You are required to have the following methods, but you can add more if you desire.
a.private static String userSelection(Scanner input)
b.private static void displayColorMenu()
c.private static void displayEvenMenu()
d.private static void display OddMenu()
e.private static void displayFortune(String number)
2.Your output needs to match the example formatting exactly, as well as the fortunes listed below! Note: you will lose points for spelling, capitalization, etc. errors! There is a space after the colons and a return carriage after the “fortune line.
1.You will soon take a trip
2.You will have good luck today
3.You will go to a party soon
4.Wealth awaits you very soon
5.You will have many friends
6.Serious trouble will pass you by
7.You will get an 'A' on the future
8.You will get free food today
Sample Execution:
Please choose your color:
1. Red
2. Green
3. Blue
4. Yellow
1
Please choose your number:
1
2
6
5
1
You will soon take a trip
Program:
import java.io.*; //import
packages
import java.util.*;
public class Fortune // class Fortune
{
private static String userSelection(String
n) // call userSelection function
{
String ss="";
// initializing the string
if(n.equals("1"))
// checking condition with 1
ss="You will soon take a
trip"; //assign value
else if(n.equals("2"))
// checking condition with 2
ss="You will have good luck
today"; //assign value
else if(n.equals("3"))
// checking condition with 3
ss="You will go to a party
soon"; //assign value
else if(n.equals("4"))
// checking condition with 4
ss="Wealth awaits you very
soon"; //assign value
else if(n.equals("5"))
// checking condition with 5
ss="You will have many
friends"; //assign value
else if(n.equals("6"))
// checking condition with 6
ss="Serious trouble will pass you
by"; //assign value
else if(n.equals("7"))
// checking condition with 7
ss="You will get an 'A' on the
future"; //assign value
else if(n.equals("8"))
// checking condition with 8
ss="You will get free food
today"; //assign
value
return ss;
// return result
} // end function
private static void displayColorMenu() //
function displayColorMenu
{
System.out.println("1. Red\n2.
Green\n3. Blue\n4. Yellow"); //
display colors
Scanner sc=new
Scanner(System.in); // scanner
object
int n1;
System.out.println("Please choose
your color:"); // display message
n1=sc.nextInt();
// read value from user
if(n1==1 || n1==2)
// check condition
displayOddMenu();
// call displayOddMenu function
else if(n1==3 || n1==4)
// check condition
displayEvenMenu();
// call displayEvenMenu function
} // end
function
private static void displayEvenMenu() //
function displayEvenMenu
{
System.out.println("Please choose
your number:"); // display message
System.out.println("7\n8\n4\n3");
// display values
Scanner sc=new
Scanner(System.in);
// create scanner object
String n1;
n1=sc.next();
// read value from user
displayFortune(n1);
// call
displayFortune function
} //
end function
private static void displayOddMenu() //
function displayOddMenu
{
System.out.println("Please choose
your number:"); // display message
System.out.println("1\n2\n6\n5");
//display message
Scanner sc=new
Scanner(System.in); // scanner
object
String n1;
n1=sc.next();
// read value from user
displayFortune(n1);
// call displayFortune function
} //
end function
private static void displayFortune(String
number) // function displayFortune
{
System.out.println(userSelection(number));
// call userSelection function
} // end function
public static void main(String[] args) //
main function
{
displayColorMenu();
// call
displayColorMenu
} // end main
function
}
Program Screenshots:
OUTPUT SCREEN SHOT: