Java Reference
In-Depth Information
For example, count("Welcome", 'e') returns 2 . Write a test program that
prompts the user to enter a string followed by a character and displays the number
of occurrences of the character in the string.
Sections 6.10-6.12
**6.24
( Display current date and time ) Listing 2.7, ShowCurrentTime.java, displays the
current time. Improve this example to display the current date and time. The cal-
endar example in Listing 6.12, PrintCalendar.java, should give you some ideas on
how to find the year, month, and day.
**6.25
( Convert milliseconds to hours, minutes, and seconds ) Write a method that con-
verts milliseconds to hours, minutes, and seconds using the following header:
public static String convertMillis( long millis)
The method returns a string as hours : minutes : seconds . For example,
convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns
a string 0:1:40 , and convertMillis(555550000) returns a string 154:19:10 .
Comprehensive
**6.26
( Palindromic prime ) A palindromic prime is a prime number and also palindro-
mic. For example, 131 is a prime and also a palindromic prime, as are 313 and
757. Write a program that displays the first 100 palindromic prime numbers. Dis-
play 10 numbers per line, separated by exactly one space, as follows:
2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929
...
**6.27
( Emirp ) An emirp (prime spelled backward) is a nonpalindromic prime number
whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17
and 71 are emirps. Write a program that displays the first 100 emirps. Display 10
numbers per line, separated by exactly one space, as follows:
13 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
...
**6.28
( Mersenne prime ) A prime number is called a Mersenne prime if it can be written
in the form 2 p
1 for some positive integer p . Write a program that finds all
Mersenne primes with p
-
31
and displays the output as follows:
p
2^p -1
2
3
3
7
5
31
...
**6.29
( Twin primes ) Twin primes are a pair of prime numbers that differ by 2. For exam-
ple, 3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are twin primes.
Write a program to find all twin primes less than 1,000. Display the output as follows:
(3, 5)
(5, 7)
...
 
 
Search WWH ::




Custom Search