Java help pleaaaase

Started by: Cronos | Replies: 9 | Views: 1,165

Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 16, 2012 12:46 PM #616939
Seriously this is annoying the crap out of me. Anyone know what I have done wrong. It compiles fine, however the printed output for fiftydollar should be 1. But it returns 0 when I run the program.

I don't know why.

ASSUMING the change variable is 50 or more. So the if statement must initiate.

import io.*;

public class test {

public static void main (String [] args) {

double change = ConsoleInput.readDouble ("Enter change");
int fiftydollar = 0;

if ((50.0 / change) >= 1.0)

fiftydollar = fiftydollar+1;
change = change - 50.0;

System.out.println ("Change " +change);
System.out.println ("fifty notes amount: " +fiftydollar);



}
}
Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 16, 2012 12:46 PM #616940
trying

fiftydollar = fiftydollar **;

does nothing.
Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 16, 2012 1:33 PM #616950
wait nevermind. Finally figured it out. I had the damn >= wrong. It is supposed to be <=. Additionally I never put braces around the if. So it only included the first statement in the if, and the second one was just executed regardless. That is what confused me lol...
Exile
Administrator
2

Posts: 8,404
Joined: Dec 2005
Rep: 10

View Profile
Mar 16, 2012 1:40 PM #616955
Glad we could help.
Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 16, 2012 2:19 PM #616996
Wait wait. Here's problem numero 2.

If you compile this and try to enter say uh 45.55 for cost and 120.65 for payment amount, you will get a slightly wrong answer for the amount of change required. You wont really know what I mean unless you compile and run it yourself.

I don't really know why the double isn't specific. Like instead of returning something like $56.55c, it would give me something like $56.55000000006c. Which makes no sense if you look at the math I used...

import io.*;

public class thecode {

public static void main (String [] args) {

double cost, payment, change;

cost = ConsoleInput.readDouble ("Enter the cost of the product");
payment = ConsoleInput.readDouble ("Enter your payment amount");

change = calcChange (cost, payment);
calcNotes (change);

}

public static double calcChange (double costIN, double paymentIN) {

double changeOUT = paymentIN - costIN;

return changeOUT;

}

public static void calcNotes (double changeIN) {

double changeReal = changeIN;
int fivecent, tencent, twentycent, fiftycent, onedollar, twodollar, fivedollar, tendollar, twentydollar, fiftydollar;
fivecent = tencent = twentycent = fiftycent = onedollar = twodollar = fivedollar = tendollar = twentydollar = fiftydollar = 0;

while (changeIN > 0.0) {

if ((50.0 / changeIN) <= 1.0) {

++fiftydollar;
changeIN = changeIN - 50.0;

}else if ((20.0 / changeIN) <= 1.0) {

++twentydollar;
changeIN = changeIN - 20.0;

}else if ((10.0 / changeIN) <= 1.0) {

++tendollar;
changeIN = changeIN - 10.0;

}else if ((5.0 / changeIN) <= 1.0) {

++fivedollar;
changeIN = changeIN - 5.0;

}else if ((2.0 / changeIN) <= 1.0) {

++twodollar;
changeIN = changeIN - 2.0;

}else if ((1.0 / changeIN) <= 1.0) {

++onedollar;
changeIN = changeIN - 1.0;

}else if ((0.5 / changeIN) <= 0.5) {

++fiftycent;
changeIN = changeIN - 0.5;

}else if ((0.2 / changeIN) <= 1.0) {

++twentycent;
changeIN = changeIN - 0.2;

}else if ((0.1 / changeIN) <= 1.0) {

++tencent;
changeIN = changeIN - 0.1;

}else{

++fivecent;
changeIN = changeIN - 0.05;

}
}

outputAll (fiftydollar, twentydollar, tendollar, fivedollar, twodollar, onedollar, fiftycent, twentycent, tencent, fivecent, changeReal);

}

public static void outputAll (int fiftydollarIN, int twentydollarIN, int tendollarIN, int fivedollarIN, int twodollarIN, int onedollarIN, int fiftycentIN, int twentycentIN, int tencentIN, int fivecentIN, double changeRealIN) {

System.out.println ("Change required to give to customer: $" +changeRealIN +"c");
System.out.println ("$50: " +fiftydollarIN);
System.out.println ("$20: " +twentydollarIN);
System.out.println ("$10: " +tendollarIN);
System.out.println ("$5 : " +fivedollarIN);
System.out.println ("$2 : " +twodollarIN);
System.out.println ("$1 : " +onedollarIN);
System.out.println ("50c: " +fiftycentIN);
System.out.println ("20c: " +twentycentIN);
System.out.println ("10c: " +tencentIN);
System.out.println ("5c : " +fivecentIN);


}

}


I also had to change the last else if to an else. Because otherwise if it were the same as the others, the program would loop forever. Because the double returns a slightly off value for no apparent reason...
Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 16, 2012 3:06 PM #617038
Actually I figured this out to. I just did some weird maths rounding thing that rounds off the double to two decimal points. Although I had to do this for every single operation with the double... which seems highly inefficient. There is probably a far easy way that I don't know lol.

import io.*;

public class checkout {

public static void main (String [] args) {

double cost, payment, change;

cost = ConsoleInput.readDouble ("Enter the product cost");
payment = ConsoleInput.readDouble ("Enter the payment amount");

change = calcChange (cost, payment);
calcNotes (change);

}

public static double calcChange (double costIN, double paymentIN) {

double changeTemp = paymentIN - costIN;
int temp = (int) Math.round (changeTemp*100);
double changeOUT = temp / 100.0;


return changeOUT;

}

public static void calcNotes (double changeIN) {

double changeReal = changeIN;
int fivecent, tencent, twentycent, fiftycent, onedollar, twodollar, fivedollar, tendollar, twentydollar, fiftydollar;
fivecent = tencent = twentycent = fiftycent = onedollar = twodollar = fivedollar = tendollar = twentydollar = fiftydollar = 0;

while (changeIN > 0.0) {

if ((50.0 / changeIN) <= 1.0) {

++fiftydollar;
changeIN = changeIN - 50.0;
int temp1 = (int) Math.round (changeIN*100);
changeIN = temp1 / 100.0;

}else if ((20.0 / changeIN) <= 1.0) {

++twentydollar;
changeIN = changeIN - 20.0;
int temp2 = (int) Math.round (changeIN*100);
changeIN = temp2 / 100.0;

}else if ((10.0 / changeIN) <= 1.0) {

++tendollar;
changeIN = changeIN - 10.0;
int temp3 = (int) Math.round (changeIN*100);
changeIN = temp3 / 100.0;

}else if ((5.0 / changeIN) <= 1.0) {

++fivedollar;
changeIN = changeIN - 5.0;
int temp4 = (int) Math.round (changeIN*100);
changeIN = temp4 / 100.0;

}else if ((2.0 / changeIN) <= 1.0) {

++twodollar;
changeIN = changeIN - 2.0;
int temp5 = (int) Math.round (changeIN*100);
changeIN = temp5 / 100.0;

}else if ((1.0 / changeIN) <= 1.0) {

++onedollar;
changeIN = changeIN - 1.0;
int temp6 = (int) Math.round (changeIN*100);
changeIN = temp6 / 100.0;

}else if ((0.5 / changeIN) <= 0.5) {

++fiftycent;
changeIN = changeIN - 0.5;
int temp7 = (int) Math.round (changeIN*100);
changeIN = temp7 / 100.0;

}else if ((0.2 / changeIN) <= 1.0) {

++twentycent;
changeIN = changeIN - 0.2;
int temp8 = (int) Math.round (changeIN*100);
changeIN = temp8 / 100.0;

}else if ((0.1 / changeIN) <= 1.0) {

++tencent;
changeIN = changeIN - 0.1;
int temp9 = (int) Math.round (changeIN*100);
changeIN = temp9 / 100.0;

}else if ((0.05 / changeIN) <= 1.0) {

++fivecent;
changeIN = changeIN - 0.05;
int temp10 = (int) Math.round (changeIN*100);
changeIN = temp10 / 100.0;

}
}

outputAll (fiftydollar, twentydollar, tendollar, fivedollar, twodollar, onedollar, fiftycent, twentycent, tencent, fivecent, changeReal);

}

public static void outputAll (int fiftydollarIN, int twentydollarIN, int tendollarIN, int fivedollarIN, int twodollarIN, int onedollarIN, int fiftycentIN, int twentycentIN, int tencentIN, int fivecentIN, double changeRealIN) {

System.out.println ("Change required to give to customer: $" +changeRealIN +"c");
System.out.println ("$50: " +fiftydollarIN);
System.out.println ("$20: " +twentydollarIN);
System.out.println ("$10: " +tendollarIN);
System.out.println ("$5 : " +fivedollarIN);
System.out.println ("$2 : " +twodollarIN);
System.out.println ("$1 : " +onedollarIN);
System.out.println ("50c: " +fiftycentIN);
System.out.println ("20c: " +twentycentIN);
System.out.println ("10c: " +tencentIN);
System.out.println ("5c : " +fivecentIN);


}

}
Jeff
Administrator
1

Posts: 4,356
Joined: Dec 2007
Rep: 10

View Profile
Mar 16, 2012 3:50 PM #617062
You're welcome
Exile
Administrator
2

Posts: 8,404
Joined: Dec 2005
Rep: 10

View Profile
Mar 16, 2012 4:56 PM #617097
We should start a programming help section. I never knew we were this helpful.
Cronos

Posts: 5,440
Joined: Apr 2009
Rep: 10

View Profile
Mar 17, 2012 9:10 PM #617756
Fuckn appreciate the help guys. Exceeded my expectations.
STUFF
2

Posts: 3,132
Joined: Aug 2005
Rep: 10

View Profile
Mar 18, 2012 12:06 AM #617856
I lol'd