Howard Wang

Howard Wang
Homework #3
Explain why a static method cannot refer to an instance variable.
A static method is only associated and invoked through the class itself, and not an
instance of the class. An instance variable exists only in instances of the class.
Therefore, a static method has no access to instance variables since it is not associated
with an instance of a class, which is the only place where instance variables can exist.
Create an interface called VCR that has methods that represent the standard
operations on a video cassette recorder (play, stop, etc.). Define the method
signatures any way you desire. Describe how a class might implement this
public interface
public void
public void
public void
public void
public void
play(boolean playing, double position);
fastFwd(boolean playing, double position);
rewind(boolean playing, double position);
A class that implements this interface will simply define the methods to perform the
corresponding action of the abstract method declared here. The play method will take
a boolean parameter specifying whether or not something is currently playing, and
either play or pause accordingly. It will also take a double specifying the time in a
recording when the method is invoked. The fastFwd and rewind methods also take
boolean parameters that specify whether something is being played or not, and will
perform different types of rewind/fast forward actions that may be defined by the
class that implements this interface, depending on the context in which the methods
are being invoked. Similarly, these two methods will take and process a type double
parameter called position much in the same way that play does.
Draw a UML class diagram that shows the relationships among the elements of
Exercise 5.6.
main (args : String[]) : void
+ play (boolean, double) : void
+ stop () : void
+ fastFwd (boolean, double) : void
+ rewind (boolean, double) : void
+ eject () : void
- position : double
- duration : double
- title : String
Draw a UML class diagram that shows the relationships among the classes used
in the PushCounter program.
- APPLET_WIDTH : int = 300
- APPLET_HEIGHT : int = 35
- pushes : int
- label : JLabel
- push : JButton
+ init() : void
+ actionPerformed(ActionEvent) : void
Draw a UML class diagram that shows the relationships among the classes used
in the Fahrenheit program.
- WIDTH : int = 300
- HEIGHT : int = 75
- frame : JFrame
- panel : JPanel
- inputLabel : JLabel
- outputLabel : JLabel
- resultLabel : JLabel
- fahrenheit : JTextField
main (args : String[]) : void
+ display() : void
+ actionPerformed(ActionEvent) : void
Which of the following are valid declarations? Which instantiate an array
object? Explain your answers.
int primes = {2, 3, 4, 5, 7, 11};
Invalid, attempts to assign an initializer list to a variable declared as an integer
type, not an integer array.
float elapsedTimes[] = {11.47, 12.04, 11.72, 13.88};
Valid, brackets signifying an array can follow either the type or the variable
int[] scores = int[30];
Invalid, needs new operator preceding int[30];
int[] primes = new {2,3,5,7,11};
Invalid, new operator not required when initializing an array using an
initializer list
int[] scores = new int[30];
char grades[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘f’};
char[] grades = new char[];
Invalid, size of array not specified.
Describe what problem occurs in the following code. What modifications should
be made to it to eliminate the problem?
int[] numbers = {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6};
for (int count = 1; count <= numbers.length; count++)
System.out.println (numbers[count]);
In the preceding code fragment, the println statement in the for loop will not print all
the values to the screen and will also throw an error when it tries to print the last
value specified by the local count variable. This is due to the fact that Java indexes
array values from 0 to N-1 instead of 1 to N. Therefore, in order to eliminate the
problem, the f
Write an array declaration and any necessary supporting classes to represent
the following statements:
a. students’ names for a class of 25 students
b. students’ test grades for a class of 40 students
c. credit-card transactions that contain a transaction number, a merchant name,
and a charge
d. students’ names for a class and homework grades for each student
e. for each employee of the L&L International Corporation: the employee
number, hire date, and the amount of the last five raises
Write a method called switchThem that accepts two integer arrays as
parameters and switches the contents of the arrays. Take into account that the
arrays may be of different sizes.
Describe a program for which you would use the ArrayList class instead of
arrays to implement choices. Describe a program for which you would use
arrays instead of the ArrayList class. Explain your choices.