Question

VIII JAVA ASSIGNMENT Phone numbers and PIN codes can be easier to remember when you find...

VIII JAVA ASSIGNMENT

Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on a standard phone pad. For example, instead of remembering the combination

5282, you can just think of JAVA. Write a recursive method that given a number, yields all possible spellings (which may or may not be real words). Please use the file included as a starting point.

Possible testing values are:

5282

26678837

7764726

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Scanner;

public class PinWordEnumerator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter pin number: ");

String num = scanner.nextLine();

System.out.println();

System.out.printf("The keypad encodings for %s are:%n", num);

enumerateWords(num);

}

/**

* method to return a character array containing all possible keypad

* encodings for a digit in a standard phone

*/

static char[] getKeysForButton(int n) {

switch (n) {

case 0:

return new char[] { ' ' }; //returning only space

case 1:

return new char[] { '.' }; //returning only dot

case 2:

//button 2 contains A, B and C keys

return new char[] { 'A', 'B', 'C' };

case 3:

return new char[] { 'D', 'E', 'F' };

case 4:

return new char[] { 'G', 'H', 'I' };

case 5:

return new char[] { 'J', 'K', 'L' };

case 6:

return new char[] { 'M', 'N', 'O' };

case 7:

return new char[] { 'P', 'Q', 'R', 'S' };

case 8:

return new char[] { 'T', 'U', 'V' };

case 9:

return new char[] { 'W', 'X', 'Y', 'Z' };

}

return null;

}

/**

* method to enumerate words

*

* @param num

* - String containing pin numbers, assuming it has only numbers

*/

static void enumerateWords(String num) {

/**

* calling the recursive method to perform the enumeration

*/

if (num != null)

enumerateWords(num, "");

}

/**

* the main method which performs the recursion

*

* @param num

* - current number

* @param text

* - text containing converted spellings

*/

static void enumerateWords(String num, String text) {

if (num.length() == 0) {

// base case, displaying the text

System.out.println(text);

} else {

// finding the digit at 0th position

int digit = num.charAt(0) - '0';

// finding possible phone keys for this digit

char letters[] = getKeysForButton(digit);

if (letters != null) {

// looping through all possible keys

for (int i = 0; i < letters.length; i++) {

/**

* appending the current letter to the text and calling the

* recursive method also neglecting the first letter of

* current 'num' string

*/

enumerateWords(num.substring(1), text + letters[i]);

}

}

}

}

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
VIII JAVA ASSIGNMENT Phone numbers and PIN codes can be easier to remember when you find...
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
  • PLEASE DO IN JAVA You can use any source for the dictionary or just a few...

    PLEASE DO IN JAVA You can use any source for the dictionary or just a few words are fine! description The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, play is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot...

    JAVA Primitive Editor (Please help, I am stuck on this assignment which is worth a lot of points. Make sure that the program works because I had someone answer this incorrectly!) The primary goal of the assignment is to develop a Java based primitive editor. We all know what an editor of a text file is. Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    IN JAVA PLS DUE TODAY Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Part I— Just Bad Luck? Brrrring! Brrrring! Jane checked the caller ID on her phone. “Sam!...

    Part I— Just Bad Luck? Brrrring! Brrrring! Jane checked the caller ID on her phone. “Sam! Great!” she thought. It was always nice to get a call from her older brother. But a little twinge of worry tugged at her. It was just a couple of weeks ago that he had mentioned making an appointment with his doctor about some abdominal pain he had been having. “Hi Sam! It’s great to hear from you,” Jane answered. “Hi Jane. Well I...

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
Active Questions
ADVERTISEMENT