Question

I need java code...

Background Suppose youre working on the next great cligital assistant to compete with the likes of Siri, Cortana Alexa, and

w am (pt) Problem to W WE Topic LTETI I Latv A phase wi th it Hesthetic (3 pts) Problem 5 Pinat will be tante rea d girls. Fo

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

If you have any problem with the code feel free to comment. The second image is unreadable so I have created the program according to my logic.

Program

import java.util.Scanner;

class NumberToWords {
   private static final String[] digitNames = { "", " thousand", " million", " billion", " trillion", " quadrillion",
           " quintillion" };

   private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty",
           " seventy", " eighty", " ninety" };

   private static final String[] oneToNineteenNames = { "", " one", " two", " three", " four", " five", " six",
           " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen",
           " seventeen", " eighteen", " nineteen" };

   private static String convertLessThanThousand(int number) {
       String word;

       if (number % 100 < 20) {// checking if the number is below 20
           word = oneToNineteenNames[number % 100];
           number /= 100;
       }

       else {// when its not below 20
           word = oneToNineteenNames[number % 10];
           number /= 10;

           word = tensNames[number % 10] + word;
           number /= 10;
       }
      
       if (number == 0)//when number is below hundred
           return word;
      
       return oneToNineteenNames[number] + " hundred" + word;//when number is above hundred
   }

   public static String convert(int number) {

       if (number == 0) {//when number is zero
           return "zero";
       }

       String word = "";
       int place = 0;
      
       //looping through the number and finding its word form
       do {
           int n = number % 1000;
           if (n != 0) {
               String s = convertLessThanThousand(n);
               word = s + digitNames[place] + word;//for thousands to billion
           }
           place++;
           number /= 1000;
       } while (number > 0);

       return word.trim();
   }
}

public class Test {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int num;
      
       while (true) {
           System.out.print("Enter the integer to pronounce(any negative value to exit): ");
           num = sc.nextInt(); sc.nextLine();
          
           if(num<0) {
               System.out.println("Bye");
               break;
           }
           System.out.println(NumberToWords.convert(num));
           System.out.println();
       }
       sc.close();
   }
}

Output

<terminated> Test (1) [Java Application] C:\Program FilesJava\jre1.8.0_211\bin\javaw.exe (31-Oct-2019, 9:11:26 am) Enter the

Add a comment
Know the answer?
Add Answer to:
I need java code... Background Suppose you're working on the next great cligital assistant to compete...
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
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