
Andy C. answered 10/02/17
Tutor
4.9
(27)
Math/Physics Tutor
import java.util.*; // for input scanner
class DollarNchange
{
private int amount; //stores the input amount in pennies
private int changeAmt; //the change in pennies from one dollar
DollarNchange() //constructor
{
amount = changeAmt = 0;
}
//inputs a valid amount: an integer multiple of 5 between 25 and 100
private void Input()
{
Scanner scanner = new Scanner(System.in);
int iReturn = 0;
amount=0;
boolean done_flag = false;
while (done_flag==false)
{
// input occurs here !!!!!
System.out.println(" -------------------------------------------");
System.out.println(" Please input the price of the item of ");
System.out.println(" At least 25 cents but no more than ");
System.out.println(" one dollar in multiples of 5. EX. ");
System.out.println(" That is: 25,30,35,40,...,85,90,95, 0r 100 ");
System.out.println("--------------------------------------------");
System.out.print(" -- :> ");
amount = scanner.nextInt();
//stops when a multiple of 5 between 25 and 100 inclusive is input per the prompt
done_flag = (((amount %5)==0 ) && ( amount >20) && (amount<105));
} //while
} //Input
// calculates the change due, displays the results, and counts the change back to the customer
private void OutputChange()
{
System.out.println(" You bought an item which costs "+ amount + " cents");
System.out.println(" Your change from one dollar is " + changeAmt + " cents ");
System.out.println(" That is : ");
System.out.println(changeAmt/25 + " quarters "); // # of quarters is change amt divided by 25
changeAmt %= 25; // takes the remainder
System.out.println(changeAmt/10 + " dimes "); // divides the remainder by 10 to get # of dimes
changeAmt %= 10; // gets the remainder of that
System.out.println(changeAmt/5 + " nickles "); // change amount should be either 0 or 5
// No pennies because all amounts are multiples of 5
}
public void Go() //driver function
{
Input();
changeAmt = 100 - amount;
OutputChange();
}
public static void main (String args[])
{
DollarNchange X = new DollarNchange();
X.Go();
}
} //class DollarNchange
class DollarNchange
{
private int amount; //stores the input amount in pennies
private int changeAmt; //the change in pennies from one dollar
DollarNchange() //constructor
{
amount = changeAmt = 0;
}
//inputs a valid amount: an integer multiple of 5 between 25 and 100
private void Input()
{
Scanner scanner = new Scanner(System.in);
int iReturn = 0;
amount=0;
boolean done_flag = false;
while (done_flag==false)
{
// input occurs here !!!!!
System.out.println(" -------------------------------------------");
System.out.println(" Please input the price of the item of ");
System.out.println(" At least 25 cents but no more than ");
System.out.println(" one dollar in multiples of 5. EX. ");
System.out.println(" That is: 25,30,35,40,...,85,90,95, 0r 100 ");
System.out.println("--------------------------------------------");
System.out.print(" -- :> ");
amount = scanner.nextInt();
//stops when a multiple of 5 between 25 and 100 inclusive is input per the prompt
done_flag = (((amount %5)==0 ) && ( amount >20) && (amount<105));
} //while
} //Input
// calculates the change due, displays the results, and counts the change back to the customer
private void OutputChange()
{
System.out.println(" You bought an item which costs "+ amount + " cents");
System.out.println(" Your change from one dollar is " + changeAmt + " cents ");
System.out.println(" That is : ");
System.out.println(changeAmt/25 + " quarters "); // # of quarters is change amt divided by 25
changeAmt %= 25; // takes the remainder
System.out.println(changeAmt/10 + " dimes "); // divides the remainder by 10 to get # of dimes
changeAmt %= 10; // gets the remainder of that
System.out.println(changeAmt/5 + " nickles "); // change amount should be either 0 or 5
// No pennies because all amounts are multiples of 5
}
public void Go() //driver function
{
Input();
changeAmt = 100 - amount;
OutputChange();
}
public static void main (String args[])
{
DollarNchange X = new DollarNchange();
X.Go();
}
} //class DollarNchange