Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this java code. 9.11 *zyLab: BasicArrayLists Basic ArrayLists Wr_ite this program using an IDE. Comment and style the code according to

I need help with this java code.

9.11 *zyLab: BasicArrayLists

Basic ArrayLists

Wr_ite this program using an IDE. Comment and style the code according to CS 200 Style Guide. Submit the source code file (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct.

The goal of this exercise is to help you become familiar with ArrayLists. ArrayLists are resizable data structures which can dynamically expand or contract, unlike Arrays.

We will perform some basic operations on ArrayLists:

Read input using a Scanner instance and then perform an operation on an ArrayList depending on the input.

Each line of input has 2 or 3 numbers. The first number is a command {0, 1, 2, 3}. Perform an action based on the command. All inputs are integers.

Commands:

0 : Add an element to end of ArrayList. (For example, the input 0 7 means add the element 7 to the end of the ArrayList.)

1 : Reset the element at given index. (For example, 1 4 5 means reset index 4 to be the element 5, if it exists.)

2 : Remove an element at a particular index. (For example, 2 6 means remove the element at index 6, if it exists.)

3: Print the contents of the ArrayList and exit the program.

Examples of input/output are shown below:

Input:

0 1 0 3 0 2 0 5 2 3 2 1 1 1 3 3

Output:

1 3

Note: Modify the ArrayList only if the operation is possible. You can assume that the commands are always one of {0,1,2,3}, and there is always a "3" command to exit.

Input:

0 12 0 32 2 3 1 4 4 0 8 2 1 3 0 6 0 3 1 1 4 3

Output:

12 8

Before turning in, remember to Style and Comment following the course standards in the CS 200 Style Guide.

Created by: Himanshu Pandotra and Anna Lubienski (Fall 2022)

Code provided:

import java.util.ArrayList; import java.util.Scanner; /** * This class has methods to perform some basic operations * on Arraylists. */ public class BasicArrayLists { /** The main method creates an ArrayList. * Reads commands from the input, one at a time * and performs ('add': 0) or ('set': 1) * or ('remove': 2) operations on an ArrayList. * On ('exit': 3) Prints the contents of the ArrayList * and exit the program. * @param args (unused) */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); /**Enter your code here * Create an ArrayList and read inputs from scanner */ return; } }

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago