cover picture cover picture


Homework 4

CMP 167: Programming Methods I
Lehman College, City University of New York
Fall 2016


This Homework Is Due By 11:59 PM on Monday October 31, 2016

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

  1. If num is odd, you should output: num is an odd number
  2. If num is even, you should output: num is an even number
  3. If num is divisible by 3, you should output: num is divisible by 3
  4. If num is divisible by 5, you should output: num is divisible by 5
  5. If num is divisible by 7, you should output: num is divisible by 7
  6. If num is divisible by 9, you should output: num is divisible by 9
  7. Increment 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:

A
93 <= grade <= 100
A-
90 <= grade < 93
B+
87 <= grade < 90
B
83 <= grade < 87
B-
80 <= grade < 83
C+
77 <= grade < 80
C
73 <= grade < 77
C-
70 <= grade < 73
D
60 <= grade < 70
F
0 <= grade < 60

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.