Student Handout: Java Programming

Java Return Types

 

 

It is often useful for an object to return information about itself. You can do this by creating methods that send back particular information about the object. You need to define the information’s data type in advance. This is known as the method’s return type. Likewise, you can send information to an object through a method by way of arguments. Each argument has a particular data type defined in advance. This is known as the argument type. You can also invoke methods to display information or perform a calculation.

The various method and return types are outlined in all of the current Java texts. In this unit, you will have the opportunity to see defined methods in action and create additional ones.

Create a new project called Vending and a VendingMachine class. Open the class in the editor and enter the code that follows. Remember to compile your class as you go along.

/**
* VendingMachine.java
* This class simulates a vending machine. Each machine can sell
* one type of product at a fixed price.
*
* @author (Your name here)
* @version (Today's date here)
*/

public class VendingMachine
{

// The following are instance variables but they

// are also known as properties

private String productType; // type of product

private int productPrice; // product's price

private int numberSold; //total number of units sold

private int balance; //amount of money buyer inserted

private int totalMoney; //total $ taken by this machine

  //constructor

  //the public methods:
  /**
   * Method to set this machine's type of product
  */
  public void setType(String type)
  {
        productType = type;
  } // end of setType()

  /**
  * Return this machine's type of product
  */
  public String getType()
  {
        return productType;
  } // end of getType()

  /**

     * Allow buyer to insert money
  */
  public void insertMoney(int money)
  {
       balance += money;
  } // end of insertMoney()

     /**
  * give the buyer the product
  */
  public void giveProduct()
  {

                 System.out.println("*--------------*");

                 System.out.println("| You bought |");

  System.out.println("| |");

  System.out.println("| " + productType);

  System.out.println("|--------------|");

  numberSold ++; // increase number sold by one

} // end of giveProduct()

/**
* Change this method so it returns the amount of change to return

            for a given product
*/
public void giveChange()
{

balance -= productPrice; //how much to give back

System.out.println("Your change is: " + balance);

balance = 0; // clear balance

} // end of giveChange()

} // end of VendingMachine class

Create a new instance of the VendingMachine class and test all of the methods.

YOUR JOB:

 

·     Create a main and instantiate the VendingMachine Class

·       Complete the VendingMachine class to include methods to get and set the product price

·        Create a totalMoney method that sets and gets the instance variable total money

·        Comment where appropriate

·       Test Throughly with at least 3 test cases

·        Save, compile, and test OFTEN!

EXTRAS YOU CAN ADD:

·        Get creative and make the program better. Add more properties and methods to make the machine more flexible.