Code readability (proper indentation, appropriate variable names, etc) Documenta
ID: 3883266 • Letter: C
Question
Code readability (proper indentation, appropriate variable names, etc) Documentation (programmer names, homework # and date: program and functions have brief comments describing their purpose, inline comments used as necessary) Correctness insert (no partial credit will be given) append (no partial credit will be given) remove (no partial credit will be given) moveToPos (no partial credit will be given) getValue (no partial credit will be given) printAll (no partial credit will be given) Non-Compliable CodeExplanation / Answer
package org.students;
public class IntArrayList {
private int maxSize;
private int listSize;
private int curr;
private int [] listArray;
public IntArrayList(int size) {
maxSize = size;
listSize = 0;
curr = 0;
listArray = new int[maxSize];
}
public IntArrayList( ) {
maxSize = 100;
listSize = 0;
curr = 0;
listArray = new int[maxSize];
}
public void clear() {
listSize = 0;
curr = 0;
}
// Insert an element at the current location.
// item: The element to be inserted
public void insert(int num) {
listArray[curr++]=num;
}
// Append an element at the end of the list.
// item: The element to be appended.
public void append(int num) {
listArray[listArray.length-1]=num;
}
// Remove and return the current element.
// Return: the element that was removed.
public int remove() {
/*
*
* FILL THIS PART
*
*/
int rem=listArray[curr];
for(int j=curr; j<(length()-1); j++)
{
listArray[j] = listArray[j+1];
}
return rem;
}
// Return: The current element.
public int getValue() {
/*
*
* FILL THIS PART
*
*/
return listArray[curr];
}
public void printAll() {
/*
*
* FILL THIS PART
*
*/
for(int i=0;i<listArray.length;i++)
{
System.out.print(listArray[i]+" ");
}
System.out.println();
}
// Set current position.
// pos: The position to make current.
public void moveToPos(int pos) {
/*
*
* FILL THIS PART
*
*/
curr=pos;
}
// Set curr to 0 (front)
public void moveToStart() {
curr = 0;
}
// Set currr to listSize (end)
public void moveToEnd() {
curr = listArray.length-1;
}
// move curr to the prev position
public void prev() {
if (curr > 0)
curr--;
}
// move curr to the next position
public void next() {
if (curr < listSize)
curr++;
}
// return the length of List
public int length() {
return listSize;
}
// return curr position
public int currPos() {
return curr;
}
public static void main(String[] args) {
// Testing IntArrayList
IntArrayList lst = new IntArrayList(10);
lst.printAll();
lst.insert(1);
lst.insert(2);
lst.insert(3);
lst.insert(4);
lst.insert(5);
lst.insert(6);
lst.printAll();
lst.moveToEnd();
lst.insert(10);
lst.printAll();
lst.moveToStart();
lst.insert(100);
lst.printAll();
lst.moveToPos(4);
System.out.println("array[4] = " + lst.getValue());
lst.clear();
lst.printAll();
lst.insert(12);
lst.append(0);
lst.append(1);
lst.append(2);
lst.insert(22);
lst.moveToPos(1);
lst.remove();
lst.printAll();
}
}
___________________
Output:
0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 0 0 0 0
1 2 3 4 5 6 0 0 0 10
100 2 3 4 5 6 0 0 0 10
array[4] = 5
100 2 3 4 5 6 0 0 0 10
12 22 3 4 5 6 0 0 0 2
_________________Thank You