P1. Implementing a simple class

In this lab, you will implement a vending machine. The vending machine 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.

What methods would you supply for a VendingMachine class? Describe them informally.



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

void fillUp(int cans)

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



What instance variables would you supply? Hint: You need to track the number of cans and tokens.



P2. Implementing Methods

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

void insertToken()
{ instructions for updating the token and can counts

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

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.



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



Next, supply two methods getCanCount and getTokenCount that return the current values of the can and token counts. (You may want to look at the getBalance method of the BankAccount class for guidance.)



P3. Putting It All Together

You have implemented all methods of the VendingMachine class.
Put them together into a class, like this:
class VendingMachine
   public your first method
   public your second method
   . . .
   private your first instance variable
   private your second instance variable



Now test your class with the following test program.

public class VendingMachineTest
   public static void main(String[] args)
      VendingMachine machine = new VendingMachine();
      machine.fillUp(10); // fill up with ten cans
      System.out.println("Token count = " + machine.getTokenCount());
      System.out.println("Can count = " + machine.getCanCount());

What is the output of the test program?



P4. Constructors

The VendingMachine class in the preceding example 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.

In this lab, you should provide two constructors for the VendingMachine class:

     - a default constructor that initializes the vending machine with 10 soda cans
     - 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.</P>

Place the code for your constructors here:



P5. Discovering Classes

Consider the following task: You are on vacation and want to send postcards to your friends. A typical postcard might look like this:

Dear Sue: I am having a great time on                Sue Wetheridge
the island of Java. The weather                1157 West Moreland Ave
is great. Wish you were here!                   Sunnyvale, CA 95105
Love,                                             USA    

This is a task that lends itself to automation. You decide to write a computer program that sends postcards to various addresses, each of them with the same message, except that the first name is substituted to match each recipient.

Using the rule stated in your textbook that certain nouns in the problem description are good candidates for classes, suggest some classes that you might implement in a postcard writing program:

Your job is to identify the classes, not to actually implement them in Java.



Now implement one of the classes in more detail. The left-hand side of the postcard is a message. Implement this as a Message class. A message has three essential properties: the sender ("Janice"), the recipient ( "Sue"), and the message text ("I am having...Wish you were here!").

Implement a Message class with the following methods:

   - a constructor that takes two strings, the sender and the message text
   - a method setRecipient that sets the recipient
   - a method print that prints the message



Try out your class with the following test code:

public class MessageTest
   public static void main(String[] args)
   {         String text = "I am having a great time on
         + "the island of Java. Weather's great.
         + "Wish you were here!";
      Message msg = new Message("Janice", text);

What is the output of your program?



R1. Object References

You should be able to answer the following two questions without writing programs. If you like, you can write small test programs to double-check your answers.

Recall that the translate method of the Rectangle class moves a rectangle by a certain amount. For example,

Rectangle r = new Rectangle(5, 10, 20, 30);
r.translate(10, 15);
System.out.println(r); // prints Rectangle[x=15,y=25,width=20,height=30]

When you copy an object variable, the copy is simply a second reference to the same object.

Consider this program segment:

Rectangle square = new Rectangle(0, 0, 100, 100);
Rectangle r2 = square;
r2.translate(10, 15);

What is the output of this code?



A null reference is used to indicate that an object variable does not refer to any object. You cannot invoke methods on a null reference since there is no object to which the method would apply.

Consider this program segment:

Rectangle r3 = null;
r3.translate(10, 15);

Do the statements output anything beyond an error message? If so, what?