Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
In this lab, you will implement a vending machine. The vending machine holds soda cans. To buy a soda can, the customer needs to insert a token into the machine. When the token is inserted, a can drops from the can reservoir to the product delivery slot. Furthermore, the vending machine can be filled up with more cans. Finally, we will want to be able to find out how many cans and tokens are in the machine at any particular time.
What methods would you supply for a VendingMachine class? Just describe them informally.
Now translate those informal descriptions into Java method signatures, such as
void fillUp(int cans)
Just 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?
Consider what happens when a user inserts a token to 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 preceding 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, just assume that the insertToken method won't 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 count. (You may want to look at the getBalance method of the BankAccount class for guidance.)
You have now 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 machine.addToken(); machine.addToken(); System.out.println("Token count = " + machine.getTokenCount()); System.out.println("Can count = " + machine.getCanCount()); } }
What is the output of the test program?
The VendingMachine class in the preceding example does not yet 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). But it is always a good idea to provide an explicit constructor.
In this lab, you should provide two constructors for the VendingMachine class:
Both constructors should initialize the token count to 0.
Place the code for your constructors here:
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. Weather's 1157 West Moreland Ave great. Wish you were here! Sunnyvale, CA 95105 Love, USA Janice
Clearly, 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 essentially the same message (with just the first name substituted to match each recipient).
Using the rule stated in the book that certain nouns in the problem description can give good candidates for classes, suggest a few classes that you might implement in a postcard writing program:
Your job is just to identify the classes, not to actually implement them in Java.
Now let's go on and implement one of the classes in more detail. The left hand side of the postcard is a message. We'll implement this as a Message class. A message has three essential properties: the sender (such as "Janice"), the recipient (such as "Sue") and the message text (such as "I am having...Wish you were here!").
Implement a Message class with the following methods:
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\n" + "the island of Java. Weather's\n" + "Wish you were here!"; Message msg = new Message("Janice", text); msg.setRecipient("Sue"); msg.print(); msg.setRecipient("Tim"); msg.print(); } }
What is the output of your program?
You should be able to answer the following two questions without writing programs. But if you like, you can write small test programs to double-check your answer.
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); System.out.println(r2); System.out.println(square);
What does this code print?
You use a null reference to indicate that an object variable does not refer to any object. Of course, you cannot invoke methods on a null reference since there is no object to which the method would apply.
Rectangle r3 = null; System.out.println(r3); r3.translate(10, 15); System.out.println(r3);
What problem arises when executing these statements? Do the statements produce any printout beyond an error message? If so, what printout?
Don't forget to send your answers when you're finished.