Create an AddCoins application that prompts the user for the number of pennies,
ID: 3543726 • Letter: C
Question
Create an AddCoins application that prompts the user for the number of pennies, nickels, dimes, and quarters the then displays their total dollar amount. The AddCoins application should include a getDollarAmount() method that has four int parameters corresponding to the number of pennies, nickels, dimes, and quarters, and will return a String that corresponds to the dollar value of the coins. Note that the string returned should include the currency sign.
Example:
Enter your total coins:
Quarters: 3
Dimes: 2
Nickels: 1
Pennies: 8
Total: $ 1.08
Explanation / Answer
Sample Output
Input number of coins
Penny: 7
Nickel: 10
Dime: 10
Quarter: 10
Total Value of coins: $4.07
Code
import java.util.Scanner;
public class AddCoins {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input number of coins");
System.out.print("Penny: ");
int numberOfPenny = scanner.nextInt();
System.out.print("Nickel: ");
int numberOfNickel = scanner.nextInt();
System.out.print("Dime: ");
int numberOfDime = scanner.nextInt();
System.out.print("Quarter: ");
int numberOfQuarters = scanner.nextInt();
AddCoins coin = new AddCoins();
double balance = coin.getDollarAmount(numberOfPenny, numberOfNickel, numberOfDime, numberOfQuarters);
System.out.print("Total Value of coins: $"+balance);
}
public double getDollarAmount (int penny, int nickel, int dime, int quarter){
double balance = ( 0.01 * penny ) + (0.05 * nickel)
+ (0.10 * dime)+ (0.25 * quarter);
return balance;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.