Java Reference
In-Depth Information
For example, the calls consecutive(1, 2, 3) , consecutive(3, 2, 4) , and consecutive(-10, -8, -9)
would return true . The calls consecutive(3, 5, 7) , consecutive(1, 2, 2) , and consecutive(7, 7, 9)
would return false .
14. Write a method named numUnique that takes three integers as parameters and returns the number of unique integers
among the three. For example, the call numUnique(18, 3, 4) should return 3 because the parameters have three
different values. By contrast, the call numUnique(6, 7, 6) should return 2 because there are only two unique
numbers among the three parameters: 6 and 7 .
15. Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the inte-
gers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your
method should return false if no such midpoint relationship exists. For example, the call hasMidpoint(7, 4,
10) should return true because 7 is halfway between 4 and 10. By contrast, the call hasMidpoint(9, 15, 8)
should return false because no integer is halfway between the other two. The integers could be passed in any order;
the midpoint could be the 1st, 2nd, or 3rd. You must check all cases. If your method is passed three of the same
value, return true .
16. Write a method named monthApart that accepts four integer parameters, m1 , d1 , m2 ,and d2 , representing two cal-
endar dates. Each date consists of a month (1 through 12) and a day (1 through the number of days in that month
[28-31]). Assume that all parameter values passed are valid. The method should return true if the dates are at least
a month apart and false otherwise. For example, the call of monthApart(4, 15, 5, 22) would return true
while the call of monthApart(9, 19, 10, 17) would return false . Assume that all the dates in this problem
occur during the same year. Note that the first date could come before or after the second date.
17. Write a method named digitRange that accepts an integer as a parameter and returns the range of values of its dig-
its. The range is defined as 1 more than the difference between the largest and smallest digit value. For example, the
call of digitRange(68437) would return 6 because the largest digit value is 8 and the smallest is 3, so 8
3
1
6. If the number contains only one digit, return 1 . You should solve this problem without using a String .
18. Write a method named swapDigitPairs that accepts an integer n as a parameter and returns a new integer whose value
is similar to n 's but with each pair of digits swapped in order. For example, the call of swapDigitPairs(482596)
would return 845269 . Notice that the 9 and 6 are swapped, as are the 2 and 5, and the 4 and 8. If the number contains an
odd number of digits, leave the leftmost digit in its original place. For example, the call of swapDigitPairs(1234567)
would return 1325476 . You should solve this problem without using a String .
Programming Projects
1. Write an interactive program that reads lines of input from the user and converts each line into “Pig Latin.” Pig Latin
is English with the initial consonant sound moved to the end of each word, followed by “ay.” Words that begin with
vowels simply have an “ay” appended. For example, the phrase
The deepest shade of mushroom blue
would have the following appearance in Pig Latin:
e-Thay eepest-day ade-shay of-ay ushroom-may ue-blay
Terminate the program when the user types a blank line.
Search WWH ::




Custom Search