Java Reference
In-Depth Information
c) Use the methods developed in parts (a) and (b) to write a method displayDigits that
receives an integer between 1 and 99999 and displays it as a sequence of digits, separating
each pair of digits by two spaces. For example, the integer 4562 should appear as
4 5 6 2
Incorporate the methods into an application that inputs an integer and calls display-
Digits by passing the method the integer entered. Display the results.
6.22 (Temperature Conversions) Implement the following integer methods:
a) Method celsius returns the Celsius equivalent of a Fahrenheit temperature, using the
calculation
celsius = 5.0 / 9.0 * (fahrenheit - 32 );
b) Method fahrenheit returns the Fahrenheit equivalent of a Celsius temperature, using
the calculation
fahrenheit = 9.0 / 5.0 * celsius + 32 ;
c) Use the methods from parts (a) and (b) to write an application that enables the user ei-
ther to enter a Fahrenheit temperature and display the Celsius equivalent or to enter a
Celsius temperature and display the Fahrenheit equivalent.
6.23 (Find the Minimum) Write a method minimum3 that returns the smallest of three floating-
point numbers. Use the Math.min method to implement minimum3 . Incorporate the method into an
application that reads three values from the user, determines the smallest value and displays the re-
sult.
6.24 (Perfect Numbers) An integer number is said to be a perfect number if its factors, including
1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 =
1 + 2 + 3. Write a method isPerfect that determines whether parameter number is a perfect number.
Use this method in an application that displays all the perfect numbers between 1 and 1000. Display
the factors of each perfect number to confirm that the number is indeed perfect. Challenge the com-
puting power of your computer by testing numbers much larger than 1000. Display the results.
6.25 (Prime Numbers) A positive integer is prime if it's divisible by only 1 and itself. For example,
2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. The number 1, by definition, is not prime.
a) Write a method that determines whether a number is prime.
b) Use this method in an application that determines and displays all the prime numbers
less than 10,000. How many numbers up to 10,000 do you have to test to ensure that
you've found all the primes?
c) Initially, you might think that n /2 is the upper limit for which you must test to see
whether a number n is prime, but you need only go as high as the square root of n . Re-
write the program, and run it both ways.
6.26 (Reversing Digits) Write a method that takes an integer value and returns the number with
its digits reversed. For example, given the number 7631, the method should return 1367. Incorpo-
rate the method into an application that reads a value from the user and displays the result.
6.27 (Greatest Common Divisor) The greatest common divisor ( GCD ) of two integers is the largest
integer that evenly divides each of the two numbers. Write a method gcd that returns the greatest
common divisor of two integers. [ Hint: You might want to use Euclid's algorithm. You can find
information about it at en.wikipedia.org/wiki/Euclidean_algorithm .] Incorporate the method
into an application that reads two values from the user and displays the result.
6.28 Write a method qualityPoints that inputs a student's average and returns 4 if it's 90-100,
3 if 80-89, 2 if 70-79, 1 if 60-69 and 0 if lower than 60. Incorporate the method into an application
that reads a value from the user and displays the result.
 
Search WWH ::




Custom Search