Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program code: import java.util.*; public class IntegerName { /** Turns a number into its English name. @paran number a positive integer < 10,000 @return the

Program code:

import java.util.*; public class IntegerName { /** Turns a number into its English name. @paran number a positive integer < 10,000 @return the name of the number(e.g. 'two hundred seventy four') **/ public static String intName(int number) { int part = number; // The part that still needs to be converted String name = ""; // The name of the number // ********* added newly ***************** // if the part is greater than 1000 if (part >= 1000) { // then get digit at 1000's place and get equivalent digit name and add a string "thousand" after it name = digitName(part / 1000) + " thousand "; // remove digit at thousands place part = part % 1000; } if (part >= 100) { name = name + digitName(part / 100) + " hundred"; part = part % 100; } if (part >= 20) { name = name + " " + tensName(part); part = part % 10; } else if (part >= 10) { name = name + " " + tensName(part); part = 0; } if (part > 0) { name = name + " " + digitName(part); } return name; } /** Turns a digit into its English name @param digit an integer between 1 and 9 @return the name f the digit ("one" ... "nine") */ public static String digitName(int digit) { if (digit == 1) { return "one"; } if (digit == 2) { return "two"; } if (digit == 3) { return "three"; } if (digit == 4) { return "four"; } if (digit == 5) { return "five"; } if (digit == 6) { return "six"; } if (digit == 7) { return "seven"; } if (digit == 8) { return "eight"; } if (digit == 9) { return "nine"; } return ""; } /** Turns a number between 10 and 19 into its English name. @param number an integer between 10 and 19 @return the name of the given number ("ten" ... "nineteen") */ public static String teenName(int number) { if (number == 10) { return "ten"; } if (number == 11) { return "eleven"; } if (number == 12) { return "twelve"; } if (number == 13) { return "thirteen"; } if (number == 14) { return "fourteen"; } if (number == 15) { return "fifteen"; } if (number == 16) { return "sixteen"; } if (number == 17) { return "seventeen"; } if (number == 18) { return "eighteen"; } if (number == 19) { return "nineteen"; } return ""; } /** Gives the name of the tens part of a number between 20 and 99. @param number an integer between 20 and 99 @return the name of the tens part of the number ("twenty" ... "ninety") */ public static String tensName(int number) { if (number >= 90) { return "ninety"; } if (number >= 80) { return "eighty"; } if (number >= 70) { return "seventy"; } if (number >= 60) { return "sixty"; } if (number >= 50) { return "fifty"; } if (number >= 40) { return "forty"; } if (number >= 30) { return "thirty"; } if (number >= 20) { return "twenty"; } return ""; } public static void main(String args[]) { // create object for scanner class Scanner scan = new Scanner(System.in); // ask user to enter a number System.out.print("Enter a number: "); // read number from keyboard int number = scan.nextInt(); // call intName() method and print the result System.out.println(intName(number)); } }

the above provided code is working fine for input 9999, but when the input is changed to 55555(5 -digit or higher number the results are not satisfatory. modify the intName method so that it works correctly for values < 1,000,000,000.

Sample output:

99999: ninety-nine thousand nine hundred ninety-nine

run2:

999999: nine hundred ninety-nine thousand nine hundred ninety-nine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What attracts you about this role?

Answered: 1 week ago

Question

How many states in India?

Answered: 1 week ago

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago