Question

In: Computer Science

in java please: Given any integer, print an English phrase that describes the integer (e.g. “One...

in java please:

Given any integer, print an English phrase that describes the integer (e.g. “One Thousand, Two Hundred Thirty Four”). An ArrayList must be used in your program.

Solutions

Expert Solution

class PrintEng
{
static void convertNumberToEnglish(char[] num)
{
   int len = num.length;
   if (len == 0)
   {
       System.out.println("Sorry string is Empty");
       return;
   }
   if (len > 4)
   {
       System.out.println("String length is greater than 4, it's not supported");
       return;
   }

   String[] singleDigitNumber = new String[]{ "zero", "one",
                                       "two", "three", "four",
                                       "five", "six", "seven",
                                           "eight", "nine"};

   String[] twoDigitsNumber = new String[]{"", "ten", "eleven", "twelve",
                                       "thirteen", "fourteen",
                                       "fifteen", "sixteen", "seventeen",
                                       "eighteen", "nineteen"};

   String[] tensMultipleNumber = new String[]{"", "", "twenty", "thirty", "forty",
                                           "fifty","sixty", "seventy",
                                           "eighty", "ninety"};

   String[] tensPwerNumber = new String[] {"hundred", "thousand"};

  
   System.out.print(String.valueOf(num)+": ");
  
   if (len == 1)
   {
       System.out.println(singleDigitNumber[num[0] - '0']);
       return;
   }

   int x = 0;
   while (x < num.length)
   {
       if (len >= 3)
       {
           if (num[x]-'0' != 0)
           {
               System.out.print(singleDigitNumber[num[x] - '0']+" ");
               System.out.print(tensPwerNumber[len - 3]+" ");
           }
           --len;
       }

       /* Code path for last 2 digits */
       else
       {
           if (num[x] - '0' == 1)
           {
               int sum = num[x] - '0' +
                   num[x] - '0';
               System.out.println(twoDigitsNumber[sum]);
               return;
           }
          
           else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0)
           {
               System.out.println("twenty");
               return;
           }
           else
           {
               int i = (num[x] - '0');
               if(i > 0)
               System.out.print(tensMultipleNumber[i]+" ");
               else
               System.out.print("");
               ++x;
               if (num[x] - '0' != 0)
                   System.out.println(singleDigitNumber[num[x] - '0']);
           }
       }
       ++x;
   }
}

// Executable Code
   public static void main(String[] args)
   {
   convertNumberToEnglish("9423".toCharArray());
   convertNumberToEnglish("5123".toCharArray());
   convertNumberToEnglish("829".toCharArray());
   convertNumberToEnglish("889".toCharArray());
   }
}


Related Solutions

JAVA Input a phrase from the keyboard, if the phrase contains "red" or "RED" print out...
JAVA Input a phrase from the keyboard, if the phrase contains "red" or "RED" print out "red" . if the phrase contains "blue" or "BLUE" output "blue" In all other cases print "No Color" For example: If the input was "Violets are BLUE" your output should be "blue" If the input was "Singing the blues" output "blue" If the input was "I have a pure bred puppy" your output should be "red" If the input was "Today is Monday" output...
Envision an algorithm that when given any positive integer n, it will print out the sum...
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. E.g. given 4 the algorithm would print 30 (because 1 + 4 + 9 + 16 = 30) You can use multiplication denoted as * in your solution and you do not have to define it (e.g. 2*2=4) Write pseudocode for this algorithm using iteration (looping). Create a flow chart Implement solution from flowchart in Python at...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
please match nitrogen from the phrase that best describes it: _Most abundant form of nitrogen _disrupts...
please match nitrogen from the phrase that best describes it: _Most abundant form of nitrogen _disrupts 03-o2 equilibrium and is a greenhouse gas _most oxidized form of nitrogen 1. NO 2. N2 3. N2O 4.NO3 5.HNO2
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
Please give a movie review of any movie. The movie can be any ENGLISH speaking movie...
Please give a movie review of any movie. The movie can be any ENGLISH speaking movie of your choice. Examples would be Star Wars (newer series), Best Exotic Marigold Hotel, The 100 Foot Journey etc.. Any film that you feel will suit the assignment. Please make sure the movie is NOT a foreign film, and that it is no older than 10-15 years old. at least 3-4 pages
Write a C++ function to print any given std::array of a given type to the standard...
Write a C++ function to print any given std::array of a given type to the standard output in the form of {element 0, element 1, element 2, ...}. For example given the double array, d_array = {2.1, 3.4, 5.6, 2.9}, you'd print {2.1, 3.4, 5.6, 2.9} to the output upon executing std::cout << d_array; line of code. Your function mus overload << operator.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT