Question

JAVA (implementing a collection class) write a completed program using ArrayIntList and Client

last occurrence of that value, or -1 if the value is not found in the list. For example, if the list stores [1, 18, 2, 40 1,

ArrayIntList.java

public class ArrayIntList {

private int[] elementData; // list of integers

private int size; // current number of elements in the list

public static final int DEFAULT_CAPACITY = 100;

// pre : capacity >= 0

// post: constructs an empty list with the given capacity

public ArrayIntList(int capacity) {

elementData = new int[capacity];

size = 0;

}

// post: constructs an empty list of default capacity

public ArrayIntList() {

this(DEFAULT_CAPACITY);

}

// post: returns the current number of elements in the list

public int size() {

return size;

}

// pre : 0 <= index < size()

// post: returns the integer at the given index in the list

public int get(int index) {

return elementData[index];

}

// post: creates a comma-separated, bracketed version of the list

public String toString() {

if (size == 0) {

return "[]";

} else {

String result = "[" + elementData[0];

for (int i = 1; i < size; i++) {

result += ", " + elementData[i];

}

result += "]";

return result;

}

}

// post : returns the position of the first occurrence of the given

// value (-1 if not found)

public int indexOf(int value) {

for (int i = 0; i < size; i++) {

if (elementData[i] == value) {

return i;

}

}

return -1;

}

// pre : size() < capacity

// post: appends the given value to the end of the list

public void add(int value) {

elementData[size] = value;

size++;

}

// pre: size() < capacity && 0 <= index <= size()

// post: inserts the given value at the given index, shifting

subsequent

// values right

public void add(int index, int value) {

for (int i = size; i > index; i--) {

elementData[i] = elementData[i - 1];

}

elementData[index] = value;

size++;

}

// pre : 0 <= index < size()

// post: removes value at the given index, shifting subsequent values

left

public void remove(int index) {

for (int i = index; i < size - 1; i++) {

elementData[i] = elementData[i + 1];

}

size--;

}

}

Client.java

public class Client {

public static void main(String[] args) {

// construct and print list

int[] data = {13, 4, 85, 13, 40, -8, 17, -5};

ArrayIntList list = new ArrayIntList();

for (int n : data) {

list.add(n);

}

System.out.println("list = " + list);

// obtain an iterator to find the product of the list

ArrayIntListIterator i = new ArrayIntListIterator(list);

int product = 1;

while (i.hasNext()) {

int n = i.next();

product *= n;

}

System.out.println("product = " + product);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

public class ArrayIntList {

private int[] elementData; // list of integers

private int size; // current number of elements in the list

public static final int DEFAULT_CAPACITY = 100;

// pre : capacity >= 0

// post: constructs an empty list with the given capacity

public ArrayIntList(int capacity) {

elementData = new int[capacity];

size = 0;

}

// post: constructs an empty list of default capacity

public ArrayIntList() {

this(DEFAULT_CAPACITY);

}

// post: returns the current number of elements in the list

public int size() {

return size;

}

// pre : 0 <= index < size()

// post: returns the integer at the given index in the list

public int get(int index) {

return elementData[index];

}

// post: creates a comma-separated, bracketed version of the list

public String toString() {

if (size == 0) {

return "[]";

} else {

String result = "[" + elementData[0];

for (int i = 1; i < size; i++) {

result += ", " + elementData[i];

}

result += "]";

return result;

}

}

// post : returns the position of the first occurrence of the given

// value (-1 if not found)

public int indexOf(int value) {

for (int i = 0; i < size; i++) {

if (elementData[i] == value) {

return i;

}

}

return -1;

}

// pre : size() < capacity

// post: appends the given value to the end of the list

public void add(int value) {

elementData[size] = value;

size++;

}

// pre: size() < capacity && 0 <= index <= size()

// post: inserts the given value at the given index, shifting subsequent

// values right

public void add(int index, int value) {

for (int i = size; i > index; i--) {

elementData[i] = elementData[i - 1];

}

elementData[index] = value;

size++;

}

// pre : 0 <= index < size()

// post: removes value at the given index, shifting subsequent values left

public void remove(int index) {

for (int i = index; i < size - 1; i++) {

elementData[i] = elementData[i + 1];

}

size--;

}

public int indexOfSubList(ArrayIntList list) {

for (int i = 0; i < size; i++) {

if (get(i) == list.get(0)) {

boolean isOk = true;

for (int j = 1; j < list.size(); j++) {

if (get(i + j) != list.get(j))

isOk = false;

}

if (isOk)

return i;

}

}

return -1;

}

}

public class Client {

public static void main(String[] args) {

// construct and print list

int[] data = {11, -7, 3, 42, 0, 14};

ArrayIntList list = new ArrayIntList();

for (int n : data) {

list.add(n);

}

int[] data2 = {3, 42, 0};

ArrayIntList list2 = new ArrayIntList();

for(int n: data2){

list2.add(n);

}

System.out.println("list = " + list);

System.out.println("list2 = " + list2);

System.out.println(list.indexOfSubList(list2));

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final...

    JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{    private static final int DEFAULT_CAPACITY = 2;   //default initial capacity / minimum capacity    private T[] data;   //underlying array    // ADD MORE PRIVATE MEMBERS HERE IF NEEDED!       @SuppressWarnings("unchecked")    public SmartArray(){        //constructor        //initial capacity of the array should be DEFAULT_CAPACITY    }    @SuppressWarnings("unchecked")    public SmartArray(int initialCapacity){        // constructor        // set the initial capacity of...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class Sor...

    JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...

  • Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts...

    Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation...

    Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT