Please show me the code for this program using JAVA and please explain in depth.
ID: 3661773 • Letter: P
Question
Please show me the code for this program using JAVA and please explain in depth.
In MainApp, write a program which does the following...Print a text menu to the user with three options...
1 - Hello, World!
2 - Integer Divider
3 - Word Sorter
3 - Exit
After printing the menu the program should wait for input.
For "1" ask the user for their name and then print out "Hello, [name]!"
For "2" ask the user for two integers and print out the integer (whole number) result of dividing that first number by the second number as well as the remainder of that division. Do not worry about validating whether or not the user entered a valid integer.
For "3"...
Ask the user for any word and store it in an array.
Ask the user if they want to enter any more words("y" or "n").
If they enter "y" then repeat this process by (i.e. ask them for another word) until they select "n".
When they finally enter "n" sort the array and print the words back out in sorted order.
For "4" the program should end.
For any other input display an error message.
For any input other than 4 this process should repeat (i.e. display menu and wait for new input).
Here is an example of the expected output:
1 - Hello, World!
2 - Integer Divider
3 - Word Sorter
4 - Exit
1
What is your name?
Zack
Hello, Zack!
1 - Hello, World!
2 - Integer Divider
3 - Word Sorter
4 - Exit
2
Enter an integer: 7
Enter another integer: 3
The result is 2 and the remainder is 1.
1 - Hello, World!
2 - Integer Divider
3 - Word Sorter
4 - Exit
3
Enter a word: banana
Do you want to enter another word? (y/n) y
Enter a word: apple
Do you want to enter another word? (y/n) y
Enter a word: pear
Do you want to enter another word? (y/n) n
apple
banana
pear
1 - Hello, World!
2 - Integer Divider
3 - Word Sorter
4 - Exit
4
Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MainApp {
/**
* Scanner used for input within program
*/
public static Scanner scanner = new Scanner(System.in);
/**
* Main method that provides user with a menu in which each number
* represents a different method (e.g addtion) that they can carry out
*/
public static void main(String[] args) {
try {
// Declare variable for user's option and defaulting to 0
int menuOption = 0;
do {
// Setting menuOption equal to return value from showMenu();
menuOption = showMenu();
// Switching on the value given from user
switch (menuOption) {
case 1:
sayHello();
break;
case 2:
divide();
break;
case 3:
sortWords();
break;
case 4:
System.out.println("Quitting Program...");
break;
default:
System.out.println("Sorry, please enter valid Option");
}// End of switch statement
} while (menuOption != 4);
// Exiting message when user decides to quit Program
System.out.println("Thanks for using this Program...");
} catch (Exception ex) {
// Error Message
System.out.println("Sorry problem occured within Program");
// flushing scanner
scanner.next();
} finally {
// Ensuring that scanner will always be closed and cleaning up
// resources
scanner.close();
}
}// End of main Method
private static void sortWords() {
String option = "y";
ArrayList<String> list = new ArrayList<String>();
do {
System.out.printf("Enter a word");
String name = scanner.next();
list.add(name);
System.out.printf("Do you want to enter another word? (y/n)");
option=scanner.next();
} while (!option.toLowerCase().equals("n"));
Collections.sort(list);
System.out.println(list);
}
private static void sayHello() {
System.out.printf(" What is your name?");
String name = scanner.next();
System.out.printf(" hello "+name);
}
/**
* Method that prints menu to screen and gets returns user's option from
* menu
*
* @return Returns user Option
*/
public static int showMenu() {
// Declaring var for user option and defaulting to 0
int option = 0;
// Printing menu to screen
System.out.println("Menu:");
System.out.println("1. Hello, World!");
System.out.println("2.Integer Divider");
System.out.println("3.Word Sorter");
System.out.println("4. Exit");
// Getting user option from above menu
System.out.println("Enter Option from above...");
option = scanner.nextInt();
return option;
}// End of showMenu
public static void divide() {
int num1;
int num2;
System.out.printf(" Enter an integer");
num1 = scanner.nextInt();
System.out.printf(" Enter another integer");
num2 = scanner.nextInt();
System.out.println("The result is :"+ (num1 / num2)+" and the remainder is:" + (num1 % num2));
}
}// End of Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.