\Java
Telephone.java
//********************************************************************
//
// Telephone.java
// Program title: This is the title
//
//********************************************************************
//
// Program Description and Design Overview:
// Prompt user to enter a single letter from A through Z (upper case only)
// Input a single letter (type char)
// Print out the corresponding digit (an int) of the buttons on a phone pad with the message:
// "The digit N corresponds to the letter L on the telephone"
// (where L is the letter typed by the program user and N is the corresponding digit computed by the program)
// The digit-letter correspondence rules are:
// 2 = ABC, 3 = DEF, 4 = GHI, 5 = JKL, 6 = MNO, 7 = PRS, 8 = TUV, 9 = WXY
// Use a multi-way if -else statement for this solution. Do not use a switch statement.
// Hint: You will need to use the logical OR operator (see page 123, 124 of your textbook)
// Since only certain upper case letters correspond to a phone pad digit, any other letter or character will not have a corresponding digit. Thus, if any other character is entered, the program should print,
//
// "There is no digit on the telephone that corresponds to L"
// (where L is the letter entered)
// Input Requirements: char letter- the capitalized letter
// answer- answer to question "Do you want to enter another character?"
//
// Output Requirements:
// Prompts for the conditions under which the user must follow in order to input the letter.
// The number corresponding to the letter typed in.
//
// Program Preconditions:The user must input capitalized letters to get the
// result they are looking for.
//
//
//********************************************************************
import cs1.Keyboard;
public class Telephone
{
char letter; //the capitalized letter to be changed into a number
//-----------------------------------------------------------------
// Prints the number corresponding to the "capital" letters on a telephone pad.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String answer = "yes"; //initialized condition to start the program
while (answer.equals("yes"))
{
System.out.print ("Please type a letter A through P or R through Y: ");
char letter = Keyboard.readChar(); //inputs user response to variable letter
// inner loop of the nested loop
while (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D' && letter != 'E'
&& letter != 'F' && letter != 'G' && letter != 'H' && letter != 'I' && letter
!= 'J' && letter != 'K' && letter != 'L' && letter != 'M' && letter != 'N' &&
letter != 'O' && letter != 'P' && letter != 'R' && letter != 'S' && letter != 'T'
&& letter != 'U' && letter != 'V' && letter != 'W' && letter != 'X' &&
letter != 'Y')// executes loop if the letter is invalid
{
System.out.println ("Sorry, " + letter + " is not valid.");
System.out.print ("Please type a letter A through P or R through Y: ");
letter = Keyboard.readChar(); //inputs user response into variable letter
}
if ( letter == 'A' || letter == 'B' || letter == 'C' )
{System.out.println ("The digit 2 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'D' || letter == 'E' || letter == 'F' )
{System.out.println ("The digit 3 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'G' || letter == 'H' || letter == 'I' )
{System.out.println ("The digit 4 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'J' || letter == 'K' || letter == 'L' )
{System.out.println ("The digit 5 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'M' || letter == 'N' || letter == 'O' )
{System.out.println ("The digit 6 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'P' || letter == 'R' || letter == 'S' )
{System.out.println ("The digit 7 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'T' || letter == 'U' || letter == 'V' )
{System.out.println ("The digit 8 corresponds to the letter " + letter + " on the telephone pad");}
else if ( letter == 'W' || letter == 'X' || letter == 'Y' )
{System.out.println ("The digit 9 corresponds to the letter " + letter + " on the telephone pad");}
else System.out.println ("Sorry, " + letter + " is not valid.");
System.out.println ("Do you want to enter another character? "); //prompts user if they want to enter another character
answer = Keyboard.readString();//inputs user response to string variable answer
}
} //end main
} // end class Telephone