Textbook Section 4.14 Homework 4-1
Write a Java program that uses an int
variable num
which starts
at 1
.
Your program will have a loop that will continue as long as num
is less than
or equal to 30
. The loop will do the following
num
is odd, you should output: num is an odd number
num
is even, you should output: num is an even number
num
is divisible by 3, you should output: num is divisible by 3
num
is divisible by 5, you should output: num is divisible by 5
num
is divisible by 7, you should output: num is divisible by 7
num
is divisible by 9, you should output: num is divisible by 9
num
Please note that your class should be named FirstLoop.
Textbook Section 4.15 Homework 4-2
Write a program to read a list of exam grades given as int
's in the range of
0
to 100
. Your program will display the total number of grades
and the number of grades in each letter-grade category as follows:
Use a negative number as a sentinel value to indicate the end of the input. (The negative value is used only to end the loop, do not use it in your calculations.)
Each time you prompt the user to enter a grade you will print:
Enter a grade:
For example, if the input is:
98 95 87 86 83 92 85 78 74 72 81 71 69 63 50 43 -1
The output would be:
Total number of grades = 16 Number of A's = 2 Number of A-'s = 1 Number of B+'s = 1 Number of B's = 3 Number of B-'s = 1 Number of C+'s = 1 Number of C's = 1 Number of C-'s = 2 Number of D's = 2 Number of F's = 2
Please note that your class should be named Grades.
Textbook Section 4.16 Homework 4-3
Write a Java program that prompts the user for an int n
. You can assume that
1 ≤ n ≤ 9
. You program should use two imbeded for
loops that produce the following output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 . . . 1 2 3 4 5 . . . n
Your prompt to the user should be:
Please enter a number 1...9 :
Please note that your class should be named PatternOne.
Textbook Section 4.17 Homework 4-4
Write a Java program that prompts the user for an int n
. You can assume that
1 ≤ n ≤ 9
. You program should use two imbeded for
loops that produce the following output:
1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 . . . n . . . 5 4 3 2 1
Your prompt to the user should be:
Please enter a number 1...9 :
Please note that your class should be named PatternTwo.
Textbook Section 4.18 Homework 4-5
To make telephone numbers easier to remember, some companies user letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN.
In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example 225-5466 can be displayed as CALL HOME, which uses eight letters.
Write a Java program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. If the user enters more that seven letters (spaces do not count), then process only the first seven letters and ignore the rest. Your program should also output the - (hyphen) after the third digit.
Allow the user to use uppercase and lowercase letters, as well as spaces between the words.
Hint: You can read the entered telephone number as a
String
and then use the charAt
method
of the class String
to extract each character. For
example, if str
refers to your String
,
then the expression str.charAt(i)
returns the character
at the ith position. Recall that in a
String
, the position of the first character is 0.
If the user enters:
Get Loan
Your program will output:
438-5626
Or, if the user enters:
cAll HoMe
Your program will output:
225-5466
Your prompt to the user should be:
Please enter a telephone number using letters :
Please note that your class should be named TelephoneNumber.