208
4.The algorithm for computing the date of Easter can be extended easily to work
with any year from 1900 to 2099. There are four years—1954, 1981, 2049, and
2076—for which the algorithm gives a date that is seven days later than it should
be. Modify the application for Programming Problem 3 to check for these years
and subtract 7 from the day of the month. This correction does not cause the
month to change. Be sure to change the documentation for the code to reflect its
broadened capabilities.
5.Write a Java application that calculates and prints the diameter, the
circumference, or the area of a circle, given the radius. The application should in-
put a character corresponding to one of three actions: D for diameter, C for
circumference, and A for area. The user should be prompted to enter the radius
in floating-point form and then the appropriate letter. The output should be
labeled appropriately. For example, if the input is 6.75and A, your application
should print something like this:
The area of a circle with radius 6.75 is 143.14.
Here are the formulas you need:
Diameter = 2r
Circumference = 2πr
Area of a circle = πr^2
where ris the radius. Use 3.14159265 for π.
6.The factorial of a number nis n* (n1) * (n2) *... * 2 * 1. Stirling’s formula ap-
proximates the factorial for large values of n:
where = 3.14159265 and e= 2.718282.
Write a Java application that inputs an integer value (but stores it into a
doublevariable n), calculates the factorial of nusing Stirling’s formula, assigns
the (rounded) result to a longinteger variable, and then displays the result
appropriately labeled.
Depending on the value of n, you should obtain one of these results:
A numerical result.
If nequals 0, the factorial is defined to be 1.
If nis less than 0, the factorial is undefined.
If nis too large, the result exceeds Long.MAX_VALUE.
Because Stirling’s formula is used to calculate the factorial of very large num-
bers, the factorial approaches Long.MAX_VALUEquickly. If the factorial exceeds
Long.MAX_VALUE, it causes an arithmetic overflow in the computer, in which case
nn^2 πn
en