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: 3914878 • 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

Below are the answers for first part (Task 1)

//Task 1a.

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

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

Exception:           IndexOutOfBoundsException

What caused the error: There is no element at index 0, set method works only if there is an element at a given index, and then replace the item at that index with new value

How to Fix It: if you really want to use set method, first of all, you have to add a value.

arr.add(“Something”); //this will add a String to index 0 if the list is empty

arr.set(0,“This is a String”); //this will replace the item at index 0 with new String value

//Task 1b.

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

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

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

Exception:           IndexOutOfBoundsException

What caused the error: the array size is 3 before executing the arr.add(6, "Pineapple Pizza"); statement, add method takes index value from 0 to n, where n is the size of the array, here the index is greater than the size of the array.

How to Fix It: change arr.add(6, "Pineapple Pizza"); to arr.add(3, "Pineapple Pizza"); which will add the new element after the last element of the current list

//Task 1c.

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

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

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

Exception:           No exception

What caused the error: No error, it is totally valid to provide array size as the index value for add method, in that case, the new element will be added after the current last element

How to Fix It: Nothing to fix as the code is correct.

//Task 1d.

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

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

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

Exception:           Syntax error

What caused the error: getter method used to assign a value. get() method is used for returning the value at a given index, not assigning a value to an index

How to Fix It: change get() method to set() method as follows.

arr.set(1,"Blood Orange");

//Task 1e.

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

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.get(arr.size());

Exception:           IndexOutOfBoundsException

What caused the error: Trying to access item at invalid index. Valid array list indices ranges from 0 to size-1

How to Fix It: change arr.get(arr.size()); to arr.get(arr.size()-1);

//Task 1f.

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

arr.add("Apple");

arr.add("Banana");

arr.add("Carrot");

arr.remove("Potato");

Exception:          There is no exception

What caused the error: No error happens if we attempt to remove an element (by value)which is not present in the list. The system will remove it if it is found, else ignores it

How to Fix It: Nothing to fix here, but the arraylist remains unchanged.

Answer for part 2 and 3 (Task 2 and 3)

// Test.java

import java.util.ArrayList;

import java.util.Scanner;

public class Test {

    static String[] data;

    static Scanner scanner = new Scanner(System.in);

   

    /**

     * method to populate the array (task 2)

     */

    public static void populateArray() {

        //filling values

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

            System.out.print("Enter value (" + (i + 1) + "): ");

            data[i] = scanner.nextLine();

        }

      //printing array

        System.out.println("Array:");

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

            System.out.println(data[i]);

        }

    }

    /**

     * method to modify the array (task 3)

     */

    public static void changeArray() {

        //getting index

        System.out.print("Enter a valid index: ");

        int index = Integer.parseInt(scanner.nextLine());

        //validating index

        if (index >= 0 && index < data.length) {

            //displaying item at entered index

            System.out.println("Current value at index " + index + " is : " + data[index]);

            System.out.print("Do you want to change this value? (y/n): ");

            String choice = scanner.nextLine();

            if (choice.equalsIgnoreCase("y")) {

                //getting new data

                System.out.print("Enter new value: ");

                //modifying

                data[index] = scanner.nextLine();

                // displaying changed array

                System.out.println("Changed Array:");

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

                    System.out.println(data[i]);

                }

            }

        } else {

            System.out.println("Invalid index!");

        }

    }

    public static void main(String[] args) {

        System.out.print("How many values you would like to store in the array? ");

        //getting array size

        int size = Integer.parseInt(scanner.nextLine());

        //initializing array

        data = new String[size];

        //prompting and filling array (task 2)

        populateArray();

        //modifying the array (task 3)

        changeArray();

    }

}

/*OUTPUT*/

How many values you would like to store in the array? 5

Enter value (1): Oliver

Enter value (2): John

Enter value (3): Barry

Enter value (4): Diggle

Enter value (5): Catelyn

Array:

Oliver

John

Barry

Diggle

Catelyn

Enter a valid index: 2

Current value at index 2 is : Barry

Do you want to change this value? (y/n): y

Enter new value: Wally

Changed Array:

Oliver

John

Wally

Diggle

Catelyn

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