# New to this, is my code right???

• September 21st, 2010, 04:03 PM
ryan1234
New to this, is my code right???
new at this does my code work out to display this output....

Output:

Enter the amount of the purchases: (user enters 44.28)
Enter the cash tendered: (user enters 50)

Your change is equal to 5.71999999999 which is...
5 dollars 72 cents
2 quarter (s)
2 dime(s)
0 nickel (s)
2 pennie (s)
__________________________________________________ _
package lab02;

/**
* Purpose: Compute the amount of change a customer should
* receive (in terms of dollars, quarters, dimes, nickels, and
* pennies) given the amount of the purchase and the cash tendered.
*
* @author jdoe
*
*/

import java.util.Scanner;

public class Change
{

/**
* @param args
*/
public static void main(String[] args)
{
double purchaseAmt;
double cashTendered;
double totalChange;
int dollars, cents;

// Create a Scanner object to read from standard input
Scanner scan = new Scanner(System.in);

// Prompt the user and read in amount of purchase and
// cash tendered.
System.out.print("Enter the amount of the purchase: ");
purchaseAmt = scan.nextDouble();
System.out.print("Enter the cash tendered: ");
cashTendered = scan.nextDouble();

totalChange = cashTendered - purchaseAmt;
dollars = (int) totalChange;

System.out.println();
System.out.println ("Your change is \$" + totalChange
+ " which is ...");
System.out.println(dollars + " dollars");

System.out.println();
int quarters, pennies, dimes ,nickels;
quarters = (totalChange-dollars)/.25;
System.out.println(quarters + "quarter (s)");
dimes= (totalChange-dollars-(.25*quarters))/.10;
System.out.println(dimes + "dime (s)");
pennies= (totalChange-dollars-(.25*quarters)-(.10*dimes)/.01);
System.out.println(pennies + "pennies");

}

}
• September 21st, 2010, 04:38 PM
Darryl.Burke
Re: New to this, is my code right???
Double post of
http://www.javaprogrammingforums.com...ing-learn.html

ryan1234 also started three threads with the same question at the Sun/Oracle forums. The threads have been removed and the user blocked for multiposting.

db