Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following errors and class in Java : How are these errors fixed? Errors when testing: 1) test_append3(ThreeTenDynArrayTester) java.lang.NullPointerException: Cannot read the array length

Given the following errors and class in Java:

How are these errors fixed?

Errors when testing:

1) test_append3(ThreeTenDynArrayTester)

java.lang.NullPointerException: Cannot read the array length because "this.data" is null

at ThreeTenDynArray.append(ThreeTenDynArray.java:161)

at ThreeTenDynArrayTester.test_append3(ThreeTenDynArrayTester.java:146)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:568)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)

2) test_setCapacity1(ThreeTenDynArrayTester)

java.lang.AssertionError: Capacity failed to grow, expected capacity 8

at org.junit.Assert.fail(Assert.java:88)

at org.junit.Assert.assertTrue(Assert.java:41)

at ThreeTenDynArrayTester.test_setCapacity1(ThreeTenDynArrayTester.java:694)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:568)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)

3) test_constructor2(ThreeTenDynArrayTester)

java.lang.AssertionError: Initial capacity of the storage has incorrect initCapacity

at org.junit.Assert.fail(Assert.java:88)

at org.junit.Assert.assertTrue(Assert.java:41)

at ThreeTenDynArrayTester.test_constructor2(ThreeTenDynArrayTester.java:90)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:568)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run(FailOnTimeout.java:74)

Here is the class:

public class ThreeTenDynArray {

//default initial capacity / minimum capacity

private static final int MIN_CAPACITY = 2;

//underlying array for storage -- you MUST use this for credit!

//Do NOT change the name or type

private T[] data;

private int size = 0;

private int capacity = 0;

// ADD MORE PRIVATE MEMBERS HERE IF NEEDED!

/**

*

*/

@SuppressWarnings("unchecked")

public ThreeTenDynArray() {

// Constructor

// Initial capacity of the storage should be MIN_CAPACITY

// Hint: Can't remember how to make an array of generic Ts? It's in the textbook...

data = (T[])(new Object[MIN_CAPACITY]);

size = 0;

capacity = MIN_CAPACITY;

}

/**

* @param initCapacity

*/

@SuppressWarnings("unchecked")

public ThreeTenDynArray(int initCapacity) {

// Constructor

// Initial capacity of the storage should be initCapacity.

// - Throw IllegalArgumentException if initCapacity is smaller than

// MIN_CAPACITY 2

// - Use this _exact_ error message for the exception

// (quotes are not part of the message):

// "Capacity must be at least 2!"

if (initCapacity < MIN_CAPACITY) {

throw new IllegalArgumentException("Capacity must be at least 2!");

}

}

/**

* @return

*/

public int size() {

// Report the current number of elements

// O(1)

return size; //default return, remove/change as needed

}

/**

* @return

*/

public int capacity() {

// Report max number of elements of the current storage

// (subject to change since this is a _dynamic_ )

// O(1)

return capacity; //default return, remove/change as needed

}

/**

* @param index index you're changing

* @param value what you're adding

* @return the old item at the index

*/

public T set(int index, T value) {

// Replace the item at the given index to be the given value.

// Return the old item at that index.

// Note: You cannot add new items (i.e. cannot increase size) with this method.

// O(1)

//return firstIndexOf(value);

// - Throw IndexOutOfBoundsException if index is not valid

// - Use this code to produce the correct error message for

// the exception (do not use a different message):

// "Index: " + index + " out of bounds!"

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

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

}

// - Throw IllegalArgumentException if value is null.

// - Use this _exact_ error message for the exception

// (quotes are not part of the message):

// "Cannot include null values!"

if (value==null) {

throw new IllegalArgumentException("Cannot include null values!");

}

T oldValue = data[index];

data[index] = value;

return oldValue; //default return, remove/change as needed

}

/**

* @param index

* @return

*/

public T get(int index) {

// Return the item at the given index

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

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

}

// O(1)

// Use the exception (and error message) described in set()

// for invalid indicies.

return data[index]; //default return, remove/change as needed

}

/**

* @param value

*/

@SuppressWarnings("unchecked")

public void append(T value) {

// Append an element to the end of the storage.

// Double the capacity if no space available.

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

// For a null value, use the same exception and message

// as set().

// You can assume we will never need to grow the capacity to a value

// beyond Integer.MAX_VALUE/4. No need to check or test that boundary

// value when you grow the capacity.

// Amortized O(1)

// - Throw IllegalArgumentException if value is null.

// - Use the same error message as set().

if (value==null) {

throw new IllegalArgumentException("Cannot include null values!");

}

if (size < capacity) {

data[size] = value;

size++;

}

else{

capacity *= 2;

T[] data2 = (T[])(new Object[capacity]);

for (int i =0; i < data.length; i++) {

data2[i] = data[i];

}

data = data2;

data[size] = value;

size++;

}

}

/**

* @param index

* @param value

*/

@SuppressWarnings("unchecked")

public void insert(int index, T value) {

// Insert the given value at the given index and shift elements if needed.

// You _can_ append items with this method.

// Double capacity if no space available.

// Assume the same as append() for the upper bound of capacity.

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

// For an invalid index or a null value, use the same exception and message

// as set(). However, remember that the condition of the exception is

// different (a different invalid range for indexes).

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

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

}

for (int i = size-1; i >= index; i--) {

data[i+1] = data[i];

}

data[index] = value;

size++;

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

}

/**

* @param index

* @return

*/

@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).

// 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

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

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

}

T oldValue = data[index];

for (int i = index; i < data.length-1; i++) {

data[i] = data[i + 1];

}

size--;

if(size<=capacity/4)

{

capacity/=2;

if(capacity

capacity = MIN_CAPACITY;

}

return oldValue ; //default return, remove/change as needed

}

/**

* @param newCapacity

* @return

*/

@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).

// 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

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

return false; //default return, remove/change as needed

}

else if (newCapacity == size) {

return true;

}

return false;

}

/**

* @param value

* @return

*/

public int firstIndexOf(T value) {

// Return the index of the first occurrence of value or -1 if not found.

// NOTES: - Remember null is not a valid item in list.

// - Remember to use .equals for comparison of non-null values.

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

if (value==null) {

return -1;

}

return -1; //default return, remove/change as needed

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Declare a float variable named price.

Answered: 1 week ago