Download CSC 162AB

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC110AB Program 3
Value: 30 points
Due: Thursday, 3/12/2009
Purpose
Student will compile and run a Java program using all three types of loops, if-else statements, relational and
logical operators, and mathematical calculations. You will also use some methods of Java’s built-in Math class.
Requirements (90%):
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!
-5
Please enter a non-negative integer.
Number cannot be negative!
6
Please enter a non-negative integer.
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 50 random numbers between 0 and the number (inclusive)
7 Find the sum of 0 up to your number (using a loop)
8 Find the square root of the number (using a Java method)
9 Find the square root of the number (using a loop, Extra Credit)
10 Find the factorial of the number (Extra Credit)
11 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.
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 do nothing if the user chooses it.
Examples:
A working version of the program is on the shared drive as 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 opening it (be
sure you look for “all files”). It will look like garbage, but you can still choose “Run Java Application” and it
will run.
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?”
“Find half of the number”
“Find the reciprocal of the
number”
“Raise the number to a power of
5 (using a Java method)”
“Raise the number to a power of
5 (using a loop)
“Generate 50 random numbers
between 0 and the number”
“Find the sum of 0 up to your
number (using a loop)”
If the number can be divided by 2 and the remainder is 0, then the number is even.
Otherwise, it is odd.
(self-explanatory)
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>.
Use Math.pow(n, e), where n is your number and e is the power to raise it to.
Use a loop to do repeated multiplication to get the power.
For example 35 = 1· 3 · 3 · 3 · 3 · 3
Create 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 50
times.
Use a loop to add to the total, and then print the total. You should not use this
formula, but it could have been calculated by:
“Find the square root of the
number (using a Java method)”
“Find the square root of the
number (using a loop, Extra
Credit)”
“Find the factorial of the number
(Extra Credit)”
( n)( n  1)
2
The square root of x can be found by telling the Math class to calculate it like this:
Math.sqrt(x)
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.
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.
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 don’t 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.
Extra Credit (Optional, 20%):
For extra credit you can do one of more of the following:
 Implement option #9 on the menu
 Implement option #10 on the menu
 Change the 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.
 Change the code so that the NUMBEROFRANDOMS (option #6) is stored as a constant, instead of
hard-coded as a 50. The constant should be used instead of the 50.
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.
Submitting your program:
Be sure that your program is named Program3.java. It should compile and run correctly. Try different test
input besides the input that is in the example. To submit it, go to the same Blackboard tab that has the
assignment materials and use the “Attach Local File” box to submit your Program3.java. Then at the bottom,
choose SUBMIT (instead of SAVE).