Section Problems Number 3 - Creating Classes
Be prepared to discuss these problems in discussion section.
Consider the following class that models a tumbler lock.
public class Lock{
private final int myNumTumblers;
private int[] myCurrent;
private int[] myRequired;
//pre: numTumblers > 0
public Lock(int numTumblers){
this(numTumblers, new int[numTumblers]);
}
// pre: numTumblers > 0, required != null, required.length =
numTumblers,
// all elements of required are between 0 and 9 inclusive
public Lock(int numTumblers, int[] required){
this(numTumblers, required, new
int[numTumblers]);
}
// pre: numTumblers > 0, required != null, required.length =
numTumblers,
// all elements of required are between 0 and 9 inclusive
// current != null, current.length = numTumblers
// all elements of current are between 0 and 9 inclusive
public Lock(int numTumblers, int[] required, int[] current){
myNumTumblers = numTumblers;
myCurrent = new int[myNumTumblers];
myRequired = new int[myNumTumblers];
for(int i = 0; i < myNumTumblers; i++){
myRequired[i]
= required[i];
myCurrent[i]
= current[i];
}
}
// pre: 0 <= tumbler < numTumblers(), 0 <= newSetting <= 9
public void alterTumbler(int tumbler, int newSetting){
myCurrent[tumbler] = newSetting;
}
// pre: none
public boolean canOpen(){
boolean correct = true;
int i = 0;
while( correct && i < myNumTumblers){
correct =
myCurrent[i] == myRequired[i];
i++;
}
return correct;
}
// pre: otherLock != null
public boolean sameSettings(Lock otherLock){
boolean same = true;
int i = 0;
while( same && i < myNumTumblers){
same =
myCurrent[i] == otherLock.myCurrent[i];
i++;
}
return same;
}
public int numTumblers(){
return myNumTumblers;
}
}
0. Explain what the calling object of a method is and how you can refer to it.
1. In the constructor with a single int what is the purpose of the single line of code:
this(numTumblers, new int[numTumblers]);
2. Write assertions to check the preconditions of the 3 constructors
3. In the method sameSettings
a parameter is sent to the method
named otherLock
. In this method the code refers to
otherLock.myCurrent
. But this is a private data member. Why is the code
in the sameSettings
class allowed to refer to it or is this a
syntax error?
4. The method sameSettings
is suppose to test if 2
different Lock objects currently have all their tumblers set to the same value.
There is a logic error in this code. What is it? How would you fix it?
5. Could the coupling of method sameSettings
be
reduced? In other words could it be made less tightly coupled?
6. Do you think method numTumblers
is really necessary? Why or
why not?
7. myNumTumblers
is an instance constant. This means every Lock
object that is created has its own myNumTumblers
constant. Classes
often have static
constants. Should myNumTumblers
be
static
? Explain why or why not?
8. What methods do you think should be added to this class so that it accurately models a tumbler lock?
9. Create a class to model complex numbers. Include methods that return the result of the addition, subtraction, multiplication, and division of 2 complex numbers. (One of which is the calling object, the other is sent as an explicit parameter.)