Java Program Write a menu driven test program that prompts the user to choose wh
ID: 3702669 • Letter: J
Question
Java Program
Write a menu driven test program that prompts the user to choose which of your methods to test
out or to exit. The user is then to enter his/her choice:
1. In the case of string permutation they enter a string and the program displays all its permutations,
then returns to the menu.
2. In the case of the hex the user enters a hex string and the program displays its decimal equivalent,
then returns to the menu.
3. In the case of the binary the user to enters a binary string and the program displays its decimal
equivalent, then returns to the menu.
4. In the case of the decimal the user to enters a decimal number and the program displays its hex
equivalent, then returns to the menu.
5. Boundary, recursive programs can quickly get out of hand complexity wise, Prompt the user at
the start of your program to set an upper bound on input size, warm them of time delay in case
of large inputs (say greater than 6 digits or chars).
6. In the case of exit, display how much time elapsed and the number of recursive cals per function requested
(bonus 1 point), then the program is to exit without leaking any resources...
Explanation / Answer
import java.util.Scanner;
public class MenuDriven
{
//class for finding permutation of string
public static void permutation( char[] characters, int k)
{
if(k==characters.length)
{
for (int i = 0; i < characters.length; i++)
{
System.out.print(characters[i])
}
System.out.println();
}
else
{
for (int i = k; i < characters.length; i++)
{
char temp = characters[k];
characters[k] = a[i];
characters[i] = temp;
permutation(characters, k + 1); //recursive function
temp = a[k];
characters[k]=characters[i];
characters[i] = temp;
}
}
}
public static void main(String[] args)
{
char ch;
//declare input as scanner
Scanner input = new Scnner(system.in);
//display input values
System.out.println("a.string permutation");
System.out.println("b.hex to decimal");
System.out.println("c.binary to decimal");
System.out.println("d.decimal to hex");
System.out.println("e.exit");
String s = input.ext();
ch =s.charAt(0);
//add switch statement
switch(ch)
{
case 'a' :long begintime = System.currentTimeMillis();//giving current time
System.out.println("Enter the length of character string: ");
int len = input.nextInt(); //receve number
char[] characters = new char[len]; //dclaring array characters
System.out.println("Enter the original string: ");
for (int i = 0; i < len; i++)
characters[i] = input.next().charAt(0)
System.out.println("The permuted strings are: ");
permutation(characters, 0);
long endtime = System.currentTimeMillis(); //giving end time of execution
long etime = endtime-begintime; //gives total time taken for execution
break();
case 'b' :long begintime = System.currentTimeMillis();
System.out.println("enter string for convertion");
hex = input.next().charAt(0)
int decimal=Integer.parseInt(hex,16); //convert hex to decimal
System.out.println("decimal value is : "+decimal);
long endtime = System.currentTimeMillis();
long etime = endtime-begintime;
break();
case 'c' : long begintime = System.currentTimeMillis();
System.out.println("enter binary number");
String binaryval =input.nextLine();
System.out.println("Decimal value is: "+Integer.parseInt(binaryval,2));//convert binary decimal
long endtime = System.currentTimeMillis();
long etime = endtime-begintime;
break();
case 'd' : long begintime = System.currentTimeMillis();
System.out.println("enter decimal value for convertion");
int dec=input.nextInt();
String hexvalue = Integer.toHexString(dec); //convert decimal to hexadecimal
System.out.println("hexadecimal value is : "+hexvalue);
long endtime = System.currentTimeMillis();
long etime = endtime-begintime;
break();
case 'e' : System.out.println("exit");
System.out.println("elapsed time is:"+etime);
}
}
}
output :
(1)
a.string permutation
b.hex to decimal
c.binary to decimal
d.decimal to hex
e.exit
a
Enter the length of character string: 3
Enter the original string: h e y
The permuted strings are:
hey
hye
ehy
eyh
yeh
yhe
(2)
a.string permutation
b.hex to decimal
c.binary to decimal
d.decimal to hex
e.exit
b
enter string for convertion:a
decimal value is :10
(3)
a.string permutation
b.hex to decimal
c.binary to decimal
d.decimal to hex
e.exit
c
enter binary number : 1010
Decimal value is: 10
(4)
a.string permutation
b.hex to decimal
c.binary to decimal
d.decimal to hex
e.exit
d
enter decimal value for convertion : 123
hexadecimal value is : 7b
(5)
a.string permutation
b.hex to decimal
c.binary to decimal
d.decimal to hex
e.exit
e
exit
elapsed time is:0.05seconds
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.