Your friend from Estonia is visiting you and finds American money confusing. You
ID: 641367 • Letter: Y
Question
Your friend from Estonia is visiting you and finds American money confusing. You want to write an application that will help your friend out, and ensure they don't get cheated. Write a class that encapsulates the concept of "money", assuming that money will have the following attributes: a number of $20 dollar bills, a number of $10 dollar bills, a number of $5 dollar bills, a number of $1 dollar bills, a number of quarters, a number of dimes, a number of nickels, and a number of pennies. Include a constructor, the accessors and mutators, a methods toString and equals. Also code the following methods: one printing the total amount of money in dollar notation with two significant digits after the decimal point, and another returning the money of just the dollar bills, and another returning the money of just the change. Then you will write an application that takes the total cost of an item (including tax) and return (a) whether or not you have enough money to pay for the item, (b) whether or not you have the exact change to pay for the item, (c) if you do have the exact change, indicate exactly what money should be given to the clerk (how many dollar bills, quarters, dimes, etc.), (d) if you do not have the exact change, indicate what money should be given to the clerk for the next dollar amount available (i.e., you won't give the clerk any change unless you have to), and (e) what change should be expected and what is the minimum number and type of coins to be returned. Your application should loop until the user indicates they wish to exit the application.
Explanation / Answer
import java.util.Scanner;
/*******************************************************************************
* Compute Change
*******************************************************************************/
public class ComputeChange
{
public static void main(String[] args)
{
/*****************************************************************************
* Properties
*****************************************************************************/
Scanner input = new Scanner(System.in);
int dollars;
int quarters;
int dimes;
int nickels;
int pennies;
double amount;
/*---------------------------------------------------------*/
/* Get the amount and break it into the number of pennies. */
/* Return the least number of each denomination to return. */
/*---------------------------------------------------------*/
System.out.print("Enter an amount, for example, 11.56: ");
amount = input.nextDouble();
input.close();
/*---------*/
/* Pennies */
/*---------*/
pennies = (int)(amount * 100);
/*---------*/
/* Dollars */
/*---------*/
dollars = pennies / 100;
pennies = pennies % 100;
/*----------*/
/* Quarters */
/*----------*/
quarters = pennies / 25;
pennies = pennies % 25;
/*-------*/
/* Dimes */
/*-------*/
dimes = pennies / 10;
pennies = pennies % 10;
/*---------*/
/* Nickels */
/*---------*/
nickels = pennies / 5;
pennies = pennies % 5;
/*--------------------*/
/* Print out results. */
/*--------------------*/
System.out.println(String.format("Your amount %.2f consists of " +
" %d dollars " +
" %d quarters " +
" %d dimes " +
" %d nickels " +
" %d pennies", amount, dollars, quarters, dimes, nickels, pennies));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.