SlideShare a Scribd company logo
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.ja
va:77)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.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:7
4)
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.ja
va:77)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.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:7
4)
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.ja
va:77)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso
rImpl.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:7
4)
Here is the class:
public class ThreeTenDynArray<T> {
//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<MIN_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
}
Ad

More Related Content

Similar to Given the following errors and class in Java- How are these errors fix.pdf (20)

Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
f3apparelsonline
 
Exception
ExceptionException
Exception
Sandeep Chawla
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
adinathassociates
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
fashionscollect
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
in c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdfin c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdf
stopgolook
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
ajoy21
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Sriram Raj
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docxMETA-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
andreecapon
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
kvangork
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
kvangork
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
f3apparelsonline
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
adinathassociates
 
Should be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdfShould be in JavaInterface Worker should extend Serializable from .pdf
Should be in JavaInterface Worker should extend Serializable from .pdf
fashionscollect
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
arihantpatna
 
in c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdfin c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdf
stopgolook
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
ajoy21
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Sriram Raj
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docxMETA-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
META-INFMANIFEST.MFManifest-Version 1.0.classpath.docx
andreecapon
 
Object-Oriented JavaScript
Object-Oriented JavaScriptObject-Oriented JavaScript
Object-Oriented JavaScript
kvangork
 
Object-Oriented Javascript
Object-Oriented JavascriptObject-Oriented Javascript
Object-Oriented Javascript
kvangork
 

More from NicholasflqStewartl (20)

he amount of income taxesThe amount of income taxes A- the corporation.pdf
he amount of income taxesThe amount of income taxes A- the corporation.pdfhe amount of income taxesThe amount of income taxes A- the corporation.pdf
he amount of income taxesThe amount of income taxes A- the corporation.pdf
NicholasflqStewartl
 
Having a hard time with this one- Fill in the blanks with the follow.pdf
Having a hard time with this one-   Fill in the blanks with the follow.pdfHaving a hard time with this one-   Fill in the blanks with the follow.pdf
Having a hard time with this one- Fill in the blanks with the follow.pdf
NicholasflqStewartl
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
 
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdfHarriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
NicholasflqStewartl
 
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdfHardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
NicholasflqStewartl
 
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdfGovernment and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
NicholasflqStewartl
 
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdfHammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
NicholasflqStewartl
 
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdfHacer un programa en c++ que lea la frase y determine que caracteres s.pdf
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
NicholasflqStewartl
 
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdfGiven the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
NicholasflqStewartl
 
Grouper Corporation is authorized to issue both preferred and commonst.pdf
Grouper Corporation is authorized to issue both preferred and commonst.pdfGrouper Corporation is authorized to issue both preferred and commonst.pdf
Grouper Corporation is authorized to issue both preferred and commonst.pdf
NicholasflqStewartl
 
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdfGuessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
NicholasflqStewartl
 
Given the following XML fragment- what XPath expression would select a.pdf
Given the following XML fragment- what XPath expression would select a.pdfGiven the following XML fragment- what XPath expression would select a.pdf
Given the following XML fragment- what XPath expression would select a.pdf
NicholasflqStewartl
 
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdfGreener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
NicholasflqStewartl
 
Given the following for the Titan Company- the company began operation.pdf
Given the following for the Titan Company- the company began operation.pdfGiven the following for the Titan Company- the company began operation.pdf
Given the following for the Titan Company- the company began operation.pdf
NicholasflqStewartl
 
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
Given Information #1- Period 1 is when Devah is working and earning mo.pdfGiven Information #1- Period 1 is when Devah is working and earning mo.pdf
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
NicholasflqStewartl
 
Give concise and substantial answers by relating your answers to your.pdf
Give concise and substantial answers by relating your answers to your.pdfGive concise and substantial answers by relating your answers to your.pdf
Give concise and substantial answers by relating your answers to your.pdf
NicholasflqStewartl
 
Given a stream of strings- remove all empty strings- import java-uti.pdf
Given a stream of strings- remove all empty strings-   import java-uti.pdfGiven a stream of strings- remove all empty strings-   import java-uti.pdf
Given a stream of strings- remove all empty strings- import java-uti.pdf
NicholasflqStewartl
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdf
NicholasflqStewartl
 
Given a string- does -oce- appear in the middle of the string- To defi.pdf
Given a string- does -oce- appear in the middle of the string- To defi.pdfGiven a string- does -oce- appear in the middle of the string- To defi.pdf
Given a string- does -oce- appear in the middle of the string- To defi.pdf
NicholasflqStewartl
 
Give examples of species interactions that might be recognizable in th.pdf
Give examples of species interactions that might be recognizable in th.pdfGive examples of species interactions that might be recognizable in th.pdf
Give examples of species interactions that might be recognizable in th.pdf
NicholasflqStewartl
 
he amount of income taxesThe amount of income taxes A- the corporation.pdf
he amount of income taxesThe amount of income taxes A- the corporation.pdfhe amount of income taxesThe amount of income taxes A- the corporation.pdf
he amount of income taxesThe amount of income taxes A- the corporation.pdf
NicholasflqStewartl
 
Having a hard time with this one- Fill in the blanks with the follow.pdf
Having a hard time with this one-   Fill in the blanks with the follow.pdfHaving a hard time with this one-   Fill in the blanks with the follow.pdf
Having a hard time with this one- Fill in the blanks with the follow.pdf
NicholasflqStewartl
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
 
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdfHarriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
NicholasflqStewartl
 
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdfHardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
NicholasflqStewartl
 
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdfGovernment and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
NicholasflqStewartl
 
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdfHammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
NicholasflqStewartl
 
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdfHacer un programa en c++ que lea la frase y determine que caracteres s.pdf
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
NicholasflqStewartl
 
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdfGiven the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
NicholasflqStewartl
 
Grouper Corporation is authorized to issue both preferred and commonst.pdf
Grouper Corporation is authorized to issue both preferred and commonst.pdfGrouper Corporation is authorized to issue both preferred and commonst.pdf
Grouper Corporation is authorized to issue both preferred and commonst.pdf
NicholasflqStewartl
 
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdfGuessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
NicholasflqStewartl
 
Given the following XML fragment- what XPath expression would select a.pdf
Given the following XML fragment- what XPath expression would select a.pdfGiven the following XML fragment- what XPath expression would select a.pdf
Given the following XML fragment- what XPath expression would select a.pdf
NicholasflqStewartl
 
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdfGreener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
NicholasflqStewartl
 
Given the following for the Titan Company- the company began operation.pdf
Given the following for the Titan Company- the company began operation.pdfGiven the following for the Titan Company- the company began operation.pdf
Given the following for the Titan Company- the company began operation.pdf
NicholasflqStewartl
 
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
Given Information #1- Period 1 is when Devah is working and earning mo.pdfGiven Information #1- Period 1 is when Devah is working and earning mo.pdf
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
NicholasflqStewartl
 
Give concise and substantial answers by relating your answers to your.pdf
Give concise and substantial answers by relating your answers to your.pdfGive concise and substantial answers by relating your answers to your.pdf
Give concise and substantial answers by relating your answers to your.pdf
NicholasflqStewartl
 
Given a stream of strings- remove all empty strings- import java-uti.pdf
Given a stream of strings- remove all empty strings-   import java-uti.pdfGiven a stream of strings- remove all empty strings-   import java-uti.pdf
Given a stream of strings- remove all empty strings- import java-uti.pdf
NicholasflqStewartl
 
Given an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdfGiven an IntNode struct and the operating functions for a linked list-.pdf
Given an IntNode struct and the operating functions for a linked list-.pdf
NicholasflqStewartl
 
Given a string- does -oce- appear in the middle of the string- To defi.pdf
Given a string- does -oce- appear in the middle of the string- To defi.pdfGiven a string- does -oce- appear in the middle of the string- To defi.pdf
Given a string- does -oce- appear in the middle of the string- To defi.pdf
NicholasflqStewartl
 
Give examples of species interactions that might be recognizable in th.pdf
Give examples of species interactions that might be recognizable in th.pdfGive examples of species interactions that might be recognizable in th.pdf
Give examples of species interactions that might be recognizable in th.pdf
NicholasflqStewartl
 
Ad

Recently uploaded (20)

MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
Ad

Given the following errors and class in Java- How are these errors fix.pdf

  • 1. 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.ja va:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso rImpl.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:7 4) 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)
  • 2. 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.ja va:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso rImpl.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:7 4) 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.ja va:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccesso rImpl.java:43)
  • 3. 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:7 4) Here is the class: public class ThreeTenDynArray<T> { //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
  • 4. // 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!"); } } /**
  • 5. * @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.
  • 6. // 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 } /**
  • 7. * @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().
  • 8. // 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++; }
  • 9. } /** * @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++;
  • 10. // 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]; }
  • 11. size--; if(size<=capacity/4) { capacity/=2; if(capacity<MIN_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,
  • 12. // 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
  • 13. }
  翻译: