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

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
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.
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...
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
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
Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print...
Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print an error message. Also have to make sure 2nd input is an integer and not a string by default. The function in the program should take the second string and insert it into the first string in the position indicated by the integer input given. The program cannot use strlen() or strncpy() functions and it should check the integer input to make sure it...
Matlab program Create a function, when given a two-digit integer as input, prints the equivalent English...
Matlab program Create a function, when given a two-digit integer as input, prints the equivalent English phrase. For example, given 39, output thirty-nine. Limit the input numbers to be in the range 21 to 39.
(In JAVA)Write code to print the location of any alphabetic character in the 2-character string passCode.
Java Language Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is: Alphabetic at 1 import java.util.Scanner;   public class FindAlpha {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String passCode;              passCode = scnr.next();     ...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
JAVA Please: Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the...
JAVA Please: Lab9B: MicroDB. In Lab9A, each method returned an integer. In this part of the lab, all methods will have a void return type and take in an array of integers as a parameter. You’re going to write a program that creates a mini database of numbers that allows the user to reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT