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
CSC 110AA
Assignment 3
Due Wed, March 11 (before class)
This program will use your knowledge of all three types of loops, if-else statements, relational and logical
operators, and mathematical calculations.
The problem:
You are to write a program that does a number of mathematical calculations based on a (positive) integer that
the user inputs. Your program logic (controlled by loops) will do the following:

Ask the user for a positive integer. As long as the user enters a non-positive integer, print a message
and then get another input. For example:
Please enter a positive number (integer).
-3

Number must be positive!
-1
Please enter a positive number (integer).
Number must be positive!
5
Please enter a positive number (integer).
Then 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 your number
1 Enter a new positive number (integer)
2 Find half of the number
3 Determine if the number is odd or even
4 Find the square root of the number
5 Find the reciprocal of the number
6 Raise the number to a power
7 Generate 6 random numbers between 1 and the number
8 Find the factorial of the number
9 Determine if the number is prime
10 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 chooses option #1 (enter a new positive integer), then use another loop to make them enter a
positive integer. This will be the same logic as when they entered the original number.

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.
The input and output should be formatted to look exactly like the examples.
Examples:
A working version of the program is on the shared drive as MyProg3d.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.
You can use this to try different inputs. A sample output is stored on the shared drive as prog3out.txt.
Mathematical Calculations:
Details on the mathematical calculations are as follows:
“Find half of the number”
“Is the number odd or even?”
“Find the square root of the
number”
“Find the reciprocal of the
number”
“Generate 6 random numbers
between 1 and the number”
“Raise the number to a power”
“Find the factorial of the
number”
“Is the number prime?”
(self-explanatory)
If the number can be divided by 2 and the remainder is 0, then the number is even.
Otherwise, it is odd.
The square root of x can be found by telling the Math class to calculate it like this:
Math.sqrt(x)
The reciprocal of x is defined as 1/x. Do not do the calculation, but actually print
“1/” + <the number>. Since the number is positive, we don’t have to worry about
n/0.
The way to generate one random number between 1 and n is by using
(int) (n * Math.random() + 1)
Print out 6 of them with a loop.
Ask the user for the exponent and then calculate the number raised to that power.
In Java, there is no operator to raise to a power. You have to tell the Math class to
use its pow method (don’t do this here) or use a for loop to do repeated
multiplication to get the power (use this method!).
For example 35 = 3 · 3 · 3 · 3 · 3
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.
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. You must implement the basic menu and control structure. If you do not implement one of the
Mathematical calculations, you will lose 2 points. The next calculation not implement will cost 4 points,
the next 8 points, etc.
2. You will have to use all 3 types of loops. See the examples on the shared drive.
3. Be sure and test it with all other input than the example above (although that is a good start). 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.
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.
Handing in the program:
Name your program Prog3.java. Test the program and make sure that it runs correctly. Please hand in a hard
copy of your code along with a 3.5” disk that is labeled with your name, class, and “Prog3” on it. The disk
should contain Prog3.java in it. If you have problems or will miss class that day, you can email it to me at
[email protected] and hand in the disk and hard copy later.