Write a full java program to solve the following problem: Read in a sum of money
ID: 3549239 • Letter: W
Question
Write a full java program to solve the following problem:
Read in a sum of money and determine the number of quarters, dimes, nickels, and pennies represented in the cents portion. You must get an optimal mix; all pennies, for example, is not allowed.
Example
With input of 43.59 you should come up with 2 quarters, 1 nickel, 4 pennies. Variable cash is a double; the rest are integers
Before you start coding, write the algorithm first. Include this algorithm as comments in your code. Use at least one static method (e.g., call your method Coins) to implement the solution, and call it from main.
Test your program thoroughly, for example, try different amounts of dollars and cents: 43.50, 23.51, 43.53,
Explanation / Answer
public class coin_changer
{
public static void main(String[] args)
{
Coins(15.69);
Coins(43.50);
Coins(23.51);
Coins(43.53);
Coins(13.59);
}
public static void Coins(double cash)
{
System.out.print("In " + cash + ", There are ");
// first get last two digits by multiplying with 100 and then take reminder with 100;
int local_cash = (int)(cash*100)%100;
// get quarter by dividing with 25;
int quarters = (local_cash / 25);
// subtract quarters from cash
local_cash = local_cash - quarters*25;
// get dimes by dividing with 10;
int dimes = local_cash/10;
// subtract dimes from cash
local_cash = local_cash - dimes*10;
// get nickels by dividing with 5;
int nickels = local_cash/5;
// now whatever left is pennies.
int pennies = local_cash - nickels*5;
// now start printing all.
System.out.print(quarters);
System.out.print(" quarters, ");
System.out.print(dimes);
System.out.print(" dimes, ");
System.out.print(nickels);
System.out.print(" nickels, and ");
System.out.print(pennies);
System.out.print(" pennies ");
System.out.print("in your cash.");
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.