This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why can't I subract coins? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why can Watch "Why can New topic
Author

Why can't I subract coins?

John Walker
Greenhorn

Joined: Oct 28, 2003
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;
}
public void addPennies (int count)
{
pennies = pennies + count;
}
public void addNickels (int count)
{
nickels = nickels + count;
}
public void addDimes (int count)
{
dimes = dimes + count;
}
public void addQuarters (int 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);
myPurse.addPennies(count);
input = JOptionPane.showInputDialog("How many nickels do you have?");
count = Integer.parseInt(input);
myPurse.addNickels(count);
input = JOptionPane.showInputDialog("How many dimes do you have?");
count = Integer.parseInt(input);
myPurse.addDimes(count);
input = JOptionPane.showInputDialog("How many quarters do you have?");
count = Integer.parseInt(input);
myPurse.addQuarters(count);
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

Joined: Jul 08, 2003
Posts: 24183
    
  34


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

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


[Jess in Action][AskingGoodQuestions]
John Walker
Greenhorn

Joined: Oct 28, 2003
Posts: 9
I tried that but it still kept adding all of them together.
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
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

Joined: Nov 22, 2008
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

Joined: Sep 03, 2003
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why can't I subract coins?
 
Similar Threads
Vending Machine Project
Calculating Change
Vending Machine.
Vending Machine Project
Help with these errors