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

Project 3: Say My large Number For Project 3, you\'ll extend the program assigne

ID: 3707576 • Letter: P

Question

Project 3: Say My large Number

For Project 3, you'll extend the program assigned for the chapter 12 lab to handle larger numbers (up to 999 Quadrillion, or a few Quintillion).

70 pts: Modify the program to accept significantly larger numbers (at least to Quadrillions) by using the long integer type (long). You'll need to modify the input portion of your program to use Long.parseLong(... instead of Integer.parseInt(, and modify the say() function to accept longs instead of ints. To test a long number against a value greater than 1 billion, you'll need to append an "L" to the number, as in the following:

90 pts: Add the ability to go up to 9.2 Quintillion (max of a signed long). Add Javadoc comments for the say() method, including a description (with the limits), the parameter and return explained.

100 pts: Add appropriate try/catch and input checking:

if the user's entry isn't a number, handle the error gracefully and tell them they made a mistake;

if the entry is too large (>19 digits, or bigger than 9 Quintillion), tell them so;

if it's negative, either add the ability to handle it, or tell the user they can't go negative..

Submit just the Java source code files from your project's 'src' folder. The files should contain the following lines of comments (or similar) at the top

outcomes for this project:

Recursive concepts

Try and catch

Problem-solving with recursion

So far this is what I have, but I still don't have all the parts for it figured out. THe code does not follow the instrutions listed above. I have the 70pts part, but can't get the things in the 100 pts part, which is the part needed the most.

***************************************************************

import java.text.DecimalFormat;

public class Saymynumber {

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"

};

private Saymynumber() {}

private static String convertLessThanOneThousand(int number) {

String soFar;

if (number % 100 < 20){

soFar = numNames[number % 100];

number /= 100;

}

else {

soFar = numNames[number % 10];

number /= 10;

soFar = tensNames[number % 10] + soFar;

number /= 10;

}

if (number == 0) return soFar;

return numNames[number] + " hundred" + soFar;

}

/*

Javadoc for say() function

*/

/**

*

* public recursive method say() to convert long numbers to words

*

* @author  

* @version 1.1

* @param Stirng number - Number to be spelled out

* Valid range - Input number is casted to Long type so the valid range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

* @return - Returns the number converted into words

*/

public static String say(String number){

try{

Long n = Long.parseLong(number);

String negativeIndicator = "";

/*

To handle negative number

*/

if(n < 0){

negativeIndicator = "Minus";

n = n * -1;

}

/*

call private say method if the input is valid

*/

return negativeIndicator + say(n);

}

catch(NumberFormatException n){

/*

Print error if the number range is not valid

*/

System.out.println("Error: Please input proper number lesser than 9,223,372,036,854,775,807L");

}

return "";

}

/*

Private overloaded say method which accepts valid long number and converts it to words

*/

private static String say(Long n){

/*

Rest of the code is just checking the range of n

*/

if(n >= 1000000000000000000L)

return say(n / 1000000000000000000L) + " Quintillion " + say(n % 1000000000000000000L);

else if(n >= 1000000000000000L)

return say(n / 1000000000000000L) + " Quadrillion " + say(n % 1000000000000000L);

if(n >= 1000000000000L)

return say(n / 1000000000000L) + " trillion " + say(n % 1000000000000L);

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(long number) {

// 0 to 999 999 999 999

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

String snumber = Long.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", " ");

}

/**

* testing

* @param args

*/

public static void main(String[] args) {

System.out.println("*** " + Saymynumber.convert(0));

System.out.println("*** " + Saymynumber.convert(1));

System.out.println("*** " + Saymynumber.convert(16));

System.out.println("*** " + Saymynumber.convert(100));

System.out.println("*** " + Saymynumber.convert(118));

System.out.println("*** " + Saymynumber.convert(200));

System.out.println("*** " + Saymynumber.convert(219));

System.out.println("*** " + Saymynumber.convert(800));

System.out.println("*** " + Saymynumber.convert(801));

System.out.println("*** " + Saymynumber.convert(1316));

System.out.println("*** " + Saymynumber.convert(1000000));

System.out.println("*** " + Saymynumber.convert(2000000));

System.out.println("*** " + Saymynumber.convert(3000200));

System.out.println("*** " + Saymynumber.convert(700000));

System.out.println("*** " + Saymynumber.convert(9000000));

System.out.println("*** " + Saymynumber.convert(9001000));

System.out.println("*** " + Saymynumber.convert(123456789));

System.out.println("*** " + Saymynumber.convert(2147483647));

System.out.println("*** " + Saymynumber.convert(3000000010L));

/*

Added 4 more conditions to test the new say method

*/

System.out.println("*** " + Saymynumber.say("rerere"));

System.out.println("*** " + Saymynumber.say("9223372036854775808"));

System.out.println("*** " + Saymynumber.say("9223372036854775807"));

System.out.println("*** " + Saymynumber.say("-158913"));

}

}

Explanation / Answer

You have already written the code correctly. Be specific on which part do you need help in, please.