04-Week.docx

advertisement
Week 04 Assignment
We will create a program with multiple methods in this assignment. You will write methods that return
several different kinds of data: int, double, string, and void.
Your main program will invoke methods of both void and primitive data type. At least one of the
methods will invoke other methods.
Your class name should be Week4.
Your main program will begin by invoking a void method named identifyMe. The IdentifyMe method
will display your name, so when I am grading the program, I will be able to see who wrote it. The
methods should have one argument, a String, which will contain the author’s (your) name.
Your main program will then invoke an int method named getNonNegInt which will prompt the user to
enter an integer number, get that number, and return it. The method will have one argument, a string
that will prompt the user what data to enter. The program won’t actually do I/O, but it will use the
getDouble method described below, then confirm that the number entered was actually an integer . It
will confirm that the double result was actually a whole number by using a method called
isWholeNumber, a boolean method. If the number entered was not actually a non-negative integer,
getNonNegInt should prompt the user with an error message, and try again. When the user finally
enters a non-negative integer, getNonNegInt should return it.
The isWholeNumber method should have a single double argument, (which it may assume is nonnegative) and return a true if the number is an integer, and false if the number is not an integer.
Your main program will then invoke a double method named getDouble which will prompt the user to
enter a double number, get that number, and return it. The method will have one argument, a string
that will prompt the user what data to enter. This method will be used in both your main program, and
in the getNonNegInt method.
Finally, after inputting a non-negative integer and a double, your program will invoke a double method
which you will write, named calcPow. This method will have two arguments, the first is a double, and
the second is a non-negative integer. The calcPow method should return the value of the double raised
to the power of the integer argument. The calcPow method should use a for loop, and the “*=”
operator.
Finally, your main should invoke a void method (which you will write) named displayDouble. The
displayDouble method should have two arguments. The first is a string which will label the value, and
the second is a double which it will output.
Some sample output from my version:
Dave Straayer
Enter Exponent: 4
Enter the base: -1
-1.0^4= 1.0
Input must be a non-negative whole number.
Enter Exponent: 2
Enter the base: 7.7
7.7^2= 59.290000000000006
Dave Straayer
Enter Exponent:
Input must be a
Enter Exponent:
Enter the base:
9.0^4= 6561.0
4.5
non-negative whole number.
4.0
9
Dave Straayer
Enter Exponent: 20
Enter the base: 2
2.0^20= 1048576.0
Dave Straayer
Enter Exponent: 1.04
Input must be a non-negative whole number.
Enter Exponent: 240
Enter the base: 1.04
1.04^240= 12246.202363755847
Download