Starting from:

$15

# Object Lab - Vending Machine


Lab 3 –Creating Objects

3.1. In this lab, you will implement a vending machine that holds cans of soda. To buy a can of soda, the customer needs to insert a token into the machine. When the token is inserted, a can drops from the can reservoir into the product delivery slot. The vending machine can be filled with more cans. The goal is to determine how many cans and tokens are in the machine at any given time.

Consider what methods would you supply for a VendingMachine class. Methods are verbs.

We need to be able to add cans to the vending machine, and to buy them from it (by inserting a token). These are all verbs. It would also be useful to get the number of cans and tokens that the machine contains.

3.2. Now translate those informal descriptions into Java method signatures, such as

```java
public void fillUp(int cans)
```

Give the names, parameters, and return types of the methods. Do not implement them yet.

```java
public void fillUp(int cans){. . .}

public void insertToken(){. . .}

public int getCanCount(){. . .}

public int getTokenCount(){. . .}
```

Because we now know what our vending machine can do, we can construct the following tester program so that it exercises all of the methods of your class.

```java
public class VendingMachineTester
{
public static void main(String[] args)
{
VendingMachine machine = new VendingMachine();
machine.fillUp(10); // Fill up with ten cans
machine.insertToken();
machine.insertToken();
System.out.print("Token count: ");
System.out.println(machine.getTokenCount());
System.out.println("Expected: 2");
System.out.print("Can count: ");
System.out.println(machine.getCanCount());
System.out.println("Expected: 8");
}
}
```

3.3. What instance variables do the methods need to do their work? Hint: You need to track the number of cans and tokens. Declare them in your VendingMachine class with their type and access modifier.

```java
private int numCans;
private int tokens;
```

3.4. Consider what happens when a user inserts a token into the vending machine. The number of tokens is increased, and the number of cans is decreased. Implement a method:

```java
public void insertToken()
{
// Instructions for updating the token and can counts
}
```

You need to use the instance variables that you defined in the previous step.

Do not worry about the case where there are no more cans in the vending machine. You will learn how to program a decision "if can count is 0" later in this course. For now, assume that the insertToken method will not be called if the vending machine is empty. This method must reduce the number of cans by one and increase the number of tokens by one.

3.5. Now implement a method fillUp(int cans) to add more cans to the machine. Simply add the number of new cans to the can count.

3.6. Next, implement two accessor methods, getCanCount and getTokenCount, that return the current values of the can and token counts.

3.7. You have implemented all methods of the VendingMachine class.

Execute the main method in theVendingMachineTester class to verify your results.

3.8. So far, theVendingMachine class does not have any constructors. Instances of a class with no constructor are always constructed with all instance variables set to zero (or null if they are object references). It is always a good idea to provide an explicit constructor.

Provide two constructors for the VendingMachine class:

1) A default constructor that initializes the vending machine with 10 soda cans
2) A constructor, VendingMachine(int cans), that initializes the vending machine with the given number of cans

Both constructors should initialize the token count to 0.

More products