Honors Computer Programming 1-2
Java Chapter 2 Activities
1.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.
AWhat methods would you supply for a VendingMachine class? Describe them informally.
BNow translate those informal descriptions into Java method definitions, such as void fillUp(intsomeCans) . Give the names, parameters, and return types of the methods. Do not implement them yet.
CWhat instance variables would you supply? Hint: You need to track the number of cans and tokens.
2.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.
AImplement a method called insertToken which will increase the number of tokens and decrease the number of cans. You need to use the instance variables that you defined in the previous problem.
BNow supply a method fillUp to add more cans to the machine. Simply add the number of new cans to the can count.
CNext, 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.
This problem continues on the next page . . .
DYou have implemented all methods of the VendingMachine class. Put them together into a class, like this:
Use your computer to enter your code using the file name VendingMachine.java.
Now test your class with the following test program:
What is the output of the test program?
3.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.
AIn this lab, you should provide two constructors for the VendingMachine class:
- a default constructor that initializes the vending machine with 20 soda cans and no tokens
- a constructor VendingMachine(intsomeCans) that initializes the vending machine with the given number of cans and no tokens
4.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,
The above code will print java.awt.Rectangle[x=15, y=25, width=20, height=30] .
AWhen you copy an object variable, the copy is simply a second reference to the same object. Consider this program segment:
What is the output of this code?
BA 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:
Do the statements output anything beyond an error message? If so, what?