Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recursive Text File Output for Number Words Description: Your solution to Lab21(

ID: 3691323 • Letter: R

Question

Recursive Text File Output for Number Words

Description:

Your solution to Lab21(https://cse.sc.edu/~pade/CSCE145/assign/Lab21.html) most likely involved 7 nested loops. Many programmers, as a rule of thumb, limit their programs to three levels of indentation. Your goal is to refactor your code so that it does not use 7 levels of nested loops (however, you are not required to limit yourself to 3).

Objective:

Write a class Numbers that asks a user to enter an arbitrary-digit number and then writes to the file 'numbers.txt' every possible word combination corresponding to that number. There will be 3^n such combinations, where n n is the length (in digits) of the given number. Treat the digits 0 & 1 as spaces, and the rest as shown:

Digit | Letters ------+-------- 1 | [space] 2 | A B C 3 | D E F 4 | G H I 5 | J K L 6 | M N O 7 | P R S 8 | T U V 9 | W X Y 0 | [space]

HINT: Try a method that returns an array of Strings of possible combinations. Then, recurse over this solution for input of larger lengths.

Note

Notice that by definition, the program should calculate 3^n outputs where n is the length of the input. This means - similar to the fibonacci sequence - that the program should run in time O ( 3^n ) . You should experiment with different input sizes to see how the running time for your program scales. WARNING: If you are printing the output to a file, that means the space that the program will use on your harddrive also scales as O ( 3^n ) . If you are not careful, you could easily use up your entire harddrive. Therefore, you should disable file output if you are experimenting with large input numbers. Make sure to reenable this before you submit your work.

To see this in original format go to https://cse.sc.edu/~pade/csce145/assign/Lab23.html

Explanation / Answer

Answer for Question:

This code may help you or solving the given problem statement using java files concept.

Step 1: Created the below java code for telephoneNumberWordGenerator using java files concept.

Step 2: Source Code is below:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import java.io.ObjectOutputStream;
import java.util.NoSuchElementException;

public class TelephoneNumbe
{
public static void main(String[] args)
   {
       PrintStream printStream;
       char[] word = new char [7];
       char numberLetters[][] = {
{'0','0','0'},{'1','1','1'},{'A','B','C'},
{'D','E','F'},{'G','H','I'},{'J','K','L'},
{'M','N','O'},{'P','R','S'},
{'T','U','V'},{'W','X','Y'}};
          
       Scanner input = new Scanner (System.in);
       String phoneNumber;
System.out.println("Enter a seven-digit telephone number: ");
phoneNumber = input.next();
       ObjectOutputStream output;
output = new ObjectOutputStream( new FileOutputStream ( "Phonenumber.txt") );
       char[] chars = phoneNumber.toCharArray ();
       int [] digit = new int [chars.length];
       for (int i = 0; i < chars.length; i++)
       {
           digit[i] = Integer.parseInt(String.valueOf(chars[i]));
       }

       printStream = new PrintStream(output);
       printStream.println(" ");


       for ( int level0 = 0; level0 < 3; level0 ++ )
       {
           word[0] = numberLetters[digit[0]][level0];

           for ( int level1 = 0; level1 < 3; level1 ++ )
           {
               word[1] = numberLetters[digit[1]][level1];

               for ( int level2 = 0; level2 < 3; level2 ++ )
               {
                   word[2] = numberLetters[digit[2]][level2];

                   for ( int level3 = 0; level3 < 3; level3 ++ )
                   {
                       word[3] = numberLetters[digit[3]][level3];

                       for ( int level4 = 0; level4 < 3; level4 ++ )
                       {
                           word[4] = numberLetters[digit[4]][level4];

                           for ( int level5 = 0; level5 < 3; level5 ++ )
                           {
                               word[5] = numberLetters[digit[5]][level5];

                               for ( int level6 = 0; level6 < 3; level6 ++ )
                               {
                                   word[6] = numberLetters[digit[6]][level6];
                                   printStream.print(word);
                               }
                           }
                       }
                   }
               }
           }
       }
           output.close();
}   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote