Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following program and it is meant to implement recursion, but I do not know how to do that. Here is the assignment:

I have the following program and it is meant to implement recursion, but I do not know how to do that. Here is the assignment:

You will produce a short, elegant, recursive algorithm that spells out any number between 1 and 2.1 billion. Allow the user to input a number in the range from 1 to 2.1 billion (a positive 32-bit int), then spell out the number (e.g. 123456 would output "one hundred twenty three thousand four hundred fifty six"). Chap12_updated2say has a simple flow diagram that could represent the logic you use for this. Use a recursive method "say(n)" where say(n) returns the string corresponding to the input integer n. The beauty of recursion is that this can be done with just a few if statements and switch/case values (about 30 or so total), thanks to the way we read numbers (the number 123,123,123 is spoken the same as a single 123, but with a few "place" words - million, thousand, etc.).

Deliverable: Saymynumber.java

And here is my code:

//////////////////////////////////////////

// SayMyNumber.java

// Ramya Raja

// CSCI 1302

// November 10th 2018

// Chapter 12 Lab 2

/////////////////////////////////////////

import java.text.DecimalFormat;

import java.util.Scanner;

public class SayMyNumber {

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

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

private SayMyNumber() {}

private static String convertLessThanOneThousand(int number) { String soFar; if (number % 100 < 20) { soFar = numberNames[number % 100]; number /= 100; } else { soFar = numberNames[number % 10]; number /= 10; soFar = tenNames[number % 10] + soFar; number /= 10; } if (number == 0) return soFar;

return numberNames[number] + " hundred" + soFar; }

public static String say(String number) { try{ Long n = Long.parseLong(number); String negativeIndicator = ""; //for negative number if(n < 0) { negativeIndicator = "Minus"; n = n * -1; } return negativeIndicator + say(n); } catch(NumberFormatException n) { System.out.println("Error: Please input proper number lesser than 9,223,372,036,854,775,807L"); } return ""; } private static String say(Long n) { if(n >= 1000000000) return say(n / 1000000000) + " billion " + say(n % 1000000000); else if(n >= 1000000) return say(n / 1000000) + " million " + say(n % 1000000); else if(n >= 1000) return say(n / 1000) + " thousand " + say(n % 1000); else return convertLessThanOneThousand(n.intValue()); }

public static String convert(double number) {// 0 to 999 999 999 999 if (number == 0) { return "zero"; } String snumber = Double.toString(number); // pad with "0" String mask = "000000000000"; DecimalFormat df = new DecimalFormat(mask); snumber = df.format(number); // XXXnnnnnnnnn int billions = Integer.parseInt(snumber.substring(0,3)); // nnnXXXnnnnnn int millions = Integer.parseInt(snumber.substring(3,6)); // nnnnnnXXXnnn int hundredThousands = Integer.parseInt(snumber.substring(6,9)); // nnnnnnnnnXXX int thousands = Integer.parseInt(snumber.substring(9,12));

String tradBillions; switch (billions) { case 0: tradBillions = ""; break; case 1:tradBillions = convertLessThanOneThousand(billions) + " billion "; break; default:tradBillions = convertLessThanOneThousand(billions) + " billion "; }

String result = tradBillions; String tradMillions; switch (millions) { case 0:tradMillions = ""; break; case 1 :tradMillions = convertLessThanOneThousand(millions) + " million "; break; default :tradMillions = convertLessThanOneThousand(millions) + " million "; }

result = result + tradMillions; String tradHundredThousands; switch (hundredThousands) { case 0:tradHundredThousands = ""; break; case 1 :tradHundredThousands = "one thousand "; break; default :tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " thousand "; }

result = result + tradHundredThousands; String tradThousand; tradThousand = convertLessThanOneThousand(thousands); result = result + tradThousand; // remove extra spaces! return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); }

//main method

public static void main(String[] args) { String newInput = "y"; while( newInput.equals("y") || newInput.equals("Y")) { Scanner input = new Scanner(System.in); System.out.println("Enter a number to find out how to spell it: "); Long num = input.nextLong(); if((num < 9223372036854775807L) || num > 0) { System.out.println((SayMyNumber.convert(num))); System.out.println("Another number? (y/n) "); newInput = input.next(); } else { System.out.println(SayMyNumber.say(num)); System.out.println("Another number? (y/n)"); newInput = input.next(); } } } }

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

Students also viewed these Databases questions