Question

IN JAVA Create a class called Telephone that accepts a phone number in the constructor. For...

IN JAVA

Create a class called Telephone that accepts a phone number in the constructor. For the purposes of this assignment, phone numbers may be any length. Make a method called getPossibilities that returns all possible phonewords for that phone number.

A phoneword is what you get when a phone number is converted in to letters. For example, the phone number 922-6966 could be re-written as ZAA-MZNO. Look at your phone to see where those letters came from.

DO NOT USE LOOPS.

////////The provided file cannot be edited\\\\\\\\

Problem4.java

import java.util.Scanner;

public class Problem4
{
  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a phone number");
    String phone = keyboard.nextLine();

    Telephone t = new Telephone(phone);
    for (String result : t.getPossibilities()) {
      System.out.println(result);
    }
  }
}

///////////Required Output\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Standard Input                 Files in the same directory
2
  • Problem4.java

Required Output

Enter a phone number\n
A\n
B\n
C\n
Standard Input                 Files in the same directory
3
  • Problem4.java

Required Output

Enter a phone number\n
D\n
E\n
F\n
Standard Input                 Files in the same directory
7
  • Problem4.java

Required Output

Enter a phone number\n
P\n
Q\n
R\n
S\n
Standard Input                 Files in the same directory
1
  • Problem4.java

Required Output

Enter a phone number\n
1\n
Standard Input                 Files in the same directory
234
  • Problem4.java

Required Output

Enter a phone number\n
ADG\n
ADH\n
ADI\n
AEG\n
AEH\n
AEI\n
AFG\n
AFH\n
AFI\n
BDG\n
BDH\n
BDI\n
BEG\n
BEH\n
BEI\n
BFG\n
BFH\n
BFI\n
CDG\n
CDH\n
CDI\n
CEG\n
CEH\n
CEI\n
CFG\n
CFH\n
CFI\n

Your Program's Output

Enter a phone number\n
ADG\n
BEH\n
CFI\n
ADG\n
BEH\n
CFI\n
ADG\n
BEH\n
CFI\n
ADG\n
BEH\n
CFI\n
 (Your output is too short.)

Test Case 8 - Failed

Standard Input                 Files in the same directory
789
  • Problem4.java

Required Output

Enter a phone number\n
PTW\n
PTX\n
PTY\n
PTZ\n
PUW\n
PUX\n
PUY\n
PUZ\n
PVW\n
PVX\n
PVY\n
PVZ\n
QTW\n
QTX\n
QTY\n
QTZ\n
QUW\n
QUX\n
QUY\n
QUZ\n
QVW\n
QVX\n
QVY\n
QVZ\n
RTW\n
RTX\n
RTY\n
RTZ\n
RUW\n
RUX\n
RUY\n
RUZ\n
RVW\n
RVX\n
RVY\n
RVZ\n
STW\n
STX\n
STY\n
STZ\n
SUW\n
SUX\n
SUY\n
SUZ\n
SVW\n
SVX\n
SVY\n
SVZ\n

Your Program's Output

Enter a phone number\n
PSZ\n
QRZ\n
RUZ\n
PSZ\n
QRZ\n
RUZ\n
PSZ\n
QRZ\n
RUZ\n
PSZ\n
QRZ\n
RUZ\n
 (Your output is too short.)

Test Case 9 - Failed

Standard Input                 Files in the same directory
2740
  • Problem4.java

Required Output

Enter a phone number\n
APG0\n
APH0\n
API0\n
AQG0\n
AQH0\n
AQI0\n
ARG0\n
ARH0\n
ARI0\n
ASG0\n
ASH0\n
ASI0\n
BPG0\n
BPH0\n
BPI0\n
BQG0\n
BQH0\n
BQI0\n
BRG0\n
BRH0\n
BRI0\n
BSG0\n
BSH0\n
BSI0\n
CPG0\n
CPH0\n
CPI0\n
CQG0\n
CQH0\n
CQI0\n
CRG0\n
CRH0\n
CRI0\n
CSG0\n
CSH0\n
CSI0\n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is as follows:

Telephone.java

************************************************************************************************************************************

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class Telephone {
   private String phoneNumber;

   /**
   * @param phoneNumber
   */
   Telephone(String phoneNumber) {
       this.phoneNumber = phoneNumber;
   }

   // method declared private so as to use only in this class
   /**
   * @param phoneNumber
   * @param map
   * @return
   */
   private static List<String> permute(String phoneNumber, HashMap<Character, List<Character>> map) {
       if (phoneNumber.length() == 1) {
           return map.get(phoneNumber.charAt(0)).stream().map(ch -> ch.toString()).collect(Collectors.toList()); //if there is only one number
       }
       List<String> arr = permute(phoneNumber.substring(1, phoneNumber.length()), map); //recursive call to the permute method

       List<Character> curr = map.get(phoneNumber.charAt(0)); //current characters
       ArrayList<String> permutations = new ArrayList<>();

       arr.stream().forEach(str -> {
           curr.stream().forEach(c -> {
               permutations.add(c + str); //add in permutations
           });
       });

       Collections.sort(permutations); //sort the list
       return permutations; //return
   }

  
   /**
   * @return
   */
   public List<String> getPossibilities() {
       if (phoneNumber.length() == 0) {
           return new ArrayList<String>();
       }
       HashMap<Character, List<Character>> map = new HashMap<Character, List<Character>>(); //created a new hashmap
       //put all the characters allowed
       map.put('0', new ArrayList<Character>(Arrays.asList('0')));
       map.put('1', new ArrayList<Character>(Arrays.asList('1')));
       map.put('2', new ArrayList<Character>(Arrays.asList('A', 'B', 'C')));
       map.put('3', new ArrayList<Character>(Arrays.asList('D', 'E', 'F')));
       map.put('4', new ArrayList<Character>(Arrays.asList('G', 'H', 'I')));
       map.put('5', new ArrayList<Character>(Arrays.asList('J', 'K', 'L')));
       map.put('6', new ArrayList<Character>(Arrays.asList('M', 'N', 'O')));
       map.put('7', new ArrayList<Character>(Arrays.asList('P', 'Q', 'R', 'S')));
       map.put('8', new ArrayList<Character>(Arrays.asList('T', 'U', 'V')));
       map.put('9', new ArrayList<Character>(Arrays.asList('W', 'X', 'Y', 'Z')));
       return permute(phoneNumber, map); //call the method permute and return
   }
}

************************************************************************************************************************************

Problem4.java

************************************************************************************************************************************

import java.util.Scanner;

public class Problem4
{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a phone number");
String phone = keyboard.nextLine();

Telephone t = new Telephone(phone);
for (String result : t.getPossibilities()) {
System.out.println(result);
}
}
}

************************************************************************************************************************************

Screenshot of the code:

Output:

Add a comment
Know the answer?
Add Answer to:
IN JAVA Create a class called Telephone that accepts a phone number in the constructor. For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java In a class called DomainParser, write a method getTopLevelDomain that accepts a URL in...

    in java In a class called DomainParser, write a method getTopLevelDomain that accepts a URL in String format then returns the top level domain. Files in the same directory: ComDemo.java Standard Input: google.com Required Output Enter a String\n Top level domain: com\n Standard Input google.co.in Required Output Enter a String\n Top level domain: in\n

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called...

    Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...

  • in java, Ask the user for a string. Display the number of whitespace characters in that...

    in java, Ask the user for a string. Display the number of whitespace characters in that string. Standard Input: this is a test Required Output: Enter a String\n Number of whitespace characters: 3\n

  • Java Help Please: I'm doing something wrong with my while loop. I want my code to...

    Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this...

  • In Java Programming chapter 6 Make a class that represents a file. This class will have...

    In Java Programming chapter 6 Make a class that represents a file. This class will have the ability to calculate the number of lines in that file and the ability to search through the file. UML diagram: -filename:String +FileStats(String fname) +getNumMatchingWords(String wordtoFind) : int +getNumLines() : int The getNumMatchingWords method will take a bit of text and determine how many lines contain that text. Make the comparison not care about case. View required output Test Case 1 Files in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT