Question
The project reads in a non-negative integer (1 or greater) from theuser. The user is prompted with a menu of choices. The menu is displayed
The project reads in a non-negative integer (1 or greater) from theuser. The user is prompted with a menu of choices. The menu is displayed repetitively until theuser chooses to quit,The menu choices are1. Enter a new number (n)2. Print the sum of odd numbers between 1 and the integer (n inclusive)3. Print the even numbers between O and the integer (n inclusive)4. Print the prime numbers between 2 and the integer (n inclusive)5. Quit the programPROGRAM PARTICULARS:? I have provided some starting code. The java file contains comments about what you need to do.When the program starts up, the user is asked for a positive integer. Using a do while loop ensureit is positive.? There must be error checking on the input integer: if it is negative or 0, the program will print anerror message and re-prompt. This process will continue until valid input is entered.? There must be error checking on the menu choice entered: if the user enters a choice not on themenu, the program will print an error message, re-display the menu and re-prompt. This processwill continue until valid input is entered. You must use a do while for this input checking? No string or array variables are allowed.? No built-in methods for integer manipulation are allowed.? No break or continue statements other than those for the switch statement.? You may use System and Scanner but NO other classes, including you can't use the Math class.
664 BOD + h +*** package assignment.pkg6; import java.util.Scanner; public class Assignment6 { public static void main(String[] args) { // TODO code application logic here Scanner inp = new Scanner(System.in); int choice = 1, theNum; //you can add more variables System.out.print("Please enter a positive number "); theNum =inp.nextInt(); // use a while loop to ensure theNum is greater than 0 // Your code goes below to the the do do { // use a do while loop to ensure choice is in the range of 1-5 inclusive // Your code goes below to the the switch System.out.println(); System.out.println("Please choose from the following"); System.out.println("1) Enter a new number"); System.out.println("2) Print the sum of odd numbers between 1 and the integer (theNum inclusive)"); System.out.println("3) Print the even numbers between 0 and the integer (theNum inclusive)"); System.out.println("4) Print the prime numbers from 2 up to and possibly including theNum"); System.out.println("5) Quit"); System.out.print("Choice --> "); // read in choice here switch(choice) { case 1: // read a new positive theNum case 2: // Your code goes below to the break break; // using a loop to estimate the sum of odd values // Your code goes below to the break break; case 3: // using a loop to display the even values // Your code goes below to the break break; case 4: // using nested for loops print the prime numbers // from 2 up to and possibly including theNum // Your code goes below to the break break; default: System.out.println("Good Bye"); }//end switch while(choice = 5); } }
Step by Step Solution
3.55 Rating (152 Votes )
There are 3 Steps involved in it
Step: 1
The provided code snippet is a starting point for a Java program that meets the requirements you outlined Heres a breakdown of the code and the missin...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started