Docsity

Change Maker Java - Assignment 1 | Programming I | COP 2210, Study notes of Computer Science

Toggle sidebar
bg1
The First Programming Assignment (1) (Copy and paste when necessary)
ChangeMaker.java
/**
* File: ChangeMaker.java
*
* @ Name
*
* I affirm that this program is entirely my own work and
* none of it is the work of any other person.
*/
import java.text.NumberFormat; // to format o/p as currency
/**
* Computes change due - using the minimum coinage possible - when
* the user enters the amount of a purchase and the amount tendered
*/
public class ChangeMaker
{
// instance var's
private double amountDue ; // total cost of purchases
private double amountTendered ; // amount paid
private int dollarsDue ; // number of dollar coins due in change
private int quartersDue ; // number of quarters due
private int dimesDue ; // number of dimes due
private int nickelsDue ; // number of nickels due
private int penniesDue ; // number of pennies due
private NumberFormat formatter = NumberFormat.getCurrencyInstance() ;
/**
* Constructs a ChangeMaker object
* @param totalDue the amount of the purchase
* @param amountReceived the amount tendered (paid)
*/
public ChangeMaker(double totalDue, double amountReceived)
{
// set instance variables to parameters passed
amountDue = totalDue ;
amountTendered = amountReceived ;
// other instance var's automatically initialized to 0
pf3

Partial preview of the text

Download Change Maker Java - Assignment 1 | Programming I | COP 2210 and more Study notes Computer Science in PDF only on Docsity!

The First Programming Assignment (1) (Copy and paste when necessary) ChangeMaker.java /**

  • File: ChangeMaker.java
  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / import java.text.NumberFormat; // to format o/p as currency /*
  • Computes change due - using the minimum coinage possible - when
  • the user enters the amount of a purchase and the amount tendered / public class ChangeMaker { // instance var's private double amountDue ; // total cost of purchases private double amountTendered ; // amount paid private int dollarsDue ; // number of dollar coins due in change private int quartersDue ; // number of quarters due private int dimesDue ; // number of dimes due private int nickelsDue ; // number of nickels due private int penniesDue ; // number of pennies due private NumberFormat formatter = NumberFormat.getCurrencyInstance() ; /*
  • Constructs a ChangeMaker object
  • @param totalDue the amount of the purchase
  • @param amountReceived the amount tendered (paid) */ public ChangeMaker(double totalDue, double amountReceived) { // set instance variables to parameters passed amountDue = totalDue ; amountTendered = amountReceived ; // other instance var's automatically initialized to 0

System.out.println("\nAmount Due: " + formatter.format(amountDue)) ; System.out.println("Amount Received: "

  • formatter.format(amountTendered)) ; } /**
  • Computes the number of coins of each denomination to be given as change */ public void computeChange() { int changeDue ; // total change due, in cents if (amountDue == amountTendered) { System.out.println("\nExact change! Thanks and have a nice day!") ; } else if (amountDue > amountTendered) // owes more money! { System.out.println("\nLo siento, you still owe me another "
  • formatter.format(amountDue - amountTendered)) ; } else // has change coming { // compute change due, in cents for easier calculations changeDue = (int) Math.round((amountTendered - amountDue) * 100) ; // compute number of dollars due, and reduce change due accordingly dollarsDue = changeDue / 100 ; changeDue = changeDue % 100 ; // now do the same for all other coins quartersDue = changeDue / 25 ; changeDue = changeDue % 25 ; dimesDue = changeDue / 10 ; changeDue = changeDue % 10 ; nickelsDue = changeDue / 5 ; changeDue = changeDue % 5 ; penniesDue = changeDue ; // change due is <= 4 cents // activate change dispenser this.giveChange() ;