I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank.
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
");
if (num == 1) {
System.out.print(num+" quarter, ");
}
else if (num > 1) {
System.out.print(num+" quarters,
");
}else{
System.out.print("0 quarters, ");
}
if((amount_left>= 0)){
arr=compute_coins(10,num,amount_left);
num=arr[0];
amount_left=arr[1];
if (num == 1) {
System.out.print(num+" dime,
");
}
else if (num > 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];
if (num == 1) {
System.out.print(num+" nickel, and ");
}
else if (num > 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];
if (num == 1) {
System.out.println(num+" penny.");
}
else if (num > 1)
System.out.println(num+"
pennies.");
}else{
System.out.println("0 pennies.");
}
}
}
}
}
}
The only thing to change is the closing curly brace of the last pennies block calculation (I have highlighted that brace in the code below). You probably had put it in the wrong place. Here's the right code, see the image of the code as well :
package edu.wit.cs.comp1050;
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 ");
if (num == 1)
{
System.out.print(num+" quarter, ");
}
else if (num > 1)
{
System.out.print(num+" quarters, ");
}
else
{
System.out.print("0 quarters, ");
}
if((amount_left>= 0))
{
arr=compute_coins(10,num,amount_left);
num=arr[0];
amount_left=arr[1];
if (num == 1)
{
System.out.print(num+" dime, ");
}
else if (num > 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];
if (num == 1)
{
System.out.print(num+" nickel, and ");
}
else if (num > 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];
if (num == 1)
{
System.out.println(num+" penny.");
}
else if (num > 1)
{
System.out.println(num+" pennies.");
}
else
{
System.out.println("0 pennies.");
}
} // this is that closing brace's right
position.
}
}
}
}
}

Output:

I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...
I have this code for python: num = int(input()) if num == 0: print('No change') else: dol = num // 100 num %= 100 Quarters = num // 25 num %= 25 Dimes = num // 10 num %= 10 Nickels = num // 5 num %= 5 Pennies = num if dol > 0: if dol == 1: print('1 Dollar') if Dollars > 1: print(dol,'Dollars') if Quarters > 0: if Quarters == 1: print('1 Quarter') if Quarters > 1:...
Write a program called "ConvertPennies" to input an integer value representing a number of pennies and convert it to its equivalent number of Dollars, Quarters, Dimes, Nickels and Pennies. Following is the result of the execution of this program for 292 pennies. Enter the amount of pennies to convert: 292 pennies is equal to 2 one dollar bills 3 quarters 1 dimes 1 nickels 2 pennies Have a nice day! This is given: //********************************************************************** // Programmer: Your first and last...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
/** this is my code, and I want to invoke the method,
doubleArraySize() in main method.
and I need to display some result in cmd or terminal.
also, I have classes such as Average and RandomN needed for
piping(pipe).
please help me and thank you.
**/
import java.util.Scanner;
public class StdDev {
static double[] arr;
static int N;
public static void main(String[] args) {
if(args.length==0){
arr= new double[N];
Scanner input = new Scanner(System.in);
int i=0;
while(input.hasNextDouble()){
arr[i] =
i++;
}...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
I cannot seem to find a single answer to this question. The output of this code is You entered: 236.0 89.5 142.0 166.3 93.0 with a trailing space at the end of the number 93.0. These inputs are changed and always have a trailing space on the last number. Now, I know it's because I have System.out.print(m[i] + " "); with the " " creating the space in between each number. How do I write it so each number has...
Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt); third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console { private Scanner sc; boolean isValid; int i; double d; public Console() { sc = new Scanner(System.in); } public String getString(String prompt) { System.out.print(prompt); return sc.nextLine();...
HELP ( i get these comment for this assignment please help me to fixed Please do not ask user to enter cents. Amount due and received should be in dollars such as 2.35 and 5.) Business P2.8Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. #include <iostream>...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...