Forums Register Login

Vending Machine Program

+Pie Number of slices to send: Send
Hey first post of the forums, sorry if I break any rules!
Basically I have to create a program that will store values for 'Cans' and 'Tokens'. Simple vending machine like program, where the user inserts 2 tokens and gets 1 can.
I've only been doing java for about 3 weeks, and still trying to get my head around constructors, objects and classes etc. I'm not sure exactly how to go about writing the constructor.
I know it makes an instance of the class, but how should I write this in the code?
Thanks!

+Pie Number of slices to send: Send
Constructors can be very simple. If you were to create a real live physicial vending machine, what would it's initial state be? How many cans and how many tokens would it have in it?

That's what a constructor should do - set those value to what makes sense for the initial creation.
+Pie Number of slices to send: Send
Welcome to the Ranch

To continue from what Fred said: you have a number of fields in your class, and the constructor must (well, at least in my opinion) set them all to sensible values. That is called establishing the class invariants.
Your method with the number of cans will almost cretahinly not do what you want it to.
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2257 times.
Similar Threads
I am supposed to call two service classes from one client(main) class?
I am supposed to call two service classes from one client(main) class?
Create a counter that increments once a second until a given number of seconds is reached
Tokenising using String.split().
Craps game
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:32:57.