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

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.
*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)
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of each word A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data as described below. Variables to hold the data read in using std::cin and a return statement. #include <iostream > int main(int argc, char *argv[]) { .... your code goes here }//main Example usage: >A01.exe Enter three 3-letter space separated words, then...
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 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 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
use java Write a program that, given two binary numbers represented as strings, prints their sum...
use java Write a program that, given two binary numbers represented as strings, prints their sum in binary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0 Input: Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces. Output: For each pair of binary numbers print to standard...
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.
Use Python to solve: show all work 1) Write a program that prints out a deck...
Use Python to solve: show all work 1) Write a program that prints out a deck of cards, in the format specified below. (That is, the following should be the output of your code). 2 of hearts 2 of diamonds 2 of spades 2 of clubs 3 of hearts 3 of diamonds 3 of spades 3 of clubs 4 of hearts 4 of diamonds 4 of spades 4 of clubs Continue with 5,6,7.. ending with A of hearts A of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT