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 (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...

  • Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies...

    Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....

  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

    Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...

  • 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 Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • 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...

  • 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 // 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: 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...

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