Question: Please I need help with myAbacus. // TO DO: add your implementation and JavaDocs public class MyAbacus implements Abacus { // ADD MORE PRIVATE MEMBERS
Please I need help with myAbacus.
// TO DO: add your implementation and JavaDocs
public class MyAbacus implements Abacus {
// ADD MORE PRIVATE MEMBERS HERE IF NEEDED!
// Remember: Using an array in this class = no credit on the project!
public MyAbacus(int base) {
// throws IllegalArgumentException if base is invalid
// remember: an abacus should always have at least one
// column!
}
public int getBase() {
// O(1)
return -1; //default return, make sure to remove/change
}
public int getNumPlaces() {
// O(1)
return -1; //default return, make sure to remove/change
}
public int getBeadsTop(int place) {
// O(1)
return -1; //default return, make sure to remove/change
}
public int getBeadsBottom(int place) {
// O(1)
return -1; //default return, make sure to remove/change
}
public boolean equals(MyAbacus m) {
// O(N) where N is the number of places currently
// in use by the abacus
return false; //default return, make sure to remove/change
}
public DynArr310 add(String value) {
// Hints:
// see: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-int-
// and: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-
// Also... I personally found a recursive helper function really, really useful here...
// Important: each Abacus in the DynArr310 returned should
// be a copy of this abacus. If you just add this abacus over and over
// you'll just get the final abacus shown multiple times in the GUI.
return null; //default return, make sure to remove/change
}
// --------------------------------------------------------
// example testing code... edit this as much as you want!
// --------------------------------------------------------
public static void main(String[] args) {
//this is the sequence from the project description
Abacus a = new MyAbacus(10);
DynArr310 steps;
AbacusGUI.printAbacus(a);
AbacusGUI.fullPrintAdd(a, "36");
AbacusGUI.fullPrintAdd(a, "12");
AbacusGUI.fullPrintAdd(a, "2");
AbacusGUI.fullPrintAdd(a, "12");
AbacusGUI.fullPrintAdd(a, "10");
AbacusGUI.fullPrintAdd(a, "2");
AbacusGUI.fullPrintAdd(a, "68");
AbacusGUI.fullPrintAdd(a, "50");
AbacusGUI.fullPrintAdd(a, "10");
AbacusGUI.fullPrintAdd(a, "5");
AbacusGUI.fullPrintAdd(a, "3");
AbacusGUI.fullPrintAdd(a, "128");
AbacusGUI.fullPrintAdd(a, "3000000");
}
}
public interface Abacus {
/**
* Gets the number base of the abacus. This base
* will never be an odd number and never be less
* than 2.
*
* @return the number base of the abacus
*/
public int getBase();
/**
* Returns the number of places (bead columns) the
* abacus is using to represent the number. This
* will never be less than one.
*
* @return the number of places in use
*/
public int getNumPlaces();
/**
* Gets the number of beads in the top area of the
* abacus which are in-use for a given place. (The
* number of beads in the top which are pushed down
* to the center.)
*
* @param place the beads column of interest (0 is the right-most place)
* @return the number of beads currently in use
* @throws IndexOutOfBoundsException if the place requested is not in use
*/
public int getBeadsTop(int place);
/**
* Gets the number of beads in the bottom area of the
* abacus which are in-use for a given place. (The
* number of beads in the bottom which are pushed up
* to the center.)
*
* @param place the beads column of interest (0 is the right-most place)
* @return the number of beads currently in use
* @throws IndexOutOfBoundsException if the place requested is not in use
*/
public int getBeadsBottom(int place);
/**
* Adds the given string representation of a number to
* the current value of the abacus. The abacus is updated
* to this new position. It returns the steps to perform
* the add (snap shots of the abacus at each step). The
* abacus may be left in an "improper state" if the
* provided arguements are invalid.
*
*
A snapshot is required for each of the following steps:
*
*
- - the initial state
*
- - the final state
*
- - expansions (beads should not be moved, the abacus just
* becomes bigger/smaller)
*
- - exchanges (beads are exchanged in one step)
*
- - movement of X beads up OR down (not both at the same time)
* in ONE place on the bottom OR top of the abacus (not both
* at the same time)
*
*
* @param value the string representation of the value to add (e.g. "100" in base 10, or "1f" in base 16)
* @return the different positions the abacus was in (including the start and finish states)
* @throws NumberFormatException if string is not correct for the base
*/
public DynArr310 add(String value);
}
import java.util.Arrays;
//TO DO: add your implementation and JavaDocs
//Big Hint: Do not try to implement this in the
//order shown... read the assignment fully first.
public class DynArr310 {
//default initial capacity / minimum capacity
private static final int DEFAULT_CAPACITY = 2;
//underlying array -- you must use this!
//we will be "breaking in" to look at it,
//even though it is private
private T[] data;
private int size;
private int capacity;
@SuppressWarnings("unchecked")
public DynArr310() {
data = (T[]) new Object[DEFAULT_CAPACITY];
size = 0;
capacity = DEFAULT_CAPACITY;
}
@SuppressWarnings("unchecked")
public DynArr310(int initialCapacity) {
if (initialCapacity < 1)
throw new IllegalArgumentException();
data = (T[]) new Object[initialCapacity];
size = 0;
capacity = initialCapacity;
}
public int size() {
return size; // default return, make sure to remove/change
}
public int capacity() {
return capacity; // default return, make sure to remove/change
}
@SuppressWarnings("unchecked")
public void add(int index, T value) {
if (index < 0)
throw new IndexOutOfBoundsException();
if (index == capacity || size == capacity) {
T[] temp = (T[]) new Object[2 * capacity];
for (int i = 0; i < size; ++i)
temp[i] = data[i];
data = temp;
capacity = 2 * capacity;
}
for (int k = size - 1; k >= index; k--)
data[k + 1] = data[k];
data[index] = value;
size++;
}
public T get(int index) {
if (index < 0 || index >= capacity)
throw new IndexOutOfBoundsException();
return data[index];
}
public T replace(int index, T value) {
if (index < 0 || index >=capacity)
throw new IndexOutOfBoundsException();
T old = data[index];
data[index] = value;
return old;
}
public int firstIndexOf(T value) {
for(int i=0; i
if(data[i].equals(value))
return i;
}
return -1; // default return, make sure to remove/change
}
@SuppressWarnings("unchecked")
public T delete(int index) {
if (index < 0 || index >=capacity)
throw new IndexOutOfBoundsException();
T val = data[index];
for(int k = index; k
data[k] = data[k+1];
size--;
if(size <= capacity/3) {
T[] temp = (T[]) new Object[capacity/2];
for(int i=0 ;i
temp[i] = data[i];
data = temp;
capacity = capacity/2;
}
return val;
}
public int deleteAll(T value) {
int count = 0;
for(int i=0; i
if(data[i].equals(value)) {
delete(i);
++count;
}else {
++i;
}
}
return count;
}
@SuppressWarnings("unchecked")
public boolean setCapacity(int newCapacity) {
if(newCapacity < DEFAULT_CAPACITY)
return false;
if(newCapacity < capacity)
return false;
T[] temp = (T[]) new Object[newCapacity];
for (int i = 0; i < size; ++i)
temp[i] = data[i];
data = temp;
capacity = newCapacity;
return true;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
DynArr310 other = (DynArr310) o;
if (size != other.size)
return false;
if (capacity != other.capacity)
return false;
if (!Arrays.deepEquals(data, other.data))
return false;
return true;
}
public static DynArr310 clone(DynArr310 orig) {
Object[] data = new Object[orig.capacity];
for(int i=0; i
data[i] = orig.data[i];
}
DynArr310 arr = new DynArr310(orig.capacity);
arr.capacity = orig.capacity;
arr.size = orig.size;
arr.data = (E[]) data;
return arr;
}
public static boolean isClone(DynArr310 arr1, DynArr310 arr2) {
if (arr1 == null || arr2 ==null)
return false;
if (arr1.size != arr2.size)
return false;
if (arr1.capacity != arr2.capacity)
return false;
for(int i=0; i
if(arr1.data[i] != arr2.data[i])
return false;
}
return true;
}
public static void main(String args[]) {
DynArr310 nums = new DynArr310<>();
if ((nums.size() == 0) && (nums.capacity() == 2)) {
System.out.println("Yay 1");
}
for (int i = 0; i < 3; i++) {
nums.add(i, i * 2);
}
if (nums.size() == 3 && nums.get(2) == 4 && nums.capacity() == 4) {
System.out.println("Yay 2");
}
DynArr310 msg = new DynArr310<>();
msg.add(0, "world");
msg.add(0, "hello");
msg.add(1, "new");
msg.add(3, "!");
if (msg.get(0).equals("hello") && msg.replace(1, "beautiful").equals("new")
&& msg.size() == 4 && msg.capacity() == 4) {
System.out.println("Yay 3");
}
if (!msg.setCapacity(0) && !msg.setCapacity(3) && msg.setCapacity(20)
&& msg.capacity() == 20) {
System.out.println("Yay 4");
}
//delete and shrinking
if (msg.delete(1).equals("beautiful") && msg.get(1).equals("world")
&& msg.size() == 3 && msg.capacity() == 10) {
System.out.println("Yay 5");
}
\\
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
