Need Java Help Stat!
#1
I need a hand with Java. This is what I have to do. If anyone can put me on the right direction as to how to set it up, I'd appreciate it.
temp = cashPaid - purchaseAmount;
temp = temp - int (temp) + 0.00001;
change = int (temp * 100);
The file ComputeChange.java contains the beginning code for the project. You need to create a project called change inside of your JavaProjects folder and then save the file in this folder. The program
Prompts the user for the following information.
Amount of purchase
Amount of cash tendered
Calculates the correct change.
Prints the amount of change.
Open the program and run the program with the following data:
Amount of purchase = 23.06
Cash tendered = 30.00
You just received an email from a friend of yours who heard that you were learning to program and wants you to change this program so that in addition to printing the correct change in dollars and cents, it also prints the number of dollars, and the number of each type of coin. When you run your program with the above data you should get the following output:
Amount of purchase = 23.06
Cash tendered = 30.00
Change = $6.94
You will need
6 dollars
3 quarters
1 dime
1 nickel
4 pennies
Do not worry about singular versus plural endings, i.e. quarter/quarters. Also make sure that if the change is something like $4.50, your program will print the ending zero.
Thanks to anyone that can help.
- Anthony
temp = cashPaid - purchaseAmount;
temp = temp - int (temp) + 0.00001;
change = int (temp * 100);
The file ComputeChange.java contains the beginning code for the project. You need to create a project called change inside of your JavaProjects folder and then save the file in this folder. The program
Prompts the user for the following information.
Amount of purchase
Amount of cash tendered
Calculates the correct change.
Prints the amount of change.
Open the program and run the program with the following data:
Amount of purchase = 23.06
Cash tendered = 30.00
You just received an email from a friend of yours who heard that you were learning to program and wants you to change this program so that in addition to printing the correct change in dollars and cents, it also prints the number of dollars, and the number of each type of coin. When you run your program with the above data you should get the following output:
Amount of purchase = 23.06
Cash tendered = 30.00
Change = $6.94
You will need
6 dollars
3 quarters
1 dime
1 nickel
4 pennies
Do not worry about singular versus plural endings, i.e. quarter/quarters. Also make sure that if the change is something like $4.50, your program will print the ending zero.
Thanks to anyone that can help.
- Anthony
#3
One way would be to take the change and do a mod (I think the modulus operator in Java is %) and division for each amount. It'd be easier if you multiply the change by 100 first, so you can use all integers. For example, if your change is $1.89, you'd multiply by 100 to get 189. Then you'd divide 189/100 to get one dollar, then use 189%100 to get 89. For quarters, you'd divide 89/25 to get 3, then do 89%25 to get 14, and so on.
As for the actual code, I could write it in C++ or VB, but I haven't done Java in years and years.
As for the actual code, I could write it in C++ or VB, but I haven't done Java in years and years.
#5
* @author Anthony Pastorini
* @created 2/16/04
*/
import chn.util.*;
public class ComputeChange
{
/**
* The main program for the Change class
*
* @param args The command line arguments - not used
*/
public static void main(String[] args)
{
double purchase, cashPaid, temp;
int change, remainder, dollars, quarters, dimes, nickels, pennies;
ConsoleIO keyboard = new ConsoleIO();
System.out.print("Enter amount of purchase --> ");
purchase = keyboard.readDouble();
System.out.print("Enter amount of cash paid --> ");
cashPaid = keyboard.readDouble();
temp = cashPaid - purchase;
dollars = (int) temp;
temp = temp - (int)temp + 0.00001;
change = (int)(temp * 100);
remainder = dollars;
quarters = change/25;
dimes = (change/25)/10;
nickels = ((change/25)/10)/5;
pennies = (((change/25)/10)/5)/1;
System.out.println("\n$" + dollars + "." + change);
System.out.println("\nDollars " +remainder + "\nQuarters " +quarters + "\nDimes " +dimes + "\nNickels " +nickels + "\nPennies " +pennies);
}
I would greatly appreciate anyone who can tell me what's wrong with that... The dollars and quarters are right, but the dimes are wrong. And if the dimes are wrong, the rest are wrong. Any help would be great!
- Anthony
* @created 2/16/04
*/
import chn.util.*;
public class ComputeChange
{
/**
* The main program for the Change class
*
* @param args The command line arguments - not used
*/
public static void main(String[] args)
{
double purchase, cashPaid, temp;
int change, remainder, dollars, quarters, dimes, nickels, pennies;
ConsoleIO keyboard = new ConsoleIO();
System.out.print("Enter amount of purchase --> ");
purchase = keyboard.readDouble();
System.out.print("Enter amount of cash paid --> ");
cashPaid = keyboard.readDouble();
temp = cashPaid - purchase;
dollars = (int) temp;
temp = temp - (int)temp + 0.00001;
change = (int)(temp * 100);
remainder = dollars;
quarters = change/25;
dimes = (change/25)/10;
nickels = ((change/25)/10)/5;
pennies = (((change/25)/10)/5)/1;
System.out.println("\n$" + dollars + "." + change);
System.out.println("\nDollars " +remainder + "\nQuarters " +quarters + "\nDimes " +dimes + "\nNickels " +nickels + "\nPennies " +pennies);
}
I would greatly appreciate anyone who can tell me what's wrong with that... The dollars and quarters are right, but the dimes are wrong. And if the dimes are wrong, the rest are wrong. Any help would be great!
- Anthony
#7
Example, if your change is .89 you'd have:
3 quarters
1 dime
4 pennies
quarters = change/25; would give you quarters = 3. Now, if you use the mod operator like I originally suggested, you'd get change%25=14 so dimes =14/10 = 1. Otherwise, you can use dimes = (Change - (25*Quarters))/10 like I suggested above and get dimes =(89-(25*3))/10 = (89-75)/10 = 14/10 = 1 and then so on for the rest of the coins.
3 quarters
1 dime
4 pennies
quarters = change/25; would give you quarters = 3. Now, if you use the mod operator like I originally suggested, you'd get change%25=14 so dimes =14/10 = 1. Otherwise, you can use dimes = (Change - (25*Quarters))/10 like I suggested above and get dimes =(89-(25*3))/10 = (89-75)/10 = 14/10 = 1 and then so on for the rest of the coins.
#9
Aaaahhhhh... Yes! I forgot modulus was used for remainders! I was wondering why my quarters amount got f*cked up when I used it. lol time to re-work the equations.
I'm such a tard...
Thanks for the help man. It's greatly appreciated.
I'm such a tard...
Thanks for the help man. It's greatly appreciated.