Java Reference
In-Depth Information
7. Modify the RollDice program, Example 7-3, so that it allows the user to
enter the desired sum of the numbers to be rolled. Also allow the user to call
the rollDice method as many times as the user desires.
8. Write a program to test the method isPalindrome discussed in Example 7-4.
9. The following formula gives the distance between two points (x 1 , y 1 ) and
(x 2 , y 2 ) in the Cartesian plane:
ðx 2 x 1 Þ 2 þðy 2 y 1 Þ 2
q
:
Given the center and a point on a circle, you can use this formula to find the
radius of the circle. Write a program that prompts the user to enter the
center and a point on the circle. The program should then output the circle's
radius, diameter, circumference, and area. Your program must have at least
the following methods:
a. distance : This method takes as its parameters four numbers that represent two
points in the plane and returns the distance between them.
b. radius : This method takes as its parameters four numbers that represent the
center and a point on the circle, calls the method distance to find the
radius of the circle, and returns the circle's radius.
c. circumference : This method takes as its parameter a number that represents
the radius of the circle and returns the circle's circumference. (If r is the
radius, the circumference is 2pr.)
d. area : This method takes as its parameter a number that represents the radius of the
circle and returns the circle's area. (If r is the radius, the area is pr 2 .)
e. Assume that p ¼ 3.1416.
10. Rewrite the program in Programming Exercise 15 from Chapter 4 (cell
phone) so that it uses the following methods to calculate the billing amount.
(In this programming exercise, do not output the number of minutes during
which the service is used.)
a. regularBill : This method calculates and returns the billing amount for regular
service.
b. premiumBill : This method calculates and returns the billing amount for pre-
mium service.
11. A nonnegative integer is called a palindrome if it reads forward and backward
in the same way. For example, the numbers 5, 121, 3443, and 123454321 are
palindromes. Write a method that takes as input a nonnegative integer and
returns true if the number is a palindrome; otherwise, it returns false .(When
determining whether the number is a palindrome, do not convert the number
into a string.) Also write a program to test your method.
Search WWH ::




Custom Search