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

PLEASE GIVE ME AN INTERMEDIATE\\ADVANCE RESPONSE SOME INFOR I\'M STILL LEARNING

ID: 3624569 • Letter: P

Question

PLEASE GIVE ME AN INTERMEDIATEADVANCE RESPONSE SOME INFOR I'M STILL LEARNING THANK YOU

Design a class Numbers that can be used to translated whole dollars amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand two hundred three.

The class should have a single integer member variable

int number;

and a collection of static string members that specify how to translate key dollar amounts into the desired format. For example: you might use static strings such as:

string lessThan20[ ] =
{"zero", "one", ..., "eighteen", "nineteen" };
String hundred = “hundred”;
String thousand = “thousand”;
The class should have a constructor that accepts a non-negative integer and uses it to initialize the Numbers object. It should have a member function print() that prints the English description of the Numbers object. Demonstrate the class by writing a main program that asks the user to enter a number in the proper range and than prints out its English description.

Explanation / Answer

package cramster; /*** * Cramster id 1216814 * @author Andreas * */ public class Numbers { private int number; public static String[] String[] { "ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE", "TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"}; public static String[] TENS = new String[] {"TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"}; public static String HUNDRED = "HUNDRED"; public static String THOUSAND = "THOUSAND"; public Numbers(int number) { if (number < 0 ) throw new IllegalArgumentException(); this.number = number; } public String toString() { StringBuilder sb = new StringBuilder(); int[] counts = getCounts(); if (counts[0] > 0) sb.append(ONES[counts[0]] + " " + THOUSAND + " ") ; if (counts[1] > 0) sb.append(ONES[counts[1]] + " " + HUNDRED + " "); if (counts[2] > 0) sb.append(TENS[counts[2]-2] + " "); if (counts[3] > 0) sb.append(ONES[counts[3]]); return sb.toString().toLowerCase(); } private int[] getCounts() { int counts[] = new int[4]; int thousands = 0; int hundreds = 0; int tens = 0; int int tmp = number; thousands = (int)tmp / 1000; tmp = tmp - thousands*1000; counts[0] = thousands; hundreds = (int) tmp / 100; counts[1] = hundreds; tmp = tmp - hundreds*100; tens = (int) tmp/10 > 1 ? (int) tmp/10 : 0; counts[2] = tens; // 17 tens = 7, tmp = tens > 1 ? tmp - (tens * 10) : tmp; counts[3] = ones; return counts; } public static void main(String[] args) { Numbers n = new Numbers(9999); System.out.println(n.toString()); } }
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