Question

In: Computer Science

You are supposed to write Java program for the following questions. For every question, do provide...

You are supposed to write Java program for the following questions. For every question, do provide at least THREE (3) different test cases. Question 1 Modify question 3 of Assignment 1 to accept a telephone number with any number of letters. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.

Solutions

Expert Solution

Copyable Code:

import java.util.Scanner;
public class assignProgram
{
public static void main(String args[])
{
       String number;
       char c;
       Scanner in = new Scanner(System.in);
       for(;;)
       {
           System.out.print("\nEnter the phone number that is in letters or for quit enter 0: ");
           number = in.nextLine();
       if(number.equals("0"))
           {
               break;
           }
           for(int i = 0; i < 8; i++)
           {
               c = number.charAt(i);
               if(i == 3)
                   System.out.print("-");
               if(c >= 'A' && c <= 'Z' || c >= 'a' && c <='z')
               {
                       switch(c)
                       {              

                           case 'A':
                           case 'a':
                           case 'B':
                           case 'b':
                           case 'C':
                           case 'c':
                           System.out.print("2");
                           break;
                           case 'D':
                           case 'd':
                           case 'E':
                           case 'e':
                           case 'F':
                           case 'f':
                           System.out.print("3");
                           break;
                           case 'G':
                           case 'g':
                           case 'H':
                           case 'h':
                           case 'I':
                           case 'i':
                               System.out.print("4");
                               break;
                           case 'J':
                           case 'j':
                           case 'K':
                           case 'k':
                           case 'L':
                           case 'l':
                               System.out.print("5");
                               break;
                           case 'M':
                           case 'm':
                           case 'N':
                           case 'n':
                           case 'O':
                           case 'o':
                               System.out.print("6");
                           break;
                       case 'P':
                           case 'p':
                           case 'Q':
                           case 'q':
                           case 'R':
                           case 'r':
                           case 'S':
                           case 's':
                               System.out.print("7");
                           break;
                       case 'T':
                           case 't':
                           case 'U':
                           case 'u':
                           case 'V':
                           case 'v':
                               System.out.print("8");
                           break;
                       case 'W':
                           case 'w':
                       case 'X':
                       case 'x':
                           case 'Y':
                           case 'y':
                           case 'Z':
                           case 'z':
                           System.out.print("9");
                           break;   
                   }
                   }
               }
           }
       }
}


Related Solutions

Java Code Question: The program is supposed to read a file and then do a little...
Java Code Question: The program is supposed to read a file and then do a little formatting and produce a new txt file. I have that functionality down. My problem is that I also need to get my program to correctly identify if a file is empty, but so far I've been unable to. Here is my program in full: import java.io.*; import java.util.Scanner; public class H1_43 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter...
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT