Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Why can't I subract coins?

John Walker
Greenhorn
Posts: 9
I'm trying to get my program to take coins out of the purse, but it keeps adding them:
public class Purse
{
private int pennies;
private int nickels;
private int dimes;
private int quarters;
public void purse ()
{
pennies = 0;
nickels = 0;
dimes = 0;
quarters = 0;
}
{
pennies = pennies + count;
}
{
nickels = nickels + count;
}
{
dimes = dimes + count;
}
{
quarters = quarters + count;
}
public void delPennies (int count)
{
pennies = count - pennies;
}
public void delNickels (int count)
{
nickels = count - nickels;
}
public void delDimes (int count)
{
dimes = count - dimes;
}
public void delQuarters (int count)
{
quarters = count - quarters;
}
public double calculateTotal ()
{
return (pennies*.01)+(nickels*.05)+(dimes*.10)+(quarters*.25);
}
}
import javax.swing.JOptionPane;
public class InputTest3 {

public static void main (String[]args) {
Purse myPurse = new Purse ();

String input = JOptionPane.showInputDialog("How many pennies do you have?");
int count = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many nickels do you have?");
count = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many dimes do you have?");
count = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many quarters do you have?");
count = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many pennies do you want to take out?");
count = Integer.parseInt(input);
myPurse.delPennies(count);
input = JOptionPane.showInputDialog("How many nickels do you want to take out?");
count = Integer.parseInt(input);
myPurse.delNickels(count);
input = JOptionPane.showInputDialog("How many dimes do you want to take out?");
count = Integer.parseInt(input);
myPurse.delDimes(count);
input = JOptionPane.showInputDialog("How many quarters do you want to take out?");
count = Integer.parseInt(input);
myPurse.delQuarters(count);
double totalValue = myPurse.calculateTotal ();
System.out.println("The total is " + totalValue);

}
}
Any ideas? I'm pretty stuck here.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35

public void delPennies (int count) {
pennies = count - pennies;
}

Don't you mean "pennies = pennies - count;" ?

John Walker
Greenhorn
Posts: 9
I tried that but it still kept adding all of them together.

Ranch Hand
Posts: 73
Should your constructor really have a return type? If you do the subtraction like you do now ( pennies = count - pennies ; ) It will probably become negative, unless you try to subtract more coins than you have. It would be correct if you wrote
pennies = pennies - count; or
pennies -= count;
[ October 30, 2003: Message edited by: Carl Pettersson ]

Anonymous
Ranch Hand
Posts: 18944
Originally posted by John Walker:
I'm trying to get my program to take coins out of the purse, but it keeps adding them:
public class Purse
{
private int pennies;
private int nickels;
private int dimes;
private int quarters;
public void purse ()
{
pennies = 0;
nickels = 0;
dimes = 0;
quarters = 0;
}

The error is in your constructor. it must be

Java is case sensitive and the constructor must have the same name than
the class. Additionally a constructor has no return type. If you don't
initialize your variables (what is not done cause default constructor is used) you have no idea with what value the variabales are filled..

Wayne L Johnson
Ranch Hand
Posts: 399
Oliver is correct with what he says about the Constructor (case-sensitive, no return type). However Java guarantees that all instance variables--static or not--will be initialized to a "reasonable" value. Every object type will be initialized to "null", booleans will be "false", and all numeric primitives will be "zero" (0, 0L, 0.0 or 0.0F, as appropriate).
So while it can't hurt to explicitly initialize everything, that won't cause the problem you're seeing. All of your instance varialbes (pennies, nickels, dimes and quarters) will be zero. [NOTE: this is not the case with local variables: they have to be explicitly set before using.]
Ernest identified the real problem. I made the changes he outlined [to all four "delXXX()" methods] and the program worked fine.