Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Remember that and ArrayList is an array - based List ( ADT ) . You are implementing an ArrayList of integers. When the list is

Remember that and ArrayList is an array-based List (ADT).
You are implementing an ArrayList of integers. When the list is initialized all elements are set to zero.
When removing an element, the value of any empty elements will be reset to zero.
Can you only fill in the blanks and not touch imports?
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/**
* You need to be mindful of the capacity of your ArrayList, and may need to
* adjust it, before/after any of the following operation completes.
*/
interface IArrayList {
public int getSize();
public int getCapacity();
public int get(int index);
public void add(int value);
public void addAtIndex(int value, int index);
public void remove(int value);
public void removeAtIndex(int index);
/**
* This method will remove all the elements from your ArrayList.
*/
public void clear();
}
class ArrayList implements IArrayList {
private int[] arr;
// The maximum number of elements arr is able to hold, until resizing is
needed.
private int capacity;
// This will hold number of elements in your ArrayList. Notice it's different
// than the capacity!
private int size;
public ArrayList(int capacity){
this.capacity = capacity;
this.arr = new int[capacity];
}
public void print(){
String elements ="[";
for (int i =0; i < this.capacity; i++){
elements += this.arr[i];
if (i < this.capacity -1){
elements +=",";
}
}
elements +="]";
String summary = String.format("{ capacity: %d, size: %d, elements: %s }",
this.capacity, this.size, elements);
System.out.println(summary);
}
/***************************************************************************/
// Write your code below here...
/***************************************************************************/
// Use this as a helper method
private void doubleCapacity(){
// Write your code here...
}
// Use this as a helper method
private void halveCapacity(){
// Write your code here...
}
// implement the IArrayList.
// Write your code here...
}
class Solution {
public static void main(String[] args) throws IOException {
// input handling
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
int capacity = Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+
$","").split("=")[1].trim());
int operationCount =
Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+$","").split("=")
[1].trim());
bufferedReader.readLine();
ArrayList arrList = new ArrayList(capacity);
IntStream.range(0, operationCount).forEach(opCountItr ->{
try {
List theInput =
Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split(","))
.collect(toList());
String action = theInput.get(0);
String arg1= theInput.size()>1? theInput.get(1).trim() : null;
String arg2= theInput.size()>2? theInput.get(2).trim() : null;
String arg3= theInput.size()>3? theInput.get(3).trim() : null;
ProcessInputs(arrList, action, arg1, arg2, arg3);
} catch (IOException exception){
throw new RuntimeException(exception);
}
});
bufferedReader.close();
}
private static void ProcessInputs(ArrayList arrList, String action, String
arg1, String arg2, String arg3){
int result;
int value;
int index;
switch (action){
case "getSize":
result = arrList.getSize();
System.out.println(result);
break;
case "getCapacity":
result = arrList.getCapacity();
System.out.println(result);
break;
case "get": {
index = Integer.parseInt(arg1);
result = arrList.get(index);
System.out.println(result);
break;
}
case "add":
value = Integer.parseInt(arg1);
arrList.add(value);
break;
case "addAtIndex":
value = Integer.parseInt(arg1);
index = Integer.parseInt(arg2);
arrList.addAtIndex(value, index);
break;
case "remove":
value = Integer.parseInt(arg1);
arrList.remove(value);
break;
case "removeAtIndex":
index = Integer.parseInt(arg1);
arrList.removeAtIndex(index);
break;
case "clear":
arrList.clear();
break;
case "print":
arrList.print();
break;
}
}
}
CAPACITY=10
OPERATION_COUNT=5
add, 1
add, 2
add, 3
addAtIndex, 4,0
print
Sample Output 3
{ capacity: 10, size: 4, elements: [4,1,2,3,0,0,0,0,0,0]}

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

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

Comparing GDP among Countries

Answered: 1 week ago

Question

Explain the different types of marketing strategies.

Answered: 1 week ago

Question

Explain product positioning.

Answered: 1 week ago

Question

Explain Industrial market segment.

Answered: 1 week ago