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?