Java Programming Assignment. Need Help. Instructions for assignment are below. T
ID: 3697433 • Letter: J
Question
Java Programming Assignment. Need Help. Instructions for assignment are below.
The name of your program/driver class should be WritingChecks.java
WritingChecks.java will be your driver class. This class will prompt the user to enter the check amount which should be a number between 0 and 99,999. WritingChecks will continue to loop until the user decides to stop. The Number.java class will be called to create an object of class Number. WritingChecks will create an array of Number objects created by the user. Once the user decides to stop the program, the Array of Numbers should be printed in reverse order.
Example: The user enters 397.899 and 2015.1
Your code should print:
two thousand fifteen 10/100
three hundred ninety seven 90/100
Number.java will have a constructor, two set methods, two get methods, a toString method and an equals method. The instance variables for Number.java. Some of these methods will not be used but they still need to be programmed. Failure to do so will result in points being deducted.
Instance Variables
double amount -User provides the amount
String spelling -String will be built within the class, not provided by the user
Set methods:
setAmount and setSpelling. The setSpelling method is the one that will spell the number. The setSpelling method may call any other number of methods to get the task done. This is also the method that will throw an exception if the number is not in range.
Other Methods in the Number.java class:
In order to efficiently spell numbers, you will need to create several other methods. These methods will also be part of the Number class. Because these methods should not be accessed by any other class they should be declared as private.
NumberException.java
You will need an exception so that it may be thrown when the user enters a number that is not in range. If the user enters more than two decimals you should just round but do not throw an exception for this.
Other requirements:
Your program should work with any number from 0 to 99,999.
Make sure that your error messages are "user friendly", specific and that they explain why things happen. You should display error messages if the number is out of range or if the user input is not a number.
Double numbers should be truncated and only the integer part should be spelled
You should use switch statements. You should not use more than 5 switch statements. None of the switch statements should contain more than 20 cases.
If you use if/else statements, you should not use more than 10 of these.
You should use at least 3 methods, more is better, will make your life easier
Your program should compile and run in JGrasp.
Your program should be commented and coded according to the JAVA coding standard.
This time you are allowed to use magic numbers
Explanation / Answer
import java.util.*;
public class NumberToWord
{
private static String input;
private static int num;
private static String[] units=
{"",
" One",
" Two",
" Three",
" Four",
" Five",
" Six",
" Seven",
" Eight",
" Nine"
};
private static String[] teen=
{" Ten",
" Eleven",
" Twelve",
" Thirteen",
" Fourteen",
" Fifteen",
" Sixteen",
" Seventeen",
" Eighteen",
" Nineteen"
};
private static String[] tens=
{ " Twenty",
" Thirty",
" Forty",
" Fifty",
" Sixty",
" Seventy",
" Eighty",
" Ninety"
};
private static String[] maxs=
{"",
"",
" Hundred",
" Thousand",
" Lakh",
" Crore"
};
public String convertNumberToWords(int n)
{
input=numToString(n);
String converted="";
int pos=1;
boolean hun=false;
while(input.length()> 0)
{
if(pos==1) // TENS AND UNIT POSITION
{ if(input.length()>= 2) // TWO DIGIT NUMBERS
{
String temp=input.substring(input.length()-2,input.length());
input=input.substring(0,input.length()-2);
converted+=digits(temp);
}
else if(input.length()==1) // 1 DIGIT NUMBER
{
converted+=digits(input);
input="";
}
pos++;
}
else if(pos==2) // HUNDRED POSITION
{
String temp=input.substring(input.length()-1,input.length());
input=input.substring(0,input.length()-1);
if(converted.length()> 0&&digits(temp)!="")
{
converted=(digits(temp)+maxs[pos]+" and")+converted;
hun=true;
}
else
{
if
(digits(temp)=="");
else
converted=(digits(temp)+maxs[pos])+converted;hun=true;
}
pos++;
}
else if(pos > 2) // REMAINING NUMBERS PAIRED BY TWO
{
if(input.length()>= 2) // EXTRACT 2 DIGITS
{
String temp=input.substring(input.length()-2,input.length());
input=input.substring(0,input.length()-2);
if(!hun&&converted.length()> 0)
converted=digits(temp)+maxs[pos]+" and"+converted;
else
{
if(digits(temp)=="") ;
else
converted=digits(temp)+maxs[pos]+converted;
}
}
else if(input.length()==1) // EXTRACT 1 DIGIT
{
if(!hun&&converted.length()> 0)
converted=digits(input)+maxs[pos]+" and"+converted;
else
{
if(digits(input)=="") ;
else
converted=digits(input)+maxs[pos]+converted;
input="";
}
}
pos++;
}
}
return converted;
}
private String digits(String temp) // TO RETURN SELECTED NUMBERS IN WORDS
{
String converted="";
for(int i=temp.length()-1;i >= 0;i--)
{ int ch=temp.charAt(i)-48;
if(i==0&&ch>1 && temp.length()> 1)
converted=tens[ch-2]+converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER TENS
else if(i==0&&ch==1&&temp.length()==2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER TEENS
{
int sum=0;
for(int j=0;j < 2;j++)
sum=(sum*10)+(temp.charAt(j)-48);
return teen[sum-10];
}
else
{
if(ch > 0)
converted=units[ch]+converted;
} // IF SINGLE DIGIT PROVIDED
}
return converted;
}
private String numToString(int x) // CONVERT THE NUMBER TO STRING
{
String num="";
while(x!=0)
{
num=((char)((x%10)+48))+num;
x/=10;
}
return num;
}
private void inputNumber()
{
Scanner in=new Scanner(System.in);
try
{
System.out.print("Please enter number to Convert into Words : ");
num=in.nextInt();
}
catch(Exception e)
{
System.out.println("Number should be Less than 1 Arab ");
System.exit(1);
}
}
public static void main(String[] args)
{
NumberToWord obj=new NumberToWord();
obj.inputNumber();
System.out.println("input in Words : "+obj.convertNumberToWords(num));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.