Question

In: Computer Science

Use if statements to write a Java program that inputs a single letter and prints out...

Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY

No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not used on a telephone.

The program may operate like this (sample output): Enter a single letter and you will get the corresponding digit on the telephone: R The digit 7 corresponds to the letter R on the telephone Another sample: Enter a single letter and you will get the corresponding digit on the telephone: Z There is no digit on the telephone that corresponds to Z NOTE: Your program should accept only uppercase letters (error message for special characters or lowercase letters) 2. Rewrite the program - this time use a switch statement.

Solutions

Expert Solution

// Using If else statement

import java.util.Scanner;
import java.lang.*;
class MyException extends Exception
{ }
public class Main
{
   public static void main(String[] args)
   {
   Scanner sc = new Scanner(System.in);  
   System.out.println("Enter a single letter and you will get the corresponding digit on the telephone :");
char c = sc.next().charAt(0); // taking character input from user
try
{
if (!Character.isUpperCase(c))
throw new MyException(); // If char is not in uppercase throw exception
else
{ // Printing digit corresponds to letter
if(c == 'A' || c == 'B' || c == 'C')
System.out.println("The digit 2 corresponds to the letter " + c + " on the telephone");
else if(c == 'D' || c == 'E' || c == 'F')
System.out.println("The digit 3 corresponds to the letter " + c + " on the telephone");
else if(c == 'G' || c == 'H' || c == 'I')
System.out.println("The digit 4 corresponds to the letter " + c + " on the telephone");
else if(c == 'J' || c == 'K' || c == 'L')
System.out.println("The digit 5 corresponds to the letter " + c + " on the telephone");
else if(c == 'M' || c == 'N' || c == 'O')
System.out.println("The digit 6 corresponds to the letter " + c + " on the telephone");
else if(c == 'P' || c == 'R' || c == 'S')
System.out.println("The digit 7 corresponds to the letter " + c + " on the telephone");
else if(c == 'T' || c == 'U' || c == 'V')
System.out.println("The digit 8 corresponds to the letter " + c + " on the telephone");
else if(c == 'W' || c == 'X' || c == 'Y')
System.out.println("The digit 9 corresponds to the letter " + c + " on the telephone");
else if(c == 'Q' || c == 'Z')
System.out.println("There is no digit on the telephone that corresponds to " + c);
}
}
catch(MyException e)
{
System.out.println("Invalid input " + c);
}
   }
}

// Switch statement

import java.util.Scanner;
import java.lang.*;
class MyException extends Exception
{ }
public class Main
{
   public static void main(String[] args)
   {
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter a single letter and you will get the corresponding digit on the telephone :");
char c = sc.next().charAt(0);
int n;
try
{
if (!Character.isUpperCase(c))
throw new MyException();
else
{
  
switch (c)
{
case 'A' :
n = 2;
break;
case 'B' :
n = 2;
break;
case 'C' :
n = 2;
break;
case 'D' :
n = 3;
break;
case 'E' :
n = 3;
break;
case 'F' :
n = 3;
break;
case 'G' :
n = 4;
break;
case 'H' :
n = 4;
break;
case 'I' :
n = 4;
break;
case 'J' :
n = 5;
break;
case 'K' :
n = 5;
break;
case 'L' :
n = 5;
break;
case 'M' :
n = 6;
break;
case 'N' :
n = 6;
break;
case 'O' :
n = 6;
break;
case 'P' :
n = 7;
break;
case 'R' :
n = 7;
break;
case 'S' :
n = 7;
break;
case 'T' :
n = 8;
break;
case 'U' :
n = 8;
break;
case 'V' :
n = 8;
break;
case 'W' :
n = 9;
break;
case 'X' :
n = 9;
break;
case 'Y' :
n = 9;
break;
default:
n = 0;
System.out.println("There is no digit on the telephone that corresponds to "+ c);
break;
}
if (n != 0)
System.out.println("The digit "+ n +" corresponds to the letter "+ c +" on the telephone");
}
}
catch(MyException e)
{
System.out.println("Invalid input " + c);
}
   }
}


Related Solutions

*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT