cover picture cover picture


Homework 6

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


This Homework Is Due By 11:59 PM on Sunday April 10, 2016

In your textbook, this homework problem is in section 4.14 Homework 6-1.

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.

In your textbook, this homework problem is in section 4.15 Homework 6-2.

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 3 4 5 . . . n 
          .
          .
          .
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
  

Your prompt to the user should be:

    Please enter a number 1...9 :
  

Please note that your class should be named PatternTwo.

In your textbook, this homework problem is in section 4.16 Homework 6-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
                  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 PatternThree.

In your textbook, this homework problem is in section 4.17 Homework 6-4.

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.