Implement a class Widget with the following
methods. A skeleton for class Widget is
available. Implement the class in the order of the
following description. Make sure what you submit compiles without errors. In
addition a toString() method and a
getSize() method have been written for you (getSize()
returns the number of elements in instance variable
list).
- An instance variable
- private int[] list
- Represents the array of values being maintained.
- A specific constructor
- public Widget(int[] data)
- Initializes list to represent a new
array with data.length elements. The
elements of data are then copied using a
loop to
this.list.
- Three inspectors
- public int positiveCount()
- Returns the number of positive values in array
this.list. A positive value is greater than
zero.
- public int negativeCount()
- Returns the number of negative values in array
this.list. A negative value is less than zero.
- public int zeroCount()
- Returns the number of zeros in array
this.list.
- Four facilitators
- public int[] getPositives()
- Returns a new array of length
this.positiveCount(). The elements of the array are the positive
values of array
this.list.
- public int[] getNegatives()
- Returns a new array of length
this.negativeCount(). The elements of the array are the negative
values of array list.
- public Object clone()
- Returns a new Widget whose list values
are duplicates of the values in this.list.
Hint: use the specific constructor in creating the new Widget.
- public boolean equals(Object v)
- The method returns false if
v is not a Widget.
If v is a Widget
whose list
array is a different size than this.list,
then the method returns false. Otherwise,
the method compares the two lists. If each of the corresponding elements
match, then the method returns true. If any of the corresponding elements
mismatch, the method returns false.
- A default constructor
- public Widget()
- Initializes list to represent a new
int array
with zero elements.