Practice Questions

advertisement
Practice Questions:
Q1. A combination lock has the following basic properties: the
combination (a sequence of three numbers) is hidden; the lock can
be opened by providing the combination; the combination can be
changed, but only by someone who knows the current combination.
Design a class with public methods “open” and
“changeCombination” and private data fields that store the
combination. The combination should be set in the constructor.
Q2. A BinaryArray represents arbitrarily long sequences of binary
variables. The private data representation is an array of Boolean
variables. For example, the representation of the BinaryArray “TFTTF”
would be an array of length five storing true,false,true,true,false in array
indices 0,1,2,3 and 4 respectively. Implement the BinaryArray class
with the following functionality:
 A one-argument constructor that initializes the array
 Get and Set method to access or change a variable at a particular
index
 A “getSize” method that returns the number of binary variables in
the BinaryArray
Q3. A polynomial is represented by two fields; one is the degree and
second is the coefficient. Thus 5x2+ 2x+3 is degree two and its
coefficients are coeff[0]=3, coeff[1]=2 and coeff[2]=5. Design a class
Polynomial to represent single-variable polynomials which contain the
following functionality:
 A zero argument constructor that initializes the degree and
coefficients to zero
 A two argument constructor that initializes both the degree
and the coefficients array
 A method “negate” that returns negative of the
polynomial. (Note: Just multiply all the coefficients with 1)
 A method “add” that takes two polynomials as input and
computes their sum (Note: a new polynomial should be
created and returned; the original should not be changed.
Also only same degree coefficients are added)
Download