# Question & Answer: The main class will create an object of the BetterArray class and run it through some tests……

I have made the Node class and BetterArray class, please help with the main Test class to satisfy the following request, you can also do some change on the classes I have gaven if needed. Thanks!!

The main class will create an object of the BetterArray class and run it through some tests.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: The main class will create an object of the BetterArray class and run it through some tests……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

a. Create the object and make its size 10 elements.

b. Store a random sequence of 10 numbers in the array.

c. Read back the elements 0 thru 19 and display the values.

d. Store a random sequence of 20 numbers into the array.

e. Read back the elements 0 thru 19 and display the values.

f. Insert the number 999 immediately before element 14.

g. Read back the elements 0 thru 20 and display the values.

h. Delete element 15

i. Read back the elements 0 thru 19 and display the values.

———————————————————–

public class Node {
private int element;
Node next;
Node prev;

public Node(int v){
this.element = v;
}

public Node(){

}

public int get(){
return this.element;
}

public void set(int v){
this.element = v;
}
}

—————————————

public class BetterArray {
private Node tail;
private int noOfNodes;

//class constructor, n is the initial allocation (the starting array size).
public BetterArray(int n) {

for (int i = 0; i < n; i++) {
Node node = new Node();
if (i == 0) {
continue;
}
}

tail.next = null;
noOfNodes = n;
}
// class constructor, use default initial allocation
public BetterArray() {
}

// uses the index to access a specific array element (node) and returns the value to the caller
public int get(int index) throws IndexOutOfBoundsException {
if (index < noOfNodes) {
int tempIndex = 0;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
return temp.get();
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}
//uses the index to find the specific array element (node) and saves the data value in the node.
//If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.
public void put(int value, int index) {
if (index < noOfNodes) {
int tempIndex = 0;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
temp.set(value);
} else {
for (int i = noOfNodes; i <= index; i++) {
Node node = new Node();
}
noOfNodes = index + 1;
tail.set(value);
}
}
// uses the index to find the specific array element (node) and saves the data value in the node.
// If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.
public void insert(int value, int index) {
if (index == 0) {
Node node = new Node(value);
noOfNodes++;
} else if (index < noOfNodes) {
get(index);
Node node = new Node(value);
Prev.next = node;
node.prev = Prev;
noOfNodes++;
} else if (index == noOfNodes) {
Node node = new Node(value);
tail.next = node;
node.prev = tail;
tail = node;
noOfNodes++;
} else {
put(value, index);
}

get(index);
}

// uses the index to find the specific array element (node) and deletes it from the list.
public void delete(int index) throws IndexOutOfBoundsException {
if (index < noOfNodes) {
get(index);
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}

public void printArray(){
System.out.println(“Empty Array!”);
return;
}
while(temp != null){
System.out.print(temp.get()+” “);
temp = temp.next;
}
}
}

——————————————————————-

import java.util.*;

class Node {
private int element;
Node next;
Node prev;
public Node(int v){
this.element = v;
}
public Node(){
}
public int get(){
return this.element;
}
public void set(int v){
this.element = v;
}
}
//—————————————
class BetterArray {
private Node tail;
private int noOfNodes;
//class constructor, n is the initial allocation (the starting array size).
public BetterArray(int n) {
for (int i = 0; i < n; i++) {
Node node = new Node();
if (i == 0) {
continue;
}
}
tail.next = null;
noOfNodes = n;
}
// class constructor, use default initial allocation
public BetterArray() {
}
// uses the index to access a specific array element (node) and returns the value to the caller
public int get(int index) throws IndexOutOfBoundsException {
if (index < noOfNodes) {
int tempIndex = 0;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
return temp.get();
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}
//uses the index to find the specific array element (node) and saves the data value in the node.
//If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.
public void put(int value, int index) {
if (index < noOfNodes) {
int tempIndex = 0;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
temp.set(value);
} else {
for (int i = noOfNodes; i <= index; i++) {
Node node = new Node(value);
}
noOfNodes = index + 1;
tail.set(value);
}
}
// uses the index to find the specific array element (node) and saves the data value in the node.
// If the requested node is outside the current size of the array (linked-list), expand the list to accommodate the new element.
public void insert(int value, int index) {
if (index == 0) {
Node node = new Node(value);
noOfNodes++;
} else if (index < noOfNodes) {
get(index);
Node node = new Node(value);
Prev.next = node;
node.prev = Prev;
noOfNodes++;
} else if (index == noOfNodes) {
Node node = new Node(value);
tail.next = node;
node.prev = tail;
tail = node;
noOfNodes++;
} else {
put(value, index);
}
get(index);
}
// uses the index to find the specific array element (node) and deletes it from the list.
public void delete(int index) throws IndexOutOfBoundsException {
if (index < noOfNodes) {
get(index);
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}

public void printArray(){
System.out.println(“Empty Array!”);
return;
}
while(temp != null){
System.out.print(temp.get()+” “);
temp = temp.next;
}
System.out.println();
}
}

class Main
{
public static void main(String[] args)
{
Random r = new Random(); // Define a random object here
BetterArray ba = new BetterArray(10);
for(int i = 10; i < 20; i++)
{

ba.put(r.nextInt(1000), i); // Put random number b/w 0 and 1000
}
ba.printArray();
for(int i = 0; i < 20; i++)
{
ba.put(r.nextInt(1000), i); // Overrwrite list with random numbers
}
ba.printArray(); // print array
ba.insert(999, 12);// insert 999 at 13th location
ba.printArray();
ba.delete(14); // delete 15th location
ba.printArray();
}
}

The output of the obove program is:

0 0 0 0 0 0 0 0 0 0 457 446 26 418 111 717 482 325 59 129
31 464 407 847 494 371 517 894 114 828 966 967 473 856 657 23 972 601 87 267
31 464 407 847 494 371 517 894 114 828 966 967 999 473 856 657 23 972 601 87 267
31 464 407 847 494 371 517 894 114 828 966 967 999 473 657 23 972 601 87 267

You may get different output form mine because random number generator is used in code.