# We need to change the Dispenser.java program, so the 2 issues do not come up. 2 issues are…

We need to change the Dispenser.java program, so the 2 issues do not come up.

2 issues are ->Coint counting

->computation of the total

Dispeser is the ONLY program that needs to be changed.

when you run the 3 programs, and ask for \$2.08, but instead you get 2.07999 , how would you fix these issues..

I dont know how els to ask.

-For each denomination, what is the maximum number of coins that this machine may dispense? Explain why.

//the odd pennies are incorrect

//Dispenser class

public class Dispenser {

public Teller.Result dispense(double request) {

int dollars = (int)request;

double remainder1 = request – dollars;

int quarters = (int)(remainder1 / 0.25);

double remainder2 = remainder1 – quarters * 0.25;

int dimes = (int)(remainder2 / 0.10);

double remainder3 = remainder2 – dimes * 0.1;

int nickels = (int)(remainder3 / 0.05);

double remainder4 = remainder3 – nickels * 0.05;

int pennies = (int)(remainder4 / 0.01);

double remainder5 = remainder4 – pennies * 0.01;

int coins[] = {dollars, quarters, dimes, nickels, pennies};

double total = coins[0] * 1.0 + coins[1] * 0.25 + coins[2] * 0.1 + coins[3] * 0.05 + coins[4] * 0.01;

return new Teller.Result(coins, total);

}

}

————————————————————————————————————————————————————————–

//Teller

import java.math.RoundingMode;

import java.text.DecimalFormat;

import java.util.Scanner;

public class Teller {

String inputs = null;

private Teller() {}

public Teller(String args[]) {

String data = "";

for (String s : args) {

data += s + " ";

}

if (data.trim().length()>0) inputs = data.trim();

}

double getInput(Scanner in) {

if (inputs==null)

System.out.print("Enter an amount (truncated to 2 decimal places): ");

else if (!in.hasNext())

return 0;

double value = in.nextDouble();

DecimalFormat df = new DecimalFormat(".##");

df.setRoundingMode(RoundingMode.DOWN);

double request = Double.parseDouble(df.format(value));

System.out.println("You have requested \$" + request);

return request;

}

void report(int coins[], double actual, double request) {

// Report results.

System.out.print("Dispensing:");

System.out.print(" " + coins[0] + " dollars");

System.out.print(" " + coins[1] + " quarters");

System.out.print(" " + coins[2] + " dimes");

System.out.print(" " + coins[3] + " nickels");

System.out.print(" " + coins[4] + " pennies");

System.out.println("nTotal dispensed = \$" + actual);

if (actual==request)

System.out.println("Correct.");

else

System.out.println("Incorrect.");

}

public void operate() {

Scanner in;

if (inputs!=null)

in = new Scanner(inputs);

else

in = new Scanner(System.in);

Dispenser dispenser = new Dispenser();

double request = getInput(in);

while (request>0.0) {

Result result = dispenser.dispense(request);

report(result.coins, result.total, request);

request = getInput(in);

}

}

public static class Result {

int coins[];

double total;

private Result() {}

public Result(int coins[], double total) {

this.coins = coins;

this.total = total;

}

}

public static void main(String[] args) {

Teller teller = new Teller(args);

teller.operate();

}

}

————————————————————————————————————————————————————————–

//tester

import java.text.DecimalFormat;

public class Test {

static void test1() {

int count = 0;

double start = 0.01;

double end = 100.00;

double interval = 0.01;

count = (int) (Math.ceil((end-start)/interval)) + 1;

DecimalFormat df = new DecimalFormat(".##");

String requests[] = new String[count];

for (int i=0; i

requests[i] = df.format(start + i*interval);

}

Teller teller = new Teller(requests);

teller.operate();

}

static void test2() {

int count = 100;

double start = Integer.MAX_VALUE;

double end = Integer.MAX_VALUE * (1+0.25+0.1+0.05+0.01);

double interval = (end-start)/(count-1);

DecimalFormat df = new DecimalFormat(".##");

String requests[] = new String[count];

for (int i=0; i

requests[i] = df.format(start + i*interval);

}

Teller teller = new Teller(requests);

teller.operate();

}

public static void main(String[] args) {

if (args.length==1 && args[0].equals("1"))

test1();

else if (args.length==1 && args[0].equals("2"))

test2();

else { // default: run all.

test1();

test2();

}

}

}