Insert BS here A place to discuss anything you want!

Need Java Help Stat!

Old Feb 16, 2004 | 11:33 AM
  #1  
Eyxom's Avatar
Thread Starter
Senior Member
 
Joined: Dec 2003
Posts: 638
From: Port St. Lucie, FL
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
Old Feb 16, 2004 | 11:34 AM
  #2  
FrestyleFC3S's Avatar
Senior Member
 
Joined: Apr 2003
Posts: 2,259
From: Schofield Barracks, HI
Default

:a splode:
Old Feb 16, 2004 | 01:03 PM
  #3  
UniqueTII's Avatar
Senior Member
 
Joined: Nov 2001
Posts: 4,775
From: Ames, IA
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.
Old Feb 16, 2004 | 01:13 PM
  #4  
Baldy's Avatar
Super Moderator
 
Joined: Sep 2002
Posts: 5,425
From: Tallahassee, FL
Default

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



Good luck.
Old Feb 16, 2004 | 07:10 PM
  #5  
Eyxom's Avatar
Thread Starter
Senior Member
 
Joined: Dec 2003
Posts: 638
From: Port St. Lucie, FL
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
Old Feb 16, 2004 | 07:13 PM
  #6  
UniqueTII's Avatar
Senior Member
 
Joined: Nov 2001
Posts: 4,775
From: Ames, IA
Default

Dimes would be (Change - (25*Quarters))/10 and so on
Old Feb 16, 2004 | 07:17 PM
  #7  
UniqueTII's Avatar
Senior Member
 
Joined: Nov 2001
Posts: 4,775
From: Ames, IA
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.
Old Feb 16, 2004 | 07:25 PM
  #8  
teknics's Avatar
Senior Member
 
Joined: Dec 2002
Posts: 4,837
From: Wayne, NJ
Default

i have one thing to say.



**** java.



kevin.
Old Feb 17, 2004 | 10:46 AM
  #9  
Eyxom's Avatar
Thread Starter
Senior Member
 
Joined: Dec 2003
Posts: 638
From: Port St. Lucie, FL
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.
Old Feb 17, 2004 | 10:48 AM
  #10  
Eyxom's Avatar
Thread Starter
Senior Member
 
Joined: Dec 2003
Posts: 638
From: Port St. Lucie, FL
Default

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



**** java.



kevin.
Java = $$$$ WIN $$$$

Thread Tools
Search this Thread

All times are GMT -5. The time now is 02:09 PM.