Question

I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

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.");

       }
}
       }
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.
}
}
}
}
}

Syacer.out.prsnEntez oclwoa:: ny 100. else if cL 1)

Output:

eP 1 Output N> Debugger Console X PA1c (run) X 宅-Enter total amount : 추156 You have 624 quarters, 0 dimes, 0 nickels, and 0 pennies BUILD SUCCESSFUL (total time 8 seconds) 5 rO

Add a comment
Know the answer?
Add Answer to:
I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this code for python: num = int(input()) if num == 0: print('No change') else:...

    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...

    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...

    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....

    /** 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 {   ...

    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...

    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 /...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    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();...

  • I need to write a program in java that reads a text file with a list...

    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); } //...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT