Question
I have this code, but I need it to convert the coin name to singular if there is only one of it present (example: you
I have this code, but I need it to convert the coin name to singular if there is only one of it present (example: you have 5 quarters, 1 dime, 2 nickels and 1 penny."
package edu.wit.cs.comp1050;
/** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c { /** * Error message to display for negative amount */ public static final String ERR_MSG = "Dollar amount must be non-negative!"; public static int[] compute_coins(int coin_value ,int num, int amount_left){ int[] arr=new int[2]; if(!(coin_value> 0 && coin_value<100)){ System.out.println("Coin value is invalid: "); return arr; } else{ num = amount_left/coin_value; amount_left = amount_left%coin_value; arr[0]=num; arr[1]=amount_left; } return arr; } /** * Method to convert a double to * an integer * * @param num number to convert * @return converted value */ public static int convertToInt(double num) { return (int) Math.round(num); }
/** * Starts the program * * @param args command-lines, ignored */ public static void main(String[] args) { int amount_left; int num, quarters, dimes, nickels, pennies; char c; double price; double money; num = 0; Scanner sc=new Scanner(System.in); System.out.print("Enter total amount: $"); money=sc.nextDouble(); if(money<0) System.out.println(ERR_MSG); else { money=money*100.0; double diff = money; amount_left = convertToInt(diff); int[] arr=new int[2]; arr=compute_coins(25,num,amount_left); num=arr[0]; amount_left=arr[1];
System.out.print("You have ");
System.out.print(num+" quarters, ");
if((amount_left>= 0)){
arr=compute_coins(10,num,amount_left);
num=arr[0];
amount_left=arr[1];
System.out.print(num+" dimes, ");
}else{
System.out.print("0 dimes, ");
}
if((amount_left>= 0)){
arr=compute_coins(5,num,amount_left);
num=arr[0];
amount_left=arr[1];
System.out.print(num+" nickels, and ");
}else{
System.out.print("0 nickels, and ");
}
if((amount_left>= 0)){
arr=compute_coins(1,num,amount_left);
num=arr[0];
amount_left=arr[1];
System.out.println(num+" pennies.");
}else{
System.out.println("0 pennies.");
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started