Question | Value | Score |
1 | 8 | |
2 | 8 | |
3 | 18 | |
4 | 24 | |
5 | 16 | |
6 | 16 | |
7 | 10 | |
Total | 100 |
1. Give an equivalent sequence of Java statements that does not use the for statement. If it cannot be done, say "It cannot be done."
for ( int
count = 1; count <= 10; count++ ) {
if ( count == 5 )
continue;
g.drawString( Integer.toString( count ), xPos, 25 );
xPos += 10;
}
while (
count <= 10 ) {
if ( count != 5 ) {
g.drawString( Integer.toString( count ), xPos, 25 );
xPos += 10;
}
count++;
}
public float getLength() { return length; }
public float getWidth() { return width; }
public void setLength(float
l) {
length = l;
if ( l < 0.0 || l > 20.0 )
length = (float) 0.0;
}
public void setWidth(float
w) {
width= w;
if ( w < 0.0 || w > 20.0 )
width = (float) 0.0;
}
public float area() { return length * width; }
public boolean square()
{ return length == width; }
}
Provide the following methods:
For example, if a represents the set {0, 2, 4} and b represents the set {2, 3, 4, 5} then a.union(b) represents the set {0, 2, 3, 4, 5}.
Solution:
public IntegerSet()
{
a = new int[SIZE + 1];
for (int i = 0; i < SIZE; i++)
a[i] = 0;
}
public IntegerSet union(IntegerSet
s) {
IntegerSet u = new IntegerSet(); // initialize to empty
for (int i = 0; i <= SIZE; i++)
if (a[i] == 1 || s.a[i] == 1)
u.a[i] = 1;
return u;
}
public void insertElement(int
e) {
if ( 0 <= e && e <= SIZE)
a[e] = 1;
}
public void deleteElement(int
e) {
if ( 0 <= e && e <= SIZE)
a[e] = 0;
}
public boolean isEqual(IntegerSet
s) {
for (int i = 0; i <= SIZE; i++)
if ( a[i] != s.a[i] )
return false;
return true;
}
public void print(Graphics
g) {
int j = 0, yPos = 15;
boolean empty = true;
for (int i = 0; i <= SIZE; i++)
if ( a[i] == 1 ) {
empty = false;
g.drawString(" " + i, 20*j, yPos);
if ( ++j % 10 == 0 ) {
j = 0;
yPos += 15;
}
}
if ( empty )
g.drawString("---", j, yPos);
}
}
public class HugeInteger {
private int digits[];
// digits of huge integer
final static int SIZE
= 10; // number of digits of huge integer
boolean error;
// if overflow or underflow occurred, then true
// else false
// Construct a Huge
0.
public HugeInteger()
{}
// Constructor that
takes an array of SIZE digits
public HugeInteger(int
d[]) {}
// Construct a HugeInteger
of value I
public HugeInteger(HugeInteger
I) {}
// Return a String representation
of a HugeInteger
public String outputHugeInteger()
{}
// Add I to this, put
result in this
public HugeInteger
addHugeIntegers(HugeInteger I) {}
// Subtract I from this,
put result in this
public HugeInteger
subtractHugeIntegers(HugeInteger I) {}
// Multiply this by
I, put result in this
public HugeInteger
multiplyHugeIntegers(HugeInteger I) {}
// Divide this by I,
put result in this
public HugeInteger
divideHugeIntegers(HugeInteger I) {}
// return the remainder
of this when divided by I (this is unchanged)
public HugeInteger
modulusHugeIntegers(HugeInteger I) {}
// true when this ==
I
public boolean isEqualTo(HugeInteger
I) {}
// true when this <
I
public boolean isLessThan(HugeInteger
I) {}
}
for (; T.isLessThan(this);
T.addHugeIntegers(One)) {
if (this.modulusHugeIntegers(T).isEqualTo(Zero))
return T;
}
return this;
}
It looks like this:
for (; !T.isEqualTo(One);
T.divideHugeIntegers(T.factor()) )
g.drawString(T.factor().outputHugeInteger(), 20, j += 20);
}
Solution:
For example, if we have the following classes:
public class Birthdate { ... }
public class TelephoneNumber { ... }
public class Employee {
Birthdate bDate;
// employee's birthdate
TelephoneNumber tNumber;
// employee's telephone number
...
}
public class Janitor extends Employee {
Room access[];
// has access to these rooms
...
}
The class Employee has a Birthdate, a TelephoneNumber, and other attributes; a Janitor is an Employee, with special attributes or behaviors of his own.