Use JAVA language.
public class DynamicArray2 {
private String[] data; // the backing array
private int virtualArrayLength; // the number of elements in the dynamic array
// Throws an IndexOutOfBoundsException if i is not a valid index
// for adding to the dynamic array, otherwise inserts s at index i.
// Elements can be added from index 0 to this.size().
public void add(int i, String s) {
// If there is no room for s in data, create a new array
// that is twice as long as data. Copy the contents of data
// over to this new array. Set data (the reference to the
// backing array) to this new array.
// Shift the items in data at indexes starting at i up by one,
// to make room for s at index i.
// HINT: Try this on paper first
// Add s at index i.
// Update virtualArrayLength.
// (DO NOT create a new array each time this method is called.
// Do so ONLY when the capacity of data in the backing store is exceeded,
// which will happen infrequently.)
}
// Throws an IndexOutOfBoundsException if i is not a valid index
// of the dynamic array, otherwise removes the element at index i
// and shifts the elements after i down one to fill in the gap.
public void remove(int i) {
// DO NOT create a new array anywhere in this method.
// Instead, shift the items in data from indexes i+1 on
// down by one, to overwrite the "removed" string at index i.
}
}
public class DynamicArray2 {
private String[] data; // the backing array
private int virtualArrayLength; // the number of elements in the dynamic array
// Throws an IndexOutOfBoundsException if i is not a valid index
// for adding to the dynamic array, otherwise inserts s at index i.
// Elements can be added from index 0 to this.size().
public void add(int i, String s) {
// If there is no room for s in data, create a new array
// that is twice as long as data. Copy the contents of data
// over to this new array. Set data (the reference to the
// backing array) to this new array.
// Shift the items in data at indexes starting at i up by one,
// to make room for s at index i.
// HINT: Try this on paper first
// Add s at index i.
// Update virtualArrayLength.
// (DO NOT create a new array each time this method is called.
// Do so ONLY when the capacity of data in the backing store is exceeded,
// which will happen infrequently.)
if(data.length == virtualArrayLength) {
String[] newArr = new String[virtualArrayLength*2];
for(int j=0; j<virtualArrayLength; j++)
newArr[i] = data[i];
data = newArr;
}
int j = virtualArrayLength;
while(j > i) {
data[virtualArrayLength] = data[i-1];
j--;
}
virtualArrayLength++;
data[j] = s;
}
// Throws an IndexOutOfBoundsException if i is not a valid index
// of the dynamic array, otherwise removes the element at index i
// and shifts the elements after i down one to fill in the gap.
public void remove(int i) {
// DO NOT create a new array anywhere in this method.
// Instead, shift the items in data from indexes i+1 on
// down by one, to overwrite the "removed" string at index i.
if( i < 0 || i >= virtualArrayLength)
throw new IndexOutOfBoundsException();
for(int j=i; j<virtualArrayLength-1; j++)
data[j] = data[j+1];
virtualArrayLength--;
}
}
Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
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...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...
import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...
JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) { if (index < 0 || index > numItems) { System.out.println("Get error: Index " + index + " is out of bounds."); return null; } return array[index]; } Hint: Update both the method signature and the method body! 2. Update the below method to include a try-catch block rather than throwing the exception to...
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
public class Fish {
private String species;
private int size;
private boolean hungry;
public Fish() {
}
public Fish(String species, int size) {
this.species = species;
this.size = size;
}
public String getSpecies() {
return species;
}
public int getSize() {
return size;
}
public boolean isHungry() {
return hungry;
}
public void setHungry(boolean hungry) {
this.hungry = hungry;
}
public String toString() {
return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species;
}
}Define a class called Lake that defines the following private...
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...
Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....
USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...