Howard Wang - Columbia University



Howard Wang 3/11/2004

CS1007

Homework #3

Exercises

5.2 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.

5.6 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 interface.

public interface VCR

{

public void play(boolean playing, double position);

public void stop();

public void fastFwd(boolean playing, double position);

public void rewind(boolean playing, double position);

public void eject();

}

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.

5.7 Draw a UML class diagram that shows the relationships among the elements of Exercise 5.6.

5.8 Draw a UML class diagram that shows the relationships among the classes used in the PushCounter program.

5.9 Draw a UML class diagram that shows the relationships among the classes used in the Fahrenheit program.

6.1 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];

Valid

char grades[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘f’};

Valid

char[] grades = new char[];

Invalid, size of array not specified.

6.3 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 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download