#ChangeCalc Problem on methods homework

1 messages · Page 1 of 1 (latest)

leaden frost
#

The attached image is the question and what I must accomplish.

This is what I have so far:


public class ChangeCalc{
  public static void main(String[] args){
  
   Scanner sc = new Scanner(System.in);
   
   int change = 0;
   int quarters = 25;
   int dimes = 10;
   int nickels = 5;
 
 
   System.out.println("How much change (in cents)?: ");
   change = sc.nextInt();
   
   int result1 = 0;
   int result2 = 0;
   int result3 = 0;
   
   if( change < 100 ) {
   
     System.out.println("Quarters = " + Quarters( change, result1, quarters));
     
     System.out.println("Dimes = " + Dimes( change, result1, result2, dimes));
     
     System.out.println("Nickels = " + Nickels(change, result2, result3, nickels));
   
     System.out.println("Pennies = " + ( change - (result1 + result2 + result3)));
   
   }

 
  
  }
  
   public static int Quarters( int change, int result1, int quarters ) {

     result1 = change - (change % quarters);
     return result1;

   }

   public static int Dimes( int change, int result1, int result2, int dimes){
     
     result2 = change - (result1 % dimes);
     return result2;
   }
   
   public static int Nickels( int change,int result2, int result3, int nickels){
     
     result3 = change - (result2 % nickels);
     return result3;
   }
   



}```

Can anyone please tell me what I am doing wrong and what I should change or fix?
spare kelpBOT
# leaden frost The attached image is the question and what I must accomplish. This is what I h...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

public class ChangeCalc {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int change = 0;
    int quarters = 25;
    int dimes = 10;
    int nickels = 5;
    System.out.println("How much change (in cents)?: ");
    change = sc.nextInt();
    int result1 = 0;
    int result2 = 0;
    int result3 = 0;
    if (change < 100) {
      System.out.println("Quarters = " + Quarters(change, result1, quarters));
      System.out.println("Dimes = " + Dimes(change, result1, result2, dimes));
      System.out.println("Nickels = " + Nickels(change, result2, result3, nickels));
      System.out.println("Pennies = " + (change - (result1 + result2 + result3)));
    }
  }
  public static int Quarters(int change, int result1, int quarters) {
    result1 = change - (change % quarters);
    return result1;
  }
  public static int Dimes(int change, int result1, int result2, int dimes) {
    result2 = change - (result1 % dimes);
    return result2;
  }
  public static int Nickels(int change, int result2, int result3, int nickels) {
    result3 = change - (result2 % nickels);
    return result3;
  }
}
#

<@&987246399047479336> please have a look, thanks.

worthy monolith
#

The int method parameter (result) is call-by-value, not call-by-reference. It means the int you send into the method gets copied. You cannot change the original result, you have to return it and store it into a result variable.

#

I did a fast check, so i dont know if the rest of the solution is correct.

leaden frost
#

hmm

worthy monolith
leaden frost
#

It seems like the code is okay but some things are messed up because the output is not what the problem is asking me to do

#

How much change (in cents)?:

Quarters = 25
Dimes = 45
Nickels = 45
Pennies = 45

#

this is definitely not right

#

I entered 45

worthy monolith
#

Yes, result1, result2, result3 is always 0

#

Hence what I wrote above..

leaden frost
#

hm...

#

perhaps instead of modulus, I actually need to divide it

worthy monolith
#

There are more mistakes in your calculations

#

Remember that each calculated result is NOT in cents.

leaden frost
#

alright

#

ohh thank you

#

yeah, I think I'm starting to get it

worthy monolith
#

cool

#

A tip, use debug and step through the code, when something is not as expected.

leaden frost
#

alright

leaden frost
#

I just realized that I could've been using way more efficient IDEs than Dr Java

worthy monolith
#

Didn't know about Dr. Java.
Go with intellij IDE.

#

Says Dr. Java is designed for students. Don't get the point to be honest. It's better to run a IDE used in the real world.

#

Debug is powerful and helpful, you learn more by understanding each sequence in the code, especially when you do not understand why it does not behave as intended.

Hope you solved your bugs.

leaden frost
#

Alright, thanks for the help