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

Continuing the discussion of the previous exercise, we reiterate the importance

ID: 3807968 • Letter: C

Question

Continuing the discussion of the previous exercise, we reiterate the importance of designing check-writing systems to prevent alteration of check amounts. One common security method requires that the check amount be both written in numbers and "spelled out" in words. Even if someone is able to alter the numerical amount of the check, it's extremely difficult to change the amount in words. Write a program that inputs a numeric check amount and writes the word equivalent of the amount. For example, the amount 52.43 should he written as FIFTY TWO and 43/100

Explanation / Answer


public class WordEquivalent {

   private static final String[] specialNames = { "", " Thousand", " Million", " Billion", " Trillion", " Quadrillion",
           " Quintillion" };

   private static final String[] tensNames = { "", " Ten", " Twenty", " Thirty", " Forty", " Fifty", " Sixty",
           " Seventy", " Eighty", " Ninety" };

   private static final String[] numNames = { "", " One", " Two", " Three", " Four", " Five", " Six", " Seven",
           " Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen",
           " Seventeen", " Eighteen", " Nineteen" };
  
    public String convertToWord(float inputNumber) {

        if (inputNumber == 0) {
           return "zero";
        }
      
        if (inputNumber < 0) {
           return "";
        }
      
        String wordEquivalent = "";
        int position = 0;

        int integerVal = (int) inputNumber;
        do {
            int n = integerVal % 1000;
            if (n != 0){
                String str = convertBelowThousand(n);
                wordEquivalent = str + specialNames[position] + wordEquivalent;
            }
            integerVal /= 1000;
            position++;
        } while (integerVal > 0);

     
       wordEquivalent = wordEquivalent + " " + convertFractionToWord(inputNumber);
       return wordEquivalent.trim();
    }
     
    private static String convertFractionToWord(float inputNumber) {
      
       if (inputNumber-(long)inputNumber == 0) {
           return "";
       }
      
        String fraction = String.valueOf(inputNumber);
        String fractionWordEquivalent = "and ";
      
        int fractionIndex = fraction.indexOf(".");
        if (fractionIndex >=0) {
            String substring = fraction.substring(fractionIndex+1);
            fractionWordEquivalent += substring +"/" +(long)Math.pow(10, substring.length());
        }
        return fractionWordEquivalent;
   }
  

    private String convertBelowThousand(int num) {
        String wordEquivalent;    
      
       if (num%100 < 20){
            wordEquivalent = numNames[num % 100];
            num /= 100;
        }
        else {
            wordEquivalent = numNames[num % 10];
            num /= 10;
          
            wordEquivalent = tensNames[num % 10] + wordEquivalent;
            num /= 10;
        }
      
        if (num == 0) {
           return wordEquivalent;          
        }

        return numNames[num] + " hundred and" + wordEquivalent;
    }
  
    public static void main(String[] args) {
       System.out.println(new WordEquivalent().convertToWord(n52.43f));
    }

}

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