Inactive Tutor answered 04/24/14
Nick V.
asked 04/13/14Help Creating Java Program Menu, Classes, and Quit function
• There should be one class that contains the main method and that should also display the menu from where user can choose the options. So there should be total 4 java files.
• You must use proper naming convention for classes, objects, variables, etc.
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = s.nextInt();
fibonacci(n);
}
public static void fibonacci(int n) {
if (n == 0) {
System.out.println("0");
} else if (n == 1) {
System.out.println("0 1");
} else {
System.out.print("0 1 ");
int a = 0;
int b = 1;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
2 Answers By Expert Tutors
Inactive Tutor answered 06/06/19
// 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);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.