• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why can't I subract coins?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


Don't you mean "pennies = pennies - count;" ?
 
John Walker
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that but it still kept adding all of them together.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic