maček
maček

Reputation: 77778

What does this Java code mean?

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)

Upvotes: 0

Views: 553

Answers (5)

Matthew Flaschen
Matthew Flaschen

Reputation: 284826

No, it's not recursive. It just means the static initialization of the class will create instances of itself. Before the instances are created, the fields are null, but that should be a moot point unless you use zero or one in the constructor.

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 509

zero and one are a way of predefining templates that can be used in your code. Another example would be to have String.empty, which is an instantiation of an empty string so that you could do things like if ("" == String.empty). It just makes your code more readable.

Upvotes: 0

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36494

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 BigInteger class, with predefined values for common numbers like ZERO, ONE and TEN.

Upvotes: 3

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

That's a technique used when only a limited number of instances exist. They all get declared and then referred to as constants. It's traditional to call them ZERO and ONE to make it more obvious they are constants. It's actually not recursive because static variables are only created once.

Upvotes: 5

hvgotcodes
hvgotcodes

Reputation: 120198

since bits can only have two values, 1 and 0, you are creating static references to objects representing those values. It makes more sense than creating a new object every time you need to represent a 1 or 0.

Upvotes: 0

Related Questions