First off, I’m not asking for anyone to do my homework. I’m simply confused with this Java Syntax.
Below is the assignment description and two classes I have to implement. The first one is here for reference only. The second one is the one I’m having trouble with.
Being able to program with numbers that do not (in theory) have a maximum value is a necessity in many applications of computer science. You are going to write a series of classes to get started on this task. Your final class will allow you to represent and at least add Binary numbers of arbitrary length.
// You are to write (implement) this class exactly as dictated by the following list of class members.
public abstract AbstractBit:
private boolean bit;
public abstract AbstractBit clone();
public abstract AbstractBit addBits(AbstractBit guest);
public abstract AbstractBit addBits(AbstractBit guest1, AbstractBit guest2);
public abstract AbstractBit carryBit(AbstractBit guest);
public abstract AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2);
protected void setBit(boolean value)
public boolean getBit()
public AbstractBit()
public AbstractBit(boolean value)
public AbstractBit(AbstractBit guest)
public boolean equals(AbstractBit guest)
public String toString()
What the heck are these?
Why am I adding static class variables of type BinaryBit? Isn’t this going to be recursive in some way? (See the lines marked with ????)
// You are to write (implement) this class exactly as dictated by the following list of class members.
public BinaryBit extends AbstractBit:
public static final BinaryBit zero = new BinaryBit(false); // ????
public static final BinaryBit one = new BinaryBit(true); // ????
public BinaryBit()
public BinaryBit(boolean bit)
public BinaryBit(int bit)
public BinaryBit(BinaryBit guest)
public BinaryBit clone()
public boolean equals(BinaryBit guest)
public String toString()
public AbstractBit addBits(AbstractBit guest)
public AbstractBit addBits(AbstractBit guest1, AbstractBit guest2)
public AbstractBit carryBit(AbstractBit guest)
public AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2)
A bit has only two possible values — zero and one. Any given zero or one is just as good as any other. Therefore, why not create a “canned”, unchangeable zero and one value, and just pass references to those values around? It saves memory and processor time. You might see this approach called the Flyweight pattern.
You can see some of that pattern in Java’s own
BigIntegerclass, with predefined values for common numbers likeZERO,ONEandTEN.