Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need the correct and accurate answer to this question without using for each loop answer must be done using the uploaded project classes which

I need the correct and accurate answer to this question without using for each loop answer must be done using the uploaded project classes which i have attached in each row of the following table please use netbeans to run the demo file.

ArrayUnsortedList.java

public class ArrayUnsortedList implements ListInterface { protected final int DEFCAP = 100; // default capacity protected int origCap; // original capacity protected T[] list; // array to hold this list's elements protected int numElements = 0; // number of elements in this list protected int currentPos; // current position for iteration

// set by find method protected boolean found; // true if element found, otherwise false protected int location; // indicates location of element if found

public ArrayUnsortedList() { list = (T[]) new Object[DEFCAP]; origCap = DEFCAP; }

public ArrayUnsortedList(int origCap) { list = (T[]) new Object[origCap]; this.origCap = origCap; }

protected void enlarge() // Increments the capacity of the list by an amount // equal to the original capacity. { // Create the larger array. T[] larger = (T[]) new Object[list.length + origCap]; // Copy the contents from the smaller array into the larger array. for (int i = 0; i < numElements; i++) { larger[i] = list[i]; } // Reassign list reference. list = larger; }

protected void find(T target) // Searches list for an occurence of an element e such that // e.equals(target). If successful, sets instance variables // found to true and location to the array index of e. If // not successful, sets found to false. { location = 0; found = false;

while (location < numElements) { if (list[location].equals(target)) { found = true; return; } else location++; } }

public void add(T element) // Adds element to this list. { if (numElements == list.length) enlarge(); list[numElements] = element; numElements++; }

public boolean remove (T element) // Removes an element e from this list such that e.equals(element) // and returns true; if no such element exists, returns false. { find(element); if (found) { list[location] = list[numElements - 1]; list[numElements - 1] = null; numElements--; } return found; } public int size() // Returns the number of elements on this list. { return numElements; }

public boolean contains (T element) // Returns true if this list contains an element e such that // e.equals(element); otherwise, returns false. { find(element); return found; }

public T get(T element) // Returns an element e from this list such that e.equals(element); // if no such element exists, returns null. { find(element); if (found) return list[location]; else return null; } public String toString() // Returns a nicely formatted string that represents this list. { String listString = "List: "; for (int i = 0; i < numElements; i++) listString = listString + " " + list[i] + " "; return listString; }

public void reset() // Initializes current position for an iteration through this list, // to the first element on this list. { currentPos = 0; }

public T getNext() // Preconditions: The list is not empty // The list has been reset // The list has not been modified since the most recent reset // // Returns the element at the current position on this list. // If the current position is the last element, it advances the value // of the current position to the first element; otherwise, it advances // the value of the current position to the next element. { T next = list[currentPos]; if (currentPos == (numElements - 1)) currentPos = 0; else currentPos++; return next; } }

ListInterface.java

public interface ListInterface { int size(); // Returns the number of elements on this list.

void add(T element); // Adds element to this list.

boolean remove (T element); // Removes an element e from this list such that e.equals(element) // and returns true; if no such element exists, returns false. boolean contains (T element); // Returns true if this list contains an element e such that // e.equals(element); otherwise, returns false. T get(T element); // Returns an element e from this list such that e.equals(element); // if no such element exists, returns null. String toString(); // Returns a nicely formatted string that represents this list. void reset(); // Initializes current position for an iteration through this list, // to the first element on this list.

T getNext(); // Preconditions: The list is not empty // The list has been reset // The list has not been modified since the most recent reset // // Returns the element at the current position on this list. // If the current position is the last element, then it advances the value // of the current position to the first element; otherwise, it advances // the value of the current position to the next element. }

ArraySortedList.java

public class ArraySortedList extends ArrayUnsortedList implements ListInterface { public ArraySortedList() { super(); }

public ArraySortedList(int origCap) { super(origCap); }

public void add(T element) // Precondition: element is Comparable. // // Adds element to this list. { T listElement; int location = 0; if (numElements == list.length) enlarge();

while (location < numElements) { listElement = (T)list[location]; if (((Comparable)listElement).compareTo(element) < 0) // list element < add element location++; else break; // list element >= add element } for (int index = numElements; index > location; index--) list[index] = list[index - 1];

list[location] = element; numElements++; }

public boolean remove (T element) // Removes an element e from this list such that e.equals(element) // and returns true; if no such element exists, returns false. { find(element); if (found) { for (int i = location; i <= numElements - 2; i++) list[i] = list[i+1]; list[numElements - 1] = null; numElements--; } return found; } }

Question:

In the array-based unsorted list project uploaded to the BIO system or MS TEAMS, you are required to create a demo class called ArrayListDemo.java. In this demo class, write a static method called removeFirstOdd that receives ArrayUnsortedList of integer values. Your method should remove and return the first occurrence of an odd number, if any exist, from the given list. Keep the list unchanged and return -1 in case there is no odd numbers in the given list.

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

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

6. Vanguard

Answered: 1 week ago

Question

1. PricewaterhouseCoopers

Answered: 1 week ago