Hello friends, I need help for this homework + extra credits. Assignment – Menu
ID: 3786987 • Letter: H
Question
Hello friends, I need help for this homework + extra credits.
Assignment – Menu Driven
Application
Design a program called IntFun.java which reads in a non-negative integer from the user. The user will then be prompted with a menu of choices (this menu should be repetitively displayed until the user chooses to quit): You must implement #1 and #5 and TWO of #2, #3,and #4. (SEE EXTRA CREDIT)
Your menu will include these choices (stub out the one you don’t implement.)
1. Enter a new number
2. Print the number of odd digits, even
digits and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
PROGRAM PARTICULARS:
. _When the program starts up, ask the user for a non-negative integer. After the user enters the non-negative integer, display the above menu. Remember the user can choose to do #2, #3, and #4 on the same number. Meaning, once you have the number from the user do not make the user enter a new number each time. The user can keep the same number until the user selects option 1 (see example output below.)
. _There must be error checking on the input integer: if it is negative, the program will print an error message and re-prompt. This process will continue until valid input is entered. You may assume an integer of some form will be entered by the user.
. _There must be error checking on the menu choice entered: if the user enters a choice not on the menu, the program will print an error message, re-display the menu and re-prompt. This process will continue until valid input is entered.
._No string variables are allowed.
. _No built-in methods for integer manipulation are allowed. You may use Math.pow( ) if you feel it is necessary.
. _You may assume that no integer entered will be greater than the maximum integer size for type int.
HINTS:
._First, solve the primary problems one at a time, testing each one separately. (Make sure that your algorithm works for single-digit numbers - including zero!). Design things modularly.
. _Solve the input and error checking problems once the others are solved and tested.
EXTRA CREDIT:
10 points: implement all three! (NOTE: if you implement the extra credit, be sure and document that you are doing so -- if you do not, you will not be given credit for it!)
TURN IN: (via the Canvas system)
A zip file - hopefully you understand the naming scheme.
This zip file will contain:
. _IntFun.java – Your code
GET STARTED ASAP
SAMPLE OUTPUT:
Welcome to Integer Fun. Please enter a non-negative integer --> -12
I am sorry that is not a non-negative integer. Please enter a non-negative integer --> 120
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zero digits in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> -6
I am sorry that is an invalid menu choice. Please try again
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> 2
Your results are: odd - 1 even - 1 zero(s) - 1
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> 4
The sum of the digits in the number 120 is 3
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> 1
Please enter a non-negative integer --> 10
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> 2
Your results are: odd - 1 even - 0 zero(s) - 1
Please select from the following menu choices.
1. Enter a new number
2. Print the number of odd, even and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
Choice --> 5
Thank you and have a nice day
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
public class IntFun
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int choice = 0;
int number = 0;
int flag = 0;
do //do loop to check choice at the end
{
//Menu
System.out.println("Please select from the following menu choices.");
System.out.println("1. Enter a new number");
System.out.println("2. Print the number of odd, even and zero digits in the integer");
System.out.println("3. Print the prime numbers between 2 and the integer");
System.out.println("4. Print the sum of the digits of the integer");
System.out.println("5. Quit the program");
choice = scan.nextInt(); //input choice
switch(choice)
{
case 1: System.out.println("Please enter a non-negative integer ");
number = scan.nextInt();
while(number < 0) //loop to coninuously input number if its negative
{
System.out.println("I am sorry that is not a non-negative integer. Please enter a non-negative integer");
number = scan.nextInt();
}
break;
case 2: int evens = 0, odds = 0, zeros = 0;
for (int temp = number; temp > 0; temp /= 10)
{
int digit = temp % 10; //extract each digit
if (digit == 0) //check if its zero
zeros++;
else if (digit % 2 == 0) //if digit is even
evens++;
else
odds++; //odd digit
}
System.out.println("Your results are: odd - "+odds+" even - "+evens +" zero(s) - "+zeros);
break;
case 3: System.out.println("Prime numbers between 2 and "+number);
for(int i=2;i<=number;i++) //outer loop for 2 to number
{
for (int j=2;j<i;j++)
{
if(i % j == 0) //if i is divisible by j it is not prime
{
flag = 0;
break;
}
else
{
flag = 1;
}
}
if(flag == 1)
{
System.out.println(i); //print prime numbers
}
}
break;
case 4: int sum = 0;
for (int temp = number; temp > 0; temp /= 10)
{
int digit = temp % 10;
sum = sum +digit; //sum of all digits
}
System.out.println("The sum of the digits in the number "+number +" is "+sum);
break;
case 5:
break;
default: System.out.println("I am sorry that is an invalid menu choice. Please try again");
break;
}
}while(choice != 5);
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.