Homework Seven
Due Friday, 27 July 2001 at 8:00AM
Big Stinkin' Note:It's in your best interests to do the Java
lab before you begin this assignment. The lab covers installing Java
(and if you've got any sense you'll install EMACS too).
Part One
60 points
Download the file HomeworkSeven.java and
complete the methods. Since we're such kind folks here at 1321, we'll
even get you started.
- Make sure you've got Java installed correctly from the Java lab.
Go ahead and compile HomeworkSeven.java and then run the resulting
class file (remember, to run the class file you type java
HomeworkSeven, not java HomeworkSeven.class). You should see
"Fib(1)=0" (without the quotes). If you don't then go back to the lab
and get Java working before you try to continue.
- Insert your ident box where indicated (enlightened EMACS users
will remember how to do this from the EMACS lab).
- Let's step through writing the factorialDoWhile method:
- We know we'll have to use a do-while loop, so let's go ahead
and add that:
public static int factorialDoWhile(int n) {
do
{
} while ( );
/* this line is to make the compiler happy.
feel free to change it to something more appropriate */
return 0;
}//end int factorialDoWhile(int n)
- Now, a do-while loop is going to do whatever's in the
curly-braces while the condition in the parenthesis is
true. So how can we state factorial in these terms? Remember,
we're thinking procedurally now. How about this algorithm:
- Begin with an answer variable set to one
- Begin with a counter variable set to zero
- Set the answer to be the answer times the counter
- Increment the counter by one
- Do steps 3 and 4 while the counter is not greater than the
number we want the factorial of.
- Answer now holds the factorial of the number.
Pull out a pencil and some paper and convince yourself that this
algorithm gives you the factorial of n. This is important! If you
don't have a correct algorithm you can't hope to get a correct
method written.
- So let's start with steps one and two. We'll need two
variables to hold numbers. What kind of numbers? Well, factorial
(for this class) is only defined for integers, and the method
header says that we'll be returning an int, so let's use ints for
our counters. Also, we want to start them off at one and zero, so
we can declare and initialize them at the same time (before the do
loop):
int ans = 1;
int counter = 1;
- Steps three and four are what we want to do each time,
so they'll go inside the curly braces of the loop:
do
{
ans = ans * counter;
counter = counter + 1;
}
- That condition in number five sounds like the test that should
go in the parenthesis after the while. Notice that "not greater
than" is mathematically equivalent to "less than or equal to", so
our test looks like this:
while ( counter <= n );
- So now that we have the factorial stored in the answer
variable, we need to return it. How do we do that? We just use
"return," so let's change the zero to something useful:
return ans;
- So now that we've put our high-level English algorithm into
Java syntax, save the file, compile, and run. Enlightened EMACS
users can simply use C-c C-v C-r to do this.
- Nothing interesting happened. Why's that? Remember that
unlike Scheme, Java doesn't have an interactive interpreter, so we
can't interactively try different function calls. We'll have to
instead put some test cases into the test main:
System.out.println("factorialDoWhile(1)=" + factorialDoWhile(1));
System.out.println("factorialDoWhile(2)=" + factorialDoWhile(2));
System.out.println("factorialDoWhile(3)=" + factorialDoWhile(3));
etc...
- Now compile and run the class again and you should see
something like:
factorialDoWhile(1)=1
factorialDoWhile(2)=2
factorialDoWhile(3)=6
- Now you should be able to complete the rest of the methods on your
own. Happy Java'ing!
Part Two
40 points
For the second part of this assignment you fill be creating a class
called Circle. Your class must include the following:
- Your ident box
- A private float instance variable for the radius
- Public instance methods named getRadius and setRadius which get and
set the value of the Circle's radius.
- A public instance method named area which takes no
parameters and returns the area of the Circle (as a float). Use
3.14 to approximate pi.
- A public instance method named circumference which takes
no parameters and returns the circumference of the circle (as a
float). Use 3.14 to approximate pi.
- A constructor which takes in a float value and creates a new
Circle with the given value as the radius.
- A test main which does the following (in this order):
- Creates a new Circle using the default constructor.
- Sets the value of the radius to some reasonable test value.
- Prints the value of the radius using the getRadius method.
- Prints the Circle's area using the area method.
- Prints the Circle's circumference using the circumference
method.
- Makes another Circle using the constructor you wrote (give it
a reasonable test value for the radius).
- Prints the values of the radius, area, and circumference as
before.
- Any other testing you feel is appropriate.
When you're finished, turn in both .java files via Webwork. Then retrieve
them and make doubly sure that you submitted both files. We will
entertain no appeals for mercy from folks who forget to submit both
files. If you're unsure how to submit multiple files then look back
at the Webwork lab. Do NOT submit .class files to Webwork.
Submitting a .class file instead of a .java file will result in NO
credit for that part. Don't forget to put your ident box at the top
of BOTH files. Failure to put your ident box at the top of a
file will be 5 points off of that part. In order to receive credit
for a part of this assignment, your .java file MUST COMPILE.
If your file doesn't compile then you won't receive credit, simple as
that. Your TA will not fix errors that keep your file from
compiling. Not even if it's one stupid semi-color or curly-brace.
MAKE SURE THAT THE FILES YOU RETRIEVE COMPILE. This cannot be
stressed enough. Not compiling is a sure way to get a zero and end
the semester on a crummy note. Don't let it happen to you.