This program will use your knowledge of all three types of loops, if-else statements, relational and logical operators, and mathematical calculations. You will also use some methods of Javas built-in Math class.

The program:

You are to write a program that does a number of mathematical calculations based on a (non-negative) integer that the user inputs. Your program logic (controlled by loops) will do the following:

Ask the user for a non-negative integer. As long as the user enters a negative integer, print a message and then get another input. For example:

Please enter a non-negative integer.
-3

Number cannot be negative! Please enter a non-negative integer.
-5

Number cannot be negative! Please enter a non-negative integer.
6

When it is correct, print a menu to let the user decide what to do with the number. The menu should look like this:

Please choose an option
0 Print the number
1 Determine if the number is odd or even
2 Find half of the number
3 Find the reciprocal of the number
4 Raise the number to the power of 5 (using a Java method)
5 Raise the number to the power of 5 (using a loop)
6 Generate 20 random numbers between 0 and the number (inclusive)
7 Find the sum of 0 up to your number (using a loop)
8 Find the factorial of the number (using a loop)
9 Find the square root of the number (using a Java method)
10 Find the square root of the number (using a loop, Extra Credit)
11 Determine whether the number is prime (using a loop, Extra Credit)
12 Exit the program

Get the menuChoice from the user. You will do different things depending on what the user chose. At this point, you can code the switch or if-elses. Once you have this right, just add code for each one of the possibilities.

If the user chose to exit the program, print a message of your choice and the program should end. In every other case, it print the menu again. This logic should be controlled by your loop.

Specific Requirements:

Input:

  • Be sure that the menu options are in the correct order so I can easily test it. If you do not implement some of the options, they should still appear in the menu and the program should specifically tell the user that it is not implemented.
  • If the user puts in an illegal option, be sure to print a message telling them that it is illegal.

Output:

  • The output from each option should clearly explain what it is displaying. They should also show the non-negative number that it is working with. For example, if the non-negative number was 5, the result of option #7 could be:
The sum of 0 to 5 is: 15
  • Be sure that there nothing is misspelled and the output does not run together ( like The sum of 0 to 5 is:15 )

Constants:

  • Implement your code so that the POWER (options #4, #5) is stored as a constant, instead of hard-coded as a 5. The constant should be used instead of the 5.
  • Implement your code so that the NUMBEROFRANDOMS (option #6) is stored as a constant, instead of hard-coded as a 20. The constant should be used instead of the 20.

Examples:

A working version of the program is in the project KMProgram3.class. It is a .class file, so it contains Java bytecode (almost ready for the computer definitely not for humans). You can run it by right-clicking the Netbeans KMProgram3.bat file and selecting Open in System.

Think about different tests to try. You should especially try 0 as your number. You can run tests on my version and on your program to compare the results.

Mathematical Calculations:

Details on the mathematical calculations are as follows:

  • Is the number odd or even?If the number can be divided by 2 and the remainder is 0, then the number is even. Otherwise, it is odd.
  • Find half of the number(self-explanatory)
  • Find the reciprocal of the number The reciprocal of x is defined as 1/x. If the number is 0, print a message that it has no reciprocal. Otherwise, do not do the calculation, but actually print 1/ + < the number>.
  • Raise the number to a power of 5 (using a Java method)Use Math.pow(n, e), where n is your number and e is the power to raise it to.
  • Raise the number to a power of 5 (using a loop)Use a loop to do repeated multiplication to get the power. For example 3^5 = 1 x 3 x 3 x 3 x 3 x 3
  • Generate 20 random numbers between 0 and the numberCreate an instance of the Random class as follows: Random rand = new Random(); Then you can tell it to a single random number between 0 and n by calling: rand.nextInt(n+1); Print the results of this statement. Put it in a loop so it will execute and print 20 times.
  • Find the sum of 0 up to your number (using a loop)Use a loop to add to the total, and then print the total. You should not use this formula, but it should come out to: (n)(n+1)/2
  • Find the factorial of the number The factorial of x is defined as x * (x-1) * (x-2) * * (3) * (2) * (1) Therefore, 3! is 6 and 5! is 120. Note that 0! is 1 by definition. To calculate factorial, you will have to use a loop and build up the answer. It will be similar to an accumulator (where we built up a total), except that we are multiplying each time instead of adding. Therefore, your initial value should be 1 instead of 0.
  • Find the square root of the number (using a Java method)The square root of x can be found by telling the Math class to calculate it like this: Math.sqrt(x)
  • Find the square root of the number (using a loop, Extra Credit)If the number is 0, the square root is 0.0. Otherwise, 1.Estimate the square root of the number (can just divide by 2 to get initial estimate) 2.Divide this estimate into the number. 3.Find the average of the quotient and the estimate. This will be come the new estimate. Use a loop to continue doing steps #2 and #3 until the estimate squared is within .000001 of the number. The estimate will be very close to the square root; print it.
  • Determine whether the number is prime (Extra Credit) A prime number is only divisible by 1 and itself. My suggestion is to have a boolean variable called isPrime, which is initialized to true. If the number is 0 or 1, then it is not prime by definition so change isPrime to false. Otherwise, you will have to see if it can be divided evenly by anything other than 1 and itself. So - if the number is x, you will have to loop through numbers from 2 up to x-1 (actually, just up to the square root of x is better), checking to see if any of them divide evenly into x. If so, isPrime is false. When finished, use the final value of isPrime to determine the result.

Notes:

1.I would first implement the input and the loop to display the menu and branch (see the demo on using a menu with a loop). Once you have the menu working, you can add code one option at a time. If you do not implement options, you should still have them in the menu and make the program do nothing.

2.I believe that you will use all 3 types of loops. See the examples on the shared drive, especially the example of how to implement a menu.

3.Be sure and test it with as much input as you can think of. Thinking up good test cases is a good skill to have. Your test cases should always include border cases what happens if I dont put in anything, what happens if I put in bad data, what happens if I put in different cases that I know will trigger an if statement, etc.

4.Be sure that the capture output option is not turned on when you run your program from TextPad. It will not work when you are also typing in input. You should be getting a black DOS window instead.

Comments and indenting:

At the top of the program should be a short comment which describes its purpose. It should also have your name and class on a separate line.

In the code itself, indent inside the class and then again inside main. Also indent inside if statements and the else part. Put a blank line and then a comment as needed to describe sections of code.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.