Search 75,802 tutors
FIND TUTORS
Ask a question
0 0

Help Creating Java Program Menu, Classes, and Quit function

I am creating a java program and I do not know how to put my 3 functions into it.
I also need some sort of menu screen and a way to quit the program.
These are my only guidelines:
 
• You must create separate class for the each 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.
 
Here are the 3 functions.
 
Programs 1 :


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

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;
 
 
Program 3:
 
 
import java.util.Scanner;

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));
}
}
 
 
Tutors, please sign in to answer this question.

1 Answer

Make class Factorial as separate class file of its own that is passed and returns an integer. Create a new class file called Menu that contains main(). Its function should simply be to give the user the choice of 4 options: determine a factorial, print a Fibonacci sequence, create a multiplication table or quit. Use a switch to invoke the appropriate class function, quit or tell the user it's an invalid choice and put the whole thing in a while loop that runs until the user selects 'quit'. The rest of the class files should require minimal tweaking other than the factorial one (since it's now being invoked rather than part of main)