Basic Java Vending Machine Program -


i making vending machine program , i'm not sure whats wrong

vendingmachine.java:

public class vendingmachine {

int cancount; int tokencount;  public vendingmachine() {     cancount = 0;     tokencount = 0; } public vendingmachine(int initialcans, int initialtokens) {     cancount = initialcans;     tokencount = initialtokens; } public void fillup(int cans)      {     cancount += cans;        } public void inserttoken() {     tokencount -= tokencount;     cancount -= cancount; } public double getcancount() {     return cancount; } public double gettokencount() {     return tokencount; } 

}

vendingmachinetester.java:

public class vendingmachinetester {     public static void main(string[] args)     {         vendingmachine machine = new vendingmachine(5, 5);         machine.fillup(10); // fill ten cans         machine.inserttoken();         machine.inserttoken();         system.out.print("token count: ");         system.out.println(machine.gettokencount());         system.out.println("expected: 7");         system.out.print("can count: ");         system.out.println(machine.getcancount());         system.out.println("expected: 13");     } } 

when run it gives me this:

token count: 0.0 expected: 7 can count: 0.0 expected: 13 

i wondering why giving me zeros , not expected values. in advance.

public void fillup(int cans)    {     cancount += cancount;    } 

since cancount starts 0, adding 0 0 method, , 0 + 0 equal 0. instead add parameter cancount: cancount += cans;

in future though, it's time learn debug. ide's have debugging capability can watch state of variables program runs -- try these out , way isolate error.

note cancount should int, not double, since ints more accurate, , unlikely you'll need deal fractional number of cans.

edit, inserttoken method 0 , cancounts -- @ logic see why. you're subtracting cancount cancount. if cancount == 10, after method 10 - 10 equal 0.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -