Saturday, June 4, 2011

Java coin change help?!?

Create a Change application that prompts the user for an amount less than $1.00 and then displays the minimum number of coins necessary to make the change. The change can be made up of quarters, dimes, nickels, and pennies.





I can%26#039;t find the right codes for this.|||======================


Cashier.java


======================


public class Cashier {





private double amountDue;


private double received;


private double change;


private int temp;





public Cashier() {





}





public void setAmountDue(double pAmountDue) {


amountDue = pAmountDue;


this.calculateChange();


}





public void receive(double pReceived) {


received = pReceived;


this.calculateChange();


}





public double calculateChange() {


change = received - amountDue;


//System.out.println(%26quot;Change----: %26quot; + change);


return change;


}





public int returnDollars() {


temp = (int) change / 1;





/*for (int i = 0; i %26lt; temp; ++i) {


change = change - 1;


}*/


change = change % 1;





return (int) temp;


}





public int returnFiftyCents() {


temp = (int) (change / 0.5);


/*for (int i = 0; i %26lt; temp; ++i) {


change = change - 0.5;


}*/


change = change % 0.5;





return temp;


}





public int returnTwentyCents() {


temp = (int) (change / 0.2);


/*for (int i = 0; i %26lt; temp; ++i) {


change = change - 0.2;


}*/


change = change % 0.2;





return temp;


}





public int returnTenCents() {


temp = (int) (change / 0.1);


/*for (int i = 0; i %26lt; temp; ++i) {


change = change - 0.1;


}*/


change = change % 0.1;





return temp;


}





public int returnFiveCents() {


temp = (int) (change / 0.05);


for (int i = 0; i %26lt; temp; ++i) {


change = change - 0.05;


}


//System.out.println(%26quot;Change: %26quot; + change);


return temp;


}





public int returnOneCents() {


temp = (int) (change / 0.01);


for (int i = 0; i %26lt; temp; ++i) {


change = change - 0.01;


}


//System.out.println(%26quot;Change: %26quot; + change);


return temp;


}





}








=======================


CashierTest.java


=======================


import java.util.*;





public class CashierTest {





public static void main(String[] args) {





Scanner sc = new Scanner(System.in);





Cashier harry = new Cashier();


System.out.print(%26quot;Enter amount due : %26quot;);


harry.setAmountDue(sc.nextDouble()); // read value from console instead





System.out.print(%26quot;Enter amount received : %26quot;);


harry.receive(sc.nextDouble()); // read value from console instead





int dollars = harry.returnDollars();


int fiftyCents = harry.returnFiftyCents(); // returns 1


int twentyCents = harry.returnTwentyCents(); // returns 0


int tenCents = harry.returnTenCents(); // returns 1


int fiveCents = harry.returnFiveCents(); // returns 0


int oneCents = harry.returnOneCents(); // returns 3





//System.out.println(fityCents + %26quot; %26quot; + twentyCents + %26quot; %26quot; + tenCents + %26quot; %26quot; + fiveCents + %26quot; %26quot; + oneCents);


System.out.println(%26quot;Change to return: %26quot;);


System.out.println(%26quot;Num ( $1 ) = %26quot; + dollars);


System.out.println(%26quot;Num ( $0.50 ) = %26quot; + fiftyCents);


System.out.println(%26quot;Num ( $0.20 ) = %26quot; + twentyCents);


System.out.println(%26quot;Num ( $0.10 ) = %26quot; + tenCents);


System.out.println(%26quot;Num ( $0.05 ) = %26quot; + fiveCents);


System.out.println(%26quot;Num ( $0.01 ) = %26quot; + oneCents);





}





}

No comments:

Post a Comment