Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 1: What kind of errors are is caused by the following code, if any? How wou

ID: 3914459 • Letter: T

Question

Task 1: What kind of errors are is caused by the following code, if

any?

How would we fix them?

Note: Some errors may be caused before

execution (before you run

the program). In this case, list the

error written in NetBeans

when you hover over the problem.

//Task 1a.

ArrayList<String> arr = new ArrayList<>();

arr.set(0, "This is a String");

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

---------------------------------------------

-----------------------

//Task 1b.

ArrayList<String> arr = new ArrayList<>();

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.add(6, "Pineapple Pizza");

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

---------------------------------------------

-----------------------

//Task 1c.

ArrayList<String> arr = new ArrayList<>();

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.add(3, "Regular Pizza");

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

---------------------------------------------

-----------------------

//Task 1d.

ArrayList<String> arr = new ArrayList<>();

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.get(1) = "Blood Orange";

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

---------------------------------------------

-----------------------

//Task 1e.

ArrayList<String> arr = new ArrayList<>();

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.get(arr.size());

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

---------------------------------------------

-----------------------

//Task 1f.

ArrayList<String> arr = new ArrayList<>();

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.remove("Potato");

Exception:__________________________________________________________

What caused the

error:______________________________________________

How to Fix

It:______________________________________________________

FOR TASKS TWO AND THREE YOU MAY MODIFY YOUR PREVIOUS ARRAY LAB.

Task 2:

Have the user store values in an array.

2a. Comment out all code from task 1.

2b. In the same Java project, begin by asking the user

how many values

they would like to store into an array.

2c. Initialize a global String array "data" using the

input as size.

2d. Create a method called "populateArray" which will

contain a loop

that asks the user to input each value and

store them within the

global array "data".

2e. Use a loop to print the contents of the changed

array.

Task 3:

Have the user access or modify a value in the array.

3a. Create a method called "changeArray" which will ask

the user for an

index (location) in the array.

3b. Print out the value stored in the global array at the

index

specified by the user .

3c. Ask the user if they would like to change that value.

If so, get a

value from the user and modify "data" at the

specified index to

match the user's input.

3d. Use a loop to print the contents of the changed

array.

Explanation / Answer

Task 1 a.

Exception: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

What caused the error: We are trying the set the 0th index element in arr which does not exist.

How to Fix It: First add atleast one element to the list and then set the 0th index.

arr.add("This is a string");

Task 1b)

Exception: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 3

What caused the error: There are total 3 elements in the array and we are trying to add an element at 6th index which does not exist.

How to Fix It: Replace arr.add(6, "Pineapple Pizza") with arr.add("Pineapple Pizza");

Task 1 c)

Exception: No exception

What caused the error: No error.

How to Fix It: No fix required.

Task 1 d)

Exception: The left-hand side of an assignment must be a variable.

What caused the error:        arr.get(1) = "Blood Orange"; You cannot use arr.get(1) on the left hand side.

How to Fix It: Replace it with arr.set(1, "Blood Orange");

Task 1 e)

Exception: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

What caused the error:        arr.get(arr.size()); The arr size is 3 but the last index is 2 (arr.size()-1).

How to Fix It: Replace it with arr.get(arr.size()-1);

Task 1 f)

Exception: No exception

What caused the error: No error.

How to Fix It: No fix required.

NOTE: Due to Chegg policy, I can answer only 4 questions in a single post. Kindly post remaining questions separately and I will try to answer them.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote