While
Loop
1. Converting Hexadecimal Digits to Decimal
The hexadecimal number system has 16 digits, 0 - 9, A - F, representing 0 – 15 in the decimal number system. Write a program that prompts the user to enter a hexadecimal digit and display its decimal value.
Call this program HexDigitToDec
. The program only allows a single character input per line. The character
must be 0 - 9 or A - F or a - f; otherwise, the program should complain about the invalid input. The program
will stop repeat these if the user enters a “sentinel value” of “done”. The
following is an example run that further illustrates the requirements:
$ java HexDigitToDec
Enter a hexadecimal digit (0-9, A-F): DoNe
$ java HexDigitToDec
Enter a hexadecimal digit (0-9, A-F): 0
Hexadecimal digit 0 is 0 in decimal.
Enter a hexadecimal digit (0-9, A-F): 1
Expected 0 - 9 or A - F, but encountered 1
Enter a hexadecimal digit (0-9, A-F): A
Expected one character per line. But saw 6
Enter a hexadecimal digit (0-9, A-F): A
Hexadecimal digit A is 10 in decimal.
Enter a hexadecimal digit (0-9, A-F): B
Hexadecimal digit B is 11 in decimal.
Enter a hexadecimal digit (0-9, A-F): C
Hexadecimal digit C is 12 in decimal.
Enter a hexadecimal digit (0-9, A-F): D
Hexadecimal digit D is 13 in decimal.
Enter a hexadecimal digit (0-9, A-F): E
Hexadecimal digit E is 14 in decimal.
Enter a hexadecimal digit (0-9, A-F): F
Hexadecimal digit F is 15 in decimal.
Enter a hexadecimal digit (0-9, A-F): a
Hexadecimal digit A is 10 in decimal.
Enter a hexadecimal digit (0-9, A-F): b
Hexadecimal digit B is 11 in decimal.
Enter a hexadecimal digit (0-9, A-F): aa
Expected one character per line. But saw 2
Enter a hexadecimal digit (0-9, A-F): zzz
Expected one character per line. But saw 3
Enter a hexadecimal digit (0-9, A-F): done
Characters and Strings
Counting Letters
Write a program called CountLowers
that prompts the user to enter a line of text, and counts the number of lower
case letters in the text. The following are several test runs that show additional requirements, such as, output
format:
$ java CountLowers
Enter a line of text:
The number of lower case letters in the input is: 0.
$ java CountLowers
Enter a line of text:
123
The number of lower case letters in the input is: 0.
$ java CountLowers
Enter a line of text:
123abcDEFxyz hello world!
The number of lower case letters in the input is: 16.
Counting Words
Write a program called CountWord
that prompts the user to enter a line of text and a word, and counts the number
of times the word appears in the text. The following are several test runs that exhibit additional requirements
including what constitute an “appearance” and the output format:
$ java CountWord
Enter a line of text:
hello, world! hello, java!
Enter a word:
hello
Word <hello> appears 2 times in line <hello, world! hello, java!>.
$ java CountWord
Enter a line of text:
aa
Enter a word:
aa
Word <aa> appears 1 times in line <aa>.
$ java CountWord
Enter a line of text:
aaa
Enter a word:
aa
Word <aa> appears 2 times in line <aaa>.
$ java CountWord
Enter a line of text:
aaaa
Enter a word:
aa
Word <aa> appears 3 times in line <aaaa>.
$ java CountWord
Enter a line of text:
aaaaa
Enter a word:
aa
Word <aa> appears 4 times in line <aaaaa>.
$ java CountWord
Enter a line of text:
aaabbaaaabbaaaaa
Enter a word:
aa
Word <aa> appears 9 times in line <aaabbaaaabbaaaaa>.
Nested While
Loop
1. Printing Number Pyramid
There are several related exercises. For each, you should use a nested while
loop.
Printing Right Half of Simpler Number Pyramid
Write a program called SimpleNumberPyramidRightHalf
to print the following pattern:
1
2 1
3 2 1
4 3 2 1
The program should prompt the user to enter a number, the height of the pyramid. To make it simple, the height is invalid if it is greater than 9. Below are several test runs of the program:
$ java SimpleNumberPyramidRightHalf
Enter the height of number pyramid: 0
hui@ThinkpadE450 MINGW64 ~/work/course/CISC1115/lab/loop_char_string
$ java SimpleNumberPyramidRightHalf
Enter the height of number pyramid: 1
1
$ java SimpleNumberPyramidRightHalf
Enter the height of number pyramid: 3
1
2 1
3 2 1
$ java SimpleNumberPyramidRightHalf
Enter the height of number pyramid: 10
Height cannot be greater than 9, encounter 10.
Printing Left Half of Simpler Number Pyramid
Write a program called SimpleNumberPyramidLeftHalf
to print the following pattern:
1
1 2
1 2 3
1 2 3 4
Like before, the program should prompt the user to enter a number, the height of the pyramid. To make it simple, the height is invalid if it is greater than 9. Below are several runs of the program:
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 0
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 1
1
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 2
1
1 2
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 3
1
1 2
1 2 3
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 4
1
1 2
1 2 3
1 2 3 4
$ java SimpleNumberPyramidLeftHalf
Enter the height of number pyramid: 10
Height cannot be greater than 9, encounter 10.
Printing Simpler Number Pyramid
Write a program called SimpleNumberPyramid
to print the following pattern:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Like before, the program should prompt the user to enter a number, the height of the pyramid. To make it simple, the height is invalid if it is greater than 9. Several sample runs of the programs are shown below:
$ java SimpleNumberPyramid.java
Enter the height of number pyramid: 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
$ java SimpleNumberPyramid.java
Enter the height of number pyramid: 8
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
$ java SimpleNumberPyramid.java
Enter the height of number pyramid: 0
$ java SimpleNumberPyramid.java
1Enter the height of number pyramid:10
Height cannot be greater than 9, encounter 10.
Printing Power Number Pyramid
Now write a program called SimplePowerNumberPyramid
is to print out the following pattern:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
where the height of a pyramid can be 1, 2, 3, or 4, and anything else is invalid. Be are several test runs:
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 0
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 1
1
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 2
1
1 2 1
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 3
1
1 2 1
1 2 4 2 1
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 4
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
$ java SimplePowerNumberPyramid.java
Enter the height of number pyramid: 5
Height cannot be greater 4, encountered 5.