Programming and Problem Solving with Java

(やまだぃちぅ) #1
209

the program continues with a strange-looking integer result, perhaps a negative
value. Before you write the application, then, you first must write a small appli-
cation that lets you determine, by trial and error, the largest value of nfor which
Java can compute a factorial using Stirling’s formula. After you’ve determined
this value, you can write the application using nested ifs that display different
messages depending on the value of n. If nis within the acceptable range, output
the number and the result with an appropriate message. If nis 0, write the mes-
sage, “The number is 0. The factorial is 1.” If the number is less than 0, write “The
number is less than 0. The factorial is undefined.” If the number is greater than
the largest value of nfor which Java can compute a factorial, write “The number
is too large.”
Hint:Don’t compute Stirling’s formula directly. The values of nnand encan be
huge, even in floating-point form. Take the natural logarithm of the formula and
manipulate it algebraically to work with more reasonable floating-point values.
If ris the result of these intermediate calculations, the final result is er. Make use
of the standard library methods Math.logand Math.exp. These methods compute
the natural logarithm and natural exponentiation, respectively.

Case Study Follow-Up


1.Add support for a second middle name, a prefix, and a suffix to the Nameclass.
The fullmethod should now include these parts of the name. You should add
knowmethods to return these new parts. You may optionally provide methods for
different name formats based on the extended Nameclass.
2.Change the Studentclass to include an INVALIDstatus constant, and set it to
invalid when any of the scores are outside of the range of 0 to 100.
3.Change the StudentStatusapplication so that it prints an appropriate message
when the input data are invalid.
4.Write a test plan to fully test theStudentStatusapplication and implement the
test plan.
5.Write a driver application that is designed specifically to test all aspects of the
Studentclass, and implement the test plan associated with the driver.
6.Add a method to the Studentclass that enables it to compare the status of two
students in the same manner that compareTodoes for names or string. Call the
method compareStatusTo, and have it take another Studentobject as an argument.
It should return 1 if the other student’s status is greater, 0 if it is equal, and 1 if
it is less than the instance’s status.
7.AddequalsandcompareTomethods to theStudentclass that compare two student
records. In the case ofequals, the method should returntrueif the names and
scores are equal. ThecompareTomethod should test for alphabetical ordering of
the student names. (Hint:You’ll be glad we implementedcompareTofor theName
class.)
Free download pdf