1

I've created a working change calculator that works off of a Driver class "CoinsCalculateTester" calling a calculate and output method from another aptly named "CoinsCalculate". Here are the 2 classes.

CoinsCalculateTester class

import chn.util.*;

public class CoinsCalculateTester
{
    public static void main(String[] args)
    {
        int change;
        ConsoleIO keyboard = new ConsoleIO();
        System.out.print("Please enter amount of change => $0.");
        change = keyboard.readInt();
        CoinsCalculate printOut = new CoinsCalculate(change);
        printOut.calculate();
        printOut.printChange();
    }
}

CoinsCalculate class

public class CoinsCalculate
{

//Instance variables
private int change;
public int q, d, n, p; //Quarters, Dimes, Nickels, and Pennies, Respectively.
public int c; //Declaring var c for change in constructor

//Constructor
public CoinsCalculate (int change)
{
    c = change;
}

public void calculate()
{
    change = change * 100;

    int q = c / 25;
    c = c % 25;

    int d = c / 10;
    c = c % 10;

    int n = c / 5;
    c = c % 5;

    int p = c / 1;
    c = c % 1;



}

public void printChange()
{
    System.out.println("Quarter(s): " +q);
    System.out.println("Nickel(s): " +d);
    System.out.println("Dime(s): " +n);
    System.out.println("Penny(s): " +p);
}
}

And when running the Tester this is the output:

Please enter amount of change => $0.84
Quarter(s): 0
Nickel(s): 0
Dime(s): 0
Penny(s): 0

The problem I believe I'm having is that once the calculate method is done the variables go back to their initial values so when returning them in the printChange method I get 0 for all of them. I know that I could put the SOP lines in one method and avoid all this trouble but I would like them to be in separate methods. So my question is, how can I do that?

2 Answers 2

3

Do not redefine those variables inside the calculate method, otherwise they hide those class level ones with the same name:

q = c / 25;
c = c % 25;

d = c / 10;
c = c % 10;

n = c / 5;
c = c % 5;

p = c / 1;
c = c % 1;
Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow, thank you very much. I figured it was something simple like this. Now when I redefine these variables in the calculate method are new variables created with the same name just inside that method? Or are they the same global variables just only being changed inside the method.
Yes. New variables are created that only exist while the method is being executed. When the method returns, the variables are nuked. Also, your method does not return anything. It does stuff, but it does NOT return anything.
0

When you declare a variable inside a method, its scope is limited to that method.

Your code declares local variables inside the calculate() method that do NOT exist once the method returns.

These variables have the same names as your public class variables, which are never used (except c).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.