Need help with the MyAbacus class, which is implemented from the Abacus class(the picture)
DynArr310
AbacusGUI
Abacus
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");
}
}
1 2 3 // // DO NOT EDIT ANYTHING BELOW THIS LINE // 50 /** 6 * Interface for an abacus for the AbacusGUI to use. 8 90 public interface Abacus { Gets the number base of the abacus. This base will never be an odd number and never be less than 2. * w * @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(); 276 * 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 (O is the right-most place) * @return the number of beads currently in use * @throws IndexOutOfBounds Exception if the place requested is not in use */ public int getBeadsTop(int place); co * 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 (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 Number FormatException if string is not correct for the base * public DynArr310
add (String value); public class DynArr310 { private static final int DEFAULT_CAPACITY = 2; private Til data; 11 12 private int size; @SuppressWarnings ("unchecked") public DynArr310() { data = (T[]) new Object (DEFAULT_CAPACITY]; size = 0; 180 @SuppressWarnings ("unchecked") public DynArr310(int initialCapacity) { if(initial Capacity size) { throw new IndexOutOfBoundsException(); 48 if(size == capacity () { setCapacity(2 * capacity()); for(int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; data[index] = value; size++; 58 59 60 61 public I get(int index) { // return the item at index // throw IndexOutOfBoundsException for invalid index // 0(1) 62 63 if(index = size) { throw new IndexOutOfBoundsException(); return data[index]; //default return, make sure to remove/change public T replace(int index, T value) { 73 74 if(index = size) { throw new IndexOutOfBounds Exception(); 75 76 77 ToldItem = data[index]; data[index] = value; 79 return oldItem; //default return, make sure to remove/change 80 81 public int firstIndexOf(T value) { 83 84 85 for(int i = 0; i = size) { throw new IndexOutOfBounds Exception(); 98 99 100 ToldItem = data[index]; for(int i = index + 1; i newCapacity) { return false; 130 131 132 133 134 135 136 T[] temp = (T[]) new Object[newCapacity]; for(int i = 0; i DynArr310 clone (DynArr310 orig) { DynArr310 copy = new DynArr310(orig.capacity()); copy.size = orig.size; for(int i = 0; i boolean isclone (DynArr310 arri, DynArr310 arr2) { // O(N) where N is the number of elements in the array 172 173 174 175 if(arri.size == arr2.size) { for(int i = 0; i nums = new DynArr310(); if((nums.size() == 0) && (nums. capacity() == 2)){ System.out.println("Yay 1"); 198 199 200 201 202 //append some numbers for(int i = 0; i msg = new DynArr310(); 215 //insert some strings msg.add(0, "world"); msg.add(0,"hello"); msg.add(1,"new"); msg.add(3,"!"); 216 217 218 219 220 221 //replace and checking if (msg.get(0).equals("hello") && msg.replace(1, "beautiful").equals("new") && msg.size() == 4 && msg. capacity() == 4){ System.out.println("Yay 3"); 222 223 224 225 226 227 //change capacity if (!msg.setCapacity (0) && !msg.setCapacity (3) && msg.setCapacity (20) && msg.capacity () == 20){ System.out.println("Yay 4"); 228 229 230 231 232 //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"); 233 234 235 //firstIndexOf and clone //remember what == does on objects... not the same as .equals() DynArr310 msgClone = DynArr310.clone(msg); if (msgClone != msg && msgClone.get(1) == msg.get(1) && msgClone.size() == msg.size() && msgClone. capacity () == msg. capacity ( ) && msgClone. firstIndexOf("world") == 1 && msgClone. firstIndexOf("beautiful") == -1) { System.out.println("Yay 6"); 1/isClone() and equals() 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2510 252 2530 254 255 256 257 258 259 260 261 262 263 //make a cat class for testing class Cat { String name; public Cat(String name) { this.name = name; public boolean equals(Object o) { if(o instanceof Cat) { return this.name.equals(((Cat)o).name); return false; 205 DynArr310 cats1 = new DynArr310(); DynArr310 cats2 = new DynArr310(); cats1.add(0, new Cat("Fred")); cats2.add(0, new Cat ("Fred")); DynArr310 cats3 = DynArr310.clone(cats2); 264 265 266 267 268 269 270 271 272 273 274 if(!DynArr310.isClone (cats1, cats2) && cats1.equals(cats2) && DynArr310.isClone( cats2, cats3) && cats2.equals(cats3)) { System.out.println("Yay 7"); 275 276 277 278 279 //and some misc items cats3.add(1, new Cat("Fred")); cats3.add(2, new Cat("Fred")); DynArr310 cats4 = DynArr310.clone(cats2); cats4.replace(0, new Cat ("Alex")); if (!DynArr310.isClone (cats2, cats3) && !cats2.equals(cats3) && !DynArr310.isClone (cats2, cats) && !cats.get(0).equals(cats.get(0)) && cats3.deleteAll(new Cat ("Fred")) == 3 && cats3.size() == 0) { System.out.println("Yay 8"); 280 281 282 283 284 285 286 287 288 289 290 291 2920 293 294 295 296 // -- // DO NOT EDIT ANYTHING BELOW THIS LINE (except to add JavaDocs) // - // this toString() method might be useful for testing public String toString() { if(size() == 0) return ""; 297 StringBuffer sb = new StringBuffer(); sb.append(get(0); for(int i = 1; i steps = a.add(value); 61 62 ---------"); 63 64 System.out.println("---- System.out.println("- Starting State"); System.out.println("---- printAbacus (steps.get(0)); ---------"); 65 67 ---------"); System.out.println("-- System.out.println("- Adding " + value); System.out.println("---- for(int i = 1; i 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) *
* * * * * 70 72 * * * @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 Number FormatException if string is not correct for the base 73 public DynArr310 add(String value); 76 } 1 2 3 // // DO NOT EDIT ANYTHING BELOW THIS LINE // 50 /** 6 * Interface for an abacus for the AbacusGUI to use. 8 90 public interface Abacus { Gets the number base of the abacus. This base will never be an odd number and never be less than 2. * w * @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(); 276 * 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 (O is the right-most place) * @return the number of beads currently in use * @throws IndexOutOfBounds Exception if the place requested is not in use */ public int getBeadsTop(int place); co * 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 (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 Number FormatException if string is not correct for the base * public DynArr310 add (String value); public class DynArr310 { private static final int DEFAULT_CAPACITY = 2; private Til data; 11 12 private int size; @SuppressWarnings ("unchecked") public DynArr310() { data = (T[]) new Object (DEFAULT_CAPACITY]; size = 0; 180 @SuppressWarnings ("unchecked") public DynArr310(int initialCapacity) { if(initial Capacity size) { throw new IndexOutOfBoundsException(); 48 if(size == capacity () { setCapacity(2 * capacity()); for(int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; data[index] = value; size++; 58 59 60 61 public I get(int index) { // return the item at index // throw IndexOutOfBoundsException for invalid index // 0(1) 62 63 if(index = size) { throw new IndexOutOfBoundsException(); return data[index]; //default return, make sure to remove/change public T replace(int index, T value) { 73 74 if(index = size) { throw new IndexOutOfBounds Exception(); 75 76 77 ToldItem = data[index]; data[index] = value; 79 return oldItem; //default return, make sure to remove/change 80 81 public int firstIndexOf(T value) { 83 84 85 for(int i = 0; i = size) { throw new IndexOutOfBounds Exception(); 98 99 100 ToldItem = data[index]; for(int i = index + 1; i newCapacity) { return false; 130 131 132 133 134 135 136 T[] temp = (T[]) new Object[newCapacity]; for(int i = 0; i DynArr310 clone (DynArr310 orig) { DynArr310 copy = new DynArr310(orig.capacity()); copy.size = orig.size; for(int i = 0; i boolean isclone (DynArr310 arri, DynArr310 arr2) { // O(N) where N is the number of elements in the array 172 173 174 175 if(arri.size == arr2.size) { for(int i = 0; i nums = new DynArr310(); if((nums.size() == 0) && (nums. capacity() == 2)){ System.out.println("Yay 1"); 198 199 200 201 202 //append some numbers for(int i = 0; i msg = new DynArr310(); 215 //insert some strings msg.add(0, "world"); msg.add(0,"hello"); msg.add(1,"new"); msg.add(3,"!"); 216 217 218 219 220 221 //replace and checking if (msg.get(0).equals("hello") && msg.replace(1, "beautiful").equals("new") && msg.size() == 4 && msg. capacity() == 4){ System.out.println("Yay 3"); 222 223 224 225 226 227 //change capacity if (!msg.setCapacity (0) && !msg.setCapacity (3) && msg.setCapacity (20) && msg.capacity () == 20){ System.out.println("Yay 4"); 228 229 230 231 232 //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"); 233 234 235 //firstIndexOf and clone //remember what == does on objects... not the same as .equals() DynArr310 msgClone = DynArr310.clone(msg); if (msgClone != msg && msgClone.get(1) == msg.get(1) && msgClone.size() == msg.size() && msgClone. capacity () == msg. capacity ( ) && msgClone. firstIndexOf("world") == 1 && msgClone. firstIndexOf("beautiful") == -1) { System.out.println("Yay 6"); 1/isClone() and equals() 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 2510 252 2530 254 255 256 257 258 259 260 261 262 263 //make a cat class for testing class Cat { String name; public Cat(String name) { this.name = name; public boolean equals(Object o) { if(o instanceof Cat) { return this.name.equals(((Cat)o).name); return false; 205 DynArr310 cats1 = new DynArr310(); DynArr310 cats2 = new DynArr310(); cats1.add(0, new Cat("Fred")); cats2.add(0, new Cat ("Fred")); DynArr310 cats3 = DynArr310.clone(cats2); 264 265 266 267 268 269 270 271 272 273 274 if(!DynArr310.isClone (cats1, cats2) && cats1.equals(cats2) && DynArr310.isClone( cats2, cats3) && cats2.equals(cats3)) { System.out.println("Yay 7"); 275 276 277 278 279 //and some misc items cats3.add(1, new Cat("Fred")); cats3.add(2, new Cat("Fred")); DynArr310 cats4 = DynArr310.clone(cats2); cats4.replace(0, new Cat ("Alex")); if (!DynArr310.isClone (cats2, cats3) && !cats2.equals(cats3) && !DynArr310.isClone (cats2, cats) && !cats.get(0).equals(cats.get(0)) && cats3.deleteAll(new Cat ("Fred")) == 3 && cats3.size() == 0) { System.out.println("Yay 8"); 280 281 282 283 284 285 286 287 288 289 290 291 2920 293 294 295 296 // -- // DO NOT EDIT ANYTHING BELOW THIS LINE (except to add JavaDocs) // - // this toString() method might be useful for testing public String toString() { if(size() == 0) return ""; 297 StringBuffer sb = new StringBuffer(); sb.append(get(0); for(int i = 1; i steps = a.add(value); 61 62 ---------"); 63 64 System.out.println("---- System.out.println("- Starting State"); System.out.println("---- printAbacus (steps.get(0)); ---------"); 65 67 ---------"); System.out.println("-- System.out.println("- Adding " + value); System.out.println("---- for(int i = 1; i 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) *
* * * * * 70 72 * * * @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 Number FormatException if string is not correct for the base 73 public DynArr310 add(String value); 76 }