Question: JAVA CLASS, my output is not passing tests 5 and 6, anybody know what I can do to pass those tests? They are bolded at

JAVA CLASS, my output is not passing tests 5 and 6, anybody know what I can do to pass those tests? They are bolded at the bottom and I believe it is the two methods setCapacity and Remove which has the instructions. NO ARRAYLISTS

@SuppressWarnings("unchecked")

public T remove(int index) {

// Remove and return the element at the given index. Shift elements

// to ensure no gap. Throw an exception when there is an invalid

// index (see set(), get(), etc. above).

if (index < 0 || index >= size) {

throw new IndexOutOfBoundsException("Index: " + index + " out of bounds!");

}

T removedValue = data[index];

System.arraycopy(data, index + 1, data, index, size - index - 1);

data[--size] = null;

if (size <= data.length / 4 && data.length / 2 >= MIN_CAPACITY) {

setCapacity(data.length / 2);

}

return removedValue;

// Halve capacity (rounding down) of the storage if the number of elements falls

// below or at 1/4 of the capacity.

// However, capacity should NOT go below MIN_CAPACITY.

// Code reuse! Consider using setCapacity (see below).

// O(N) where N is the number of elements currently in the storage

//default return, remove/change as needed

}

@SuppressWarnings("unchecked")

public boolean setCapacity(int newCapacity) {

// Change the max number of items allowed before next expansion to newCapacity.

// No other changes to the current values in storage

// (i.e. they should all keep the same index).

final int MIN_CAPACITY = 1;

if (newCapacity < MIN_CAPACITY || newCapacity > size) {

T[] newData = (T[]) new Object[newCapacity];

for (int i = 0; i < size; i++) {

newData[i] = data[i];

}

this.data = newData;

size++;

return false;

}

return true;

}

// Capacity should not be changed if:

// - newCapacity is below MIN_CAPACITY; or

// - newCapacity is not large enough to accommodate current number of items

// Return false if newCapacity cannot be applied; return true otherwise.

// Special case: if newCapacity is identical to the current max number of items,

// no need to change anything but still return true.

// O(N) where N is the number of elements in the array

//default return, remove/change as needed

//******************************************************

//******* BELOW THIS LINE IS PROVIDED code *******

//******* Do NOT edit code! *******

//******* Remember to add JavaDoc *******

//******************************************************

@Override

public String toString() {

//This method is provided. Add JavaDoc and comments.

StringBuilder s = new StringBuilder("[");

for (int i = 0; i < size(); i++) {

s.append(get(i));

if (i

s.append(", ");

}

s.append("]");

return s.toString().trim();

}

//******************************************************

//******* BELOW THIS LINE IS TESTING CODE *******

//******* Edit it as much as you'd like! *******

//******* Remember to add JavaDoc *******

//******************************************************

public String toStringDebug() {

//This method is provided for debugging purposes

//(use/modify as much as you'd like), it just prints

//out the ThreeTenDynArray details for easy viewing.

StringBuilder s = new StringBuilder("ThreeTenDynArray with " + size()

+ " items and a capacity of " + capacity() + ":");

for (int i = 0; i < size(); i++) {

s.append(" ["+i+"]: " + get(i));

}

return s.toString().trim();

}

public static void main(String args[]){

//These are _sample_ tests. If you're seeing all the "yays" that's

//an excellend first step! But it does NOT guarantee your code is 100%

//working... You may edit this as much as you want, so you can add

//own tests here, modify these tests, or whatever you need!

//create a ThreeTenDynArray of integers

ThreeTenDynArray nums = new ThreeTenDynArray<>();

if((nums.size() == 0) && (nums.capacity() == MIN_CAPACITY)){

System.out.println("Yay 1");

}

//append some numbers

for(int i = 0; i < 3; i++) {

nums.append(i*3 % 2);

}

//uncomment to check details

//System.out.println(nums);

//checking

if(nums.size() == 3 && nums.get(2) == 0 && nums.capacity() == 4

&& nums.firstIndexOf(1) == 1 && nums.firstIndexOf(0) == 0

&& nums.firstIndexOf(6) == -1 ){

System.out.println("Yay 2");

}

//create a ThreeTenDynArray of strings

ThreeTenDynArray title = new ThreeTenDynArray<>();

//insert some strings

title.insert(0,"using");

title.insert(0,"problem");

title.insert(1,"solving");

title.insert(3,"c");

//checking and replace

if (title.get(0).equals("problem") && title.set(3,"java").equals("c")

&& title.size() == 4 && title.capacity() == 4){

System.out.println("Yay 3");

}

title.insert(0,"&");

title.insert(0,"structures");

title.insert(0,"data");

//uncomment to check details

//System.out.println(title);

//delete

if (title.capacity() == 8 && title.remove(5).equals("using") &&

title.remove(title.size()-1).equals("java")){

System.out.println("Yay 4");

}

//change capacity

if (!title.setCapacity(4) && !title.setCapacity(1)

&& title.setCapacity(20) && title.capacity()==20){

System.out.println("Yay 5");

}

//remove and shrinking

if (title.remove(2).equals("&") && title.capacity()==10){

System.out.println("Yay 6");

}

//exception checking

try{

title.append(null);

}

catch (IllegalArgumentException ex){

if (ex.getMessage().equals("Cannot include null values!")){

System.out.println("Yay 7");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!