
Larry C. answered 04/24/14
Experienced programmer, analyst, database designer and administrator
Nick V.
asked 04/13/14Larry C. answered 04/24/14
Experienced programmer, analyst, database designer and administrator
Patrick B. answered 06/06/19
Math and computer tutor/teacher
// User Interface Utilities
import java.io.*;
class UI_Util
{
private Console console;
UI_Util()
{
console = System.console();
}
public int Menu ( String [] menuOptions, String menuTitle)
{
String inbuff;
int user_menu_choice=-1;
int N = menuOptions.length;
if (menuTitle == null) { menuTitle = new String(" MAIN MENU"); }
while ((user_menu_choice<0) || (user_menu_choice>N))
{
System.out.println("*********************************************");
System.out.println(menuTitle);
System.out.println("*********************************************");
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.println(" <" + (iLoop+1) + "> " + menuOptions[iLoop]);
}
System.out.println("*********************************************");
System.out.println(" OR INPUT ZER0 to EXIT the menu ");
System.out.println("*********************************************");
System.out.print(" Please input your choice :> ");
inbuff = console.readLine();
user_menu_choice = Integer.parseInt(inbuff);
}
return(user_menu_choice);
}
public int PromptForInteger( String promptMsg)
{
String inbuff;
int iReturn;
System.out.println("******************************************************");
System.out.print(promptMsg + ":> ");
inbuff = console.readLine();
iReturn = Integer.parseInt(inbuff);
return(iReturn);
}
}
//Driver.java
class Driver
{
public int Go()
{
int iReturn = 0;
int n;
UI_Util myUI_Util = new UI_Util();
String[] menuOptions = {"FACTORIAL","FIBONACCI","MULTIPLICATION TABLE"};
int user_menu_choice = -1;
do
{
user_menu_choice = myUI_Util.Menu(menuOptions,"MAIN MENU");
System.out.println("\n\n\n");
switch (user_menu_choice)
{
case 0:
{
System.out.println(" Goodbye \n");
break;
}
case 1:
{
n=myUI_Util.PromptForInteger(" Please input the integer of which to calculate the factorial");
Factorial myFactorial = new Factorial();
System.out.println( n + "! = " + myFactorial.factorial(n));
break;
}
case 2:
{
n=myUI_Util.PromptForInteger(" Please input the integer of which to calculate the fibonacci");
Fibonacci myFibonacci = new Fibonacci();
for (int iLoop=1; iLoop<=n; iLoop++)
{
System.out.print(myFibonacci.fibonacci(iLoop) + " ");
}
break;
}
case 3:
{
n=myUI_Util.PromptForInteger(" Please input the integer of which to show the multiples");
MultiplicationTable myMultiplicationTable = new MultiplicationTable(10);
myMultiplicationTable.Go(n);
System.out.println("\n\n");
break;
}
} //switch
System.out.println("\n");
} //while
while (user_menu_choice != 0);
return iReturn;
} //Go
public static void main(String args[])
{
Driver myDriver = new Driver();
myDriver.Go();
}
}
//Factorial.java
class Factorial
{
// should check n for possible overflow error
int factorial( int n)
{
int iReturn;
if (n==0)
{
iReturn = 1;
}
else
{
iReturn = n * factorial(n-1);
}
return(iReturn);
}
}
//class MultiplicationTable.java
class MultiplicationTable
{
private int nMax;
MultiplicationTable( int maxN) { nMax = maxN; }
public void Go ( int x)
{
for (int iLoop=1; iLoop<=nMax; iLoop++)
{
System.out.print( (iLoop * x) + " ");
}
}
}
//Fibonacci.java
class Fibonacci
{
// should check n for possible overflow error
int fibonacci( int n)
{
int iReturn;
if ((n==0) || (n==1))
{
iReturn = 1;
}
else
{
iReturn = fibonacci(n-1) + fibonacci(n-2);
}
return(iReturn);
}
}
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.