Java Bootcamp – Workbooks and Challenges
)}

Vending Machine – Part 1

From Java Bootcamp Resources -> Module 2 -> 7. The World of Objects, open the folder: vending-machine.

Your main() method should contain a welcome message and a commented array.

The requirements.

Based on the requirements, we can classify two types of objects:

We can describe each item using three fields: name, price and quantity.

The vending machine is identified by the items that it manages. It also performs the action of dispensing an item.

The tasks in this workbook apply to the Item class.

Task 1 – Fields


Add the necessary fields to the Item class. Protect each field using the private keyword.

Task 2 – The Big 3


If a class has fields, you need to apply the Big 3.

So, add a constructor, getters, and setters.

Task 3 – Copy Constructor


To avoid the reference trap, we need a way to copy Item objects.

So, add a copy constructor.

Task 4 – toString


Every class that models an object should have a toString method.

Add a toString method to your class, and return a String that adheres to this format:

<name>: <price> (<quantity>)

Task 5 – Test your code.


  • Inside main(), create a new object of the Item class. Feel free to pass random values to the constructor.

  • Then, print the object.

Example Output:

Good Luck!


Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.