Class Tally

java.lang.Object
  extended by Tally

public class Tally
extends java.lang.Object

This class represents a tally of integers, indexed from zero.


Field Summary
static Tally IMPOSSIBLE
          Singleton constant representing an impossible tally.
 
Constructor Summary
Tally(int n)
          Constructs a tally of size n with all integers initialized to zero.
Tally(int[] data)
          Constructs a tally of size n from the given data, where n = data.length.
 
Method Summary
 Tally add(Tally t)
          Adds tally t to this tally, returning the "sum" as a new tally.
 boolean equals(java.lang.Object t)
          Returns true iff this tally is value-identical to t.
 int getElement(int i)
          Returns the int at position i.
 int hashCode()
          Returns the product of this tally’s elements as its hash code.
 boolean isImpossible()
          Returns whether the tally is impossible.
 int length()
          Returns the length (i.e., the number of elements) of this tally.
 void setElement(int i, int j)
          Sets the int at position i to value j.
 java.lang.String toString()
          Returns a string representation of this tally.
 int total()
          Returns the total of the elements in this tally.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

IMPOSSIBLE

public static final Tally IMPOSSIBLE
Singleton constant representing an impossible tally.

Constructor Detail

Tally

public Tally(int n)
Constructs a tally of size n with all integers initialized to zero.

Parameters:
n - the number of elements in this tally

Tally

public Tally(int[] data)
Constructs a tally of size n from the given data, where n = data.length.

Parameters:
data - the ints that should populate the tally
Method Detail

isImpossible

public boolean isImpossible()
Returns whether the tally is impossible.

Returns:
whether the tally is imposible

setElement

public void setElement(int i,
                       int j)
Sets the int at position i to value j.

Parameters:
i - the position of the int to set
j - the value to which the position should be set

getElement

public int getElement(int i)
Returns the int at position i.

Parameters:
i - the position whose int is requested
Returns:
the int at position i

length

public int length()
Returns the length (i.e., the number of elements) of this tally.

Returns:
the length of this tally

total

public int total()
Returns the total of the elements in this tally. For example, the 3-element tally (9, 3, 2) has a total of 14.

Returns:
the total of the elements of this tally

add

public Tally add(Tally t)
Adds tally t to this tally, returning the "sum" as a new tally.

Parameters:
t - the tally to add to this tally
Returns:
the element-wise sum of this and t

equals

public boolean equals(java.lang.Object t)
Returns true iff this tally is value-identical to t.

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Returns the product of this tally’s elements as its hash code.

Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

toString

public java.lang.String toString()
Returns a string representation of this tally.

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()