Insert BS here A place to discuss anything you want!

Need Java Help Stat!

Thread Tools
 
Search this Thread
 
Old 02-16-2004, 11:33 AM
  #1  
Senior Member
Thread Starter
 
Eyxom's Avatar
 
Join Date: Dec 2003
Location: Port St. Lucie, FL
Posts: 638
Default

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
Eyxom is offline  
Old 02-16-2004, 11:34 AM
  #2  
Senior Member
 
FrestyleFC3S's Avatar
 
Join Date: Apr 2003
Location: Schofield Barracks, HI
Posts: 2,259
Default

:a splode:
FrestyleFC3S is offline  
Old 02-16-2004, 01:03 PM
  #3  
Senior Member
 
UniqueTII's Avatar
 
Join Date: Nov 2001
Location: Ames, IA
Posts: 4,773
Default

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.
UniqueTII is offline  
Old 02-16-2004, 01:13 PM
  #4  
Super Moderator
 
Baldy's Avatar
 
Join Date: Sep 2002
Location: Tallahassee, FL
Posts: 5,425
Default

Originally Posted by FrestyleFC3S' date='Feb 16 2004, 12:34 PM
:a splode:
I concur.



Good luck.
Baldy is offline  
Old 02-16-2004, 07:10 PM
  #5  
Senior Member
Thread Starter
 
Eyxom's Avatar
 
Join Date: Dec 2003
Location: Port St. Lucie, FL
Posts: 638
Default

* @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
Eyxom is offline  
Old 02-16-2004, 07:13 PM
  #6  
Senior Member
 
UniqueTII's Avatar
 
Join Date: Nov 2001
Location: Ames, IA
Posts: 4,773
Default

Dimes would be (Change - (25*Quarters))/10 and so on
UniqueTII is offline  
Old 02-16-2004, 07:17 PM
  #7  
Senior Member
 
UniqueTII's Avatar
 
Join Date: Nov 2001
Location: Ames, IA
Posts: 4,773
Default

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.
UniqueTII is offline  
Old 02-16-2004, 07:25 PM
  #8  
Senior Member
 
teknics's Avatar
 
Join Date: Dec 2002
Location: Wayne, NJ
Posts: 4,837
Default

i have one thing to say.



**** java.



kevin.
teknics is offline  
Old 02-17-2004, 10:46 AM
  #9  
Senior Member
Thread Starter
 
Eyxom's Avatar
 
Join Date: Dec 2003
Location: Port St. Lucie, FL
Posts: 638
Default

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.
Eyxom is offline  
Old 02-17-2004, 10:48 AM
  #10  
Senior Member
Thread Starter
 
Eyxom's Avatar
 
Join Date: Dec 2003
Location: Port St. Lucie, FL
Posts: 638
Default

Originally Posted by teknics' date='Feb 16 2004, 05:25 PM
i have one thing to say.



**** java.



kevin.
Java = $$$$ WIN $$$$
Eyxom is offline  


Quick Reply: Need Java Help Stat!



All times are GMT -5. The time now is 01:16 PM.