Java Reference
In-Depth Information
/** Return this number if it is a single digit; otherwise,
* return the sum of the two digits */
public static int
getDigit(
int
number)
/** Return sum of odd-place digits in number */
public static int
sumOfOddPlace(String cardNumber)
**9.24
(
Implement the
StringBuilder
class
) The
StringBuilder
class is provided
in the Java library. Provide your own implementation for the following methods
(name the new class
MyStringBuilder2
):
public
MyStringBuilder2();
public
MyStringBuilder2(
char
[] chars);
public
MyStringBuilder2(String s);
public
MyStringBuilder2 insert(
int
offset, MyStringBuilder2 s);
public
MyStringBuilder2 reverse();
public
MyStringBuilder2 substring(
int
begin);
public
MyStringBuilder2 toUpperCase();
***9.25
(
Game: hangman
) Write a hangman game that randomly generates a word and
prompts the user to guess one letter at a time, as shown in the sample run. Each
letter in the word is displayed as an asterisk. When the user makes a correct
guess, the actual letter is then displayed. When the user finishes a word, display
the number of misses and ask the user whether to continue to play with another
word. Declare an array to store words, as follows:
// Add any words you wish in this array
String[] words = {
"write"
,
"that"
, ...};
(Guess) Enter a letter in word ******* > p
(Guess) Enter a letter in word p****** > r
(Guess) Enter a letter in word pr**r** > p
p is already in the word
(Guess) Enter a letter in word pr**r** > o
(Guess) Enter a letter in word pro*r** > g
(Guess) Enter a letter in word progr** > n
n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr*m > a
The word is program. You missed 1 time
Do you want to guess another word? Enter y or n>
**9.26
(
Check ISBN-10
) Use string operations to simplify Programming Exercise 3.9.
Enter the first 9 digits of an ISBN number as a string.
VideoNote
*9.27
(
Bioinformatics: find genes
) Biologists use a sequence of the letters A, C, T, and G
to model a
genome
. A
gene
is a substring of a genome that starts after a triplet
ATG and ends before a triplet TAG, TAA, or TGA. Furthermore, the length of a
gene string is a multiple of 3, and the gene does not contain any of the triplets
ATG, TAG, TAA, or TGA. Write a program that prompts the user to enter a
genome and displays all genes in the genome. If no gene is found in the input
sequence, display “no gene is found”. Here are the sample runs:
Check ISBN-10
Enter a genome string:
TTT
GGGCGT
TTATGTTTTAAGGATGGGGCGTTAGTT