Last Revised 9/13/02 at 12:30pm.

CSC 211 -- Project VendingMachine -- 50 Points

If you have not yet installed JDK 1.3 and BlueJ, follow the Installation Directions.

Create a project similar to the VendingMachine Example from Intro (Intro.zip or Intro.txt).

Follow these steps:

  1. Create a folder for the VendingMachine project called VendingMachineSmith. (Use your last name instead of Smith).

  2. Invoke BlueJ.   Start the BlueJ IDE by finding and double clicking on its icon. If you followed the installation directions for home, it will have an icon on the desktop. In the computer labs, select Course Applications, CSC, BlueJ.

  3. Create the VendingMachine Project.   In the BlueJ IDE, select Project and New.... to get the New Project Dialog. Select the VendingMachineSmith folder that you created in Step 1. Enter VendingMachine for the File name and click on Create. The Project Display Area to the middle right should turn white, and an icon that looks like a piece of paper with five lines and the corner turned down (representing the project) should appear.

  4. Create the VendingMachine Class.   Click on the button New Class... to obtain the Create New Class Dialog. Enter VendingMachine in the Class Name textbox, and make sure that the Class radio button is selected. Then click on the OK button. A yellow icon with the label VendingMachine and diagonal lines representing the uncompiled class should appear.

  5. Open the editor for the VendingMachine class.   Double click on the yellow VendingMachine icon to open the editor for that class.

  6. Modify the code for the VendingMachine Class.   When you open the VendingMachine class, you will see some sample code for the class, including an instance variable x, a constructor VendingMachine, and the method named sampleMethod. This code may be interesting the first time you see it, but it is not useful in general. Select all of the code (Control-a) and delete it. Add the following code:

    // Project VendingMachine
    // Class VendingMachine

    public class VendingMachine
    {

      // Instance Variables:
      // The number of candy bars currently in machine.
      public int numberCandyBars;

      // The number of quarters deposited for current
      // candy bar purchase.
      public int numberQuarters;

      // Default NoArg Constructor
      public VendingMachine()
      {
        numberCandyBars = 0;
        numberQuarters = 0;
      }
      public VendingMachine(int n)
      {
        numberCandyBars = n;
        numberQuarters = 0;
      }

      public void depositQuarter()
      {
        numberQuarters++;
      }

      public void selectCandy()
      {
        if(numberQuarters >= 3 && numberCandyBars > 0)
        {
          System.out.println("Candy bar dispensed.");
          numberQuarters = 0;
          numberCandyBars--;
        }
        else
        {
          System.out.println("Could not dispense candy bar");
        }
      }
      public void printStatus()
      {
        System.out.println("Number of candy bars = " + numberCandyBars);
        System.out.println("Number of quarters = " + numberQuarters);
        System.out.println();
      }
    }

  7. Compile the VendingMachine Class.   Click on the Compile button at the top of the editor. You should get the message "Class compiled - no syntax errors."

  8. Create the Main Class.   Repeat Steps 4 to 7 to create the Main class with this code:

    // Project VendingMachine
    // Project Main

    public class Main
    {

      public static void main(String[] args)
      {
        VendingMachine vm;

        // Create new VendingMachine object with 3 candy bars.
        vm = new VendingMachine(3);

        // Print status of VendingMachine object.
        vm.printStatus();

        // Deposit 3 quarters (correct amount) and select candy.
        // Then print status of vending machine.
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.selectCandy();
        vm.printStatus();

        // Deposit 5 quarters (too many) and select
        // candy. Then print the status.
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.selectCandy();
        vm.printStatus();
        // Deposit 2 quarters (too few) and select
        // candy. Then print the status.
        vm.depositQuarter();
        vm.depositQuarter();
        vm.selectCandy();
        vm.printStatus();

        // Deposit 3 quarters (correct amount) and
        // select candy. Then print the status.
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.selectCandy();
        vm.printStatus();

        // Deposit 3 quarters (correct amount) and
        // select candy. Then print the status.
        vm.depositQuarter();
        vm.depositQuarter();
        vm.depositQuarter();
        vm.selectCandy();
        vm.printStatus();
      }
    }

  9. Run the application.   Go back to the main BlueJ window. Right click on the yellow Main icon, and select void main(args) to run the main method. You will get the Method Call Dialog. Since you don't have any command line arguments to enter, click on OK. You will get the following output in the Terminal Window.

    Sometimes the Terminal Window will not be visible, so you will have to find its entry in the task bar and restore it. If it is not open, open it by selecting on View and Show Terminal in the main BlueJ window.

  10. Modify the VendingMachine Project   Change the main method in the Main class so that it creates a VendingMachine object with 10 candy bars. Then use it to buy 4 candy bars at 75 cents each. Call the depositQuarter and selectCancy methods to do this.

  11. Get out of BlueJ.   Close the Terminal Window, the Editor, and the Main BlueJ Window. The console window that opened when you invoked BlueJ should close automatically.

  12. Zip the Project.   Right click on the file VendingMachineSmith in the Microsoft Explorer and select Open With and Compressed (zipped) Files. A zip file named VendingMachineSmith.zip will appear.

  13. Zip and Submit the Project.   Submit the VendingMachine Project using the DLWeb Assignment Submission System at http://DLWeb.cs.depaul.edu. Submit it under the project name VendingMachine.