Homework 1

Learning Objectives

In this homework, you will apply the concepts you learned in class to reinforce your understanding of:

Homework Policy

This is an indiviual homework assignment. Please review Academic Integrity section of the class syllabus for rules on individual homework assignments.

Description

Implement a program that simulates a simple vending machine. A vending machine is a machine that sells snacks/drinks: you insert money into the money slot, select an item you want to purchase, and if the amount of money you inserted is sufficient to pay for your selected item, your selected item is dropped into a dispenser compartment. If you supplied more than enough money, change is provided. Some vending machine accept credit cards. Our simulation will simply model a vending machine and will not deal with any real money. An example vending machine is shown below:


Getting started

In your CSCI 2300 git repositories, you will find a hw1 directory. Place all the code you write for this homework into hw1 directory. IMPORTANT NOTE this is the directory I will be looking for when grading your solution. You are responsible for making sure your code is submitted to the correct directory. If you submit your code to some other directory, I will not find your solution.

We'll represent items that the vending machine sells with a Snack class that has the following attributes:

Note that price is an integer and represents the price of the item in cents. For exaple, an item that costs $1.25 will have price=125. We will need a way to set values for the price and name attributes of Snack objects. Include a constructor in your Snack class that has the following method signature: This constructor will initialize the Snack object with values provided as arguments to the constructor.

We'll represent a vending machine with a VendingMachine class, which stores some number of Snack objects in an attribute.The number of Snack objects in a vending machine is not fixed: it can increase and decrearse as the vending machine is being used. What are the possible ways to represent such an attribute in a class? Determine how you will represent it.

To simulate a vending machine, we need to consider the possible uses (or use cases) that we need to model. Specifically, a vending machine operator may (1) add more snacks to the machine, (2) turn on the vending machine, (3) turn off the vending machine. A vending machine customer may (4) purchase an item from the machine. Therefore, we'll start by designing a vending machine class that can support the 5 operations (use cases) we outlined. Specifically, your VendingMachine will need the following methods:

Write a Snack class and a VendingMachine class that has methods outlined above. (Tip: start with a Snack class and make sure your code compiles. Then add a VendingMachine class and get this code to compile). Let's now add a Driver class with a main method that uses you VendingMachine class. Specifically, your main method needs to:
  1. Instantiate a VendingMachine class
  2. Turn off the vending machine
  3. Add 5 different snack objects to the vending machine: ("Chips", $0.75), ("Twix", $1.00), ("Cookies", $1.25), ("Gum", $0.85), ("Pretzels", $1.05).
  4. Sell "Twix" and print the returned value to the screen. Use System.out.println to print the returned value. What method do you need to add to the Snack class to make sure your output shows the price and name of the snack?. The vending machine should return null. Why is that?
  5. Turn on the vending machine
  6. Sell "Twix" and print the returned value to the screen. Use System.out.println to print the returned value. What method do you need to add to the Snack class to make sure your output shows the price and name of the snack?.
  7. Sell "Twix" again and print the returned value. The vending machine should return null. Why is that?
  8. Sell "Gum" and print the returned value.
  9. Sell "Pretzels" and print the returned value.
Compile and run your Driver. Does it do what you expected it to do or did you get a NullPointerException. If you got a NullPointerException, it means you are using some variable that has not been instantiated. Most likely, it is a VendingMachine variable/attribute that holds the collection of Snack objects. Let's consider what happened. When you don't write a constructor for a class, a no-argument constructor is provided for you by default. However, the no-argument constructor will leave your class attributes uninitialized (or initialze them to default values). In the case of non-primitive data types, the default value is null, which is exactly what's causing the NullPointerException when you use your VendingMachine object. To fix this problem, add a no-argument constructor to the VendingMachine class that instantiates the variable holding the collection of Snack objects. Now run your driver again. Your output should look like this:
null
Twix: $1.00
null
Gum: $0.85
Pretzels: $1.05

Note, that the price is formatted as $<DOLLARS>.<CENTS>. For example, Pretzels should show the price of $1.05 and Twix should show the price of $1.00.

Submitting your solution

Submit your VendingMachine.java, Snack.java, and Driver.java to hw1 directory of your CSCI 2300 git repo using git command line tool. Remember to use

Additionally, select git commands and their output from your terminal window, copy your selection, and paste it into a file MY_COMMIT.txt. Submit this file to the same hw1 directory using git command line tool. This will allow me to verify that you used git command line tool to commit your code, which contributes to your grade for this homework.

Verifying that your submission is successful

Log in to git.cs.slu.edu using your git credentials and veriify that you see your latest code in the hw1 directory of your CSCI 2300 repository. If you do not, it means that your solution is not submitted and I will not find it.

Grading

All submitted code must compile. Code that does not compile will receive a grade of zero. I will be compiling your submissions on hopper.slu.edu with the following command:
javac Driver.java
It is a good idea to verify that your code compiles on hopper.slu.edu using this command.

To grade your programs, I will use the list of questions below. Each question can score you from 0 to 2 points: No - 0, Somewhat - 1, Yes - 2. The sum of these points will be your grade for this homework.

  1. Does your driver contain all required steps in the correct order?
  2. Did you break up your code into the required classes?
  3. Does your VendingMachine contain all required methods?
  4. Does sell method of the VendingMachine work correctly?
  5. Does your output price format match the required format?
  6. Did you add the appropriate method for printing the human readable version of the object to the Snack class?
  7. Does your program run to completion and produces expected output?
  8. Did you make instance variables of the Snack and the VendingMachine classes private?
  9. Did you follow good programming practices (code indentation, variable names, class names)?
  10. Did you commit and push your submission using git command line tool?