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.

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 head;
private Node tail;
private int noOfNodes;
private int recAddNodeIndex;
private Node recAddNode;

//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) {
head = node;
head.prev = null;
recAddNode = head;
continue;
}
recAddNode.next = node;
node.prev = recAddNode;
recAddNode = node;
}

tail = recAddNode;
tail.next = null;
noOfNodes = n;
recAddNodeIndex = n – 1;
}
// class constructor, use default initial allocation
public BetterArray() {
head = new Node();
head.next = null;
head.prev = null;
tail = head;
recAddNodeIndex = 1;
recAddNode = head;
}

// 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;
Node temp = head;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
recAddNodeIndex = index;
recAddNode = temp;
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;
Node temp = head;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
recAddNode = temp;
recAddNodeIndex = index;
temp.set(value);
} else {
recAddNode = tail;
for (int i = noOfNodes; i <= index; i++) {
Node node = new Node();
recAddNode.next = node;
node.prev = recAddNode;
recAddNode = node;
}
tail = recAddNode;
recAddNodeIndex = index;
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);
node.next = head;
head = node;
head.prev = null;
noOfNodes++;
} else if (index < noOfNodes) {
get(index);
Node Prev = recAddNode.prev;
Node node = new Node(value);
Prev.next = node;
node.next = recAddNode;
node.prev = Prev;
recAddNode.prev = node;
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);
recAddNode.prev.next = recAddNode.next;
recAddNode.next.prev = recAddNode.prev;
recAddNode.prev = null;
recAddNode.next = null;
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}

 

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

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

Expert Answer

 

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 head;
private Node tail;
private int noOfNodes;
private int recAddNodeIndex;
private Node recAddNode;
//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) {
head = node;
head.prev = null;
recAddNode = head;
continue;
}
recAddNode.next = node;
node.prev = recAddNode;
recAddNode = node;
}
tail = recAddNode;
tail.next = null;
noOfNodes = n;
recAddNodeIndex = n – 1;
}
// class constructor, use default initial allocation
public BetterArray() {
head = new Node();
head.next = null;
head.prev = null;
tail = head;
recAddNodeIndex = 1;
recAddNode = head;
}
// 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;
Node temp = head;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
recAddNodeIndex = index;
recAddNode = temp;
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;
Node temp = head;
while (tempIndex < index) {
temp = temp.next;
tempIndex++;
}
recAddNode = temp;
recAddNodeIndex = index;
temp.set(value);
} else {
recAddNode = tail;
for (int i = noOfNodes; i <= index; i++) {
Node node = new Node(value);
recAddNode.next = node;
node.prev = recAddNode;
recAddNode = node;
}
tail = recAddNode;
recAddNodeIndex = index;
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);
node.next = head;
head = node;
head.prev = null;
noOfNodes++;
} else if (index < noOfNodes) {
get(index);
Node Prev = recAddNode.prev;
Node node = new Node(value);
Prev.next = node;
node.next = recAddNode;
node.prev = Prev;
recAddNode.prev = node;
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);
recAddNode.prev.next = recAddNode.next;
recAddNode.next.prev = recAddNode.prev;
recAddNode.prev = null;
recAddNode.next = null;
} else {
throw new IndexOutOfBoundsException(“Index should be less than the number of nodes.”);
}
}

public void printArray(){
Node temp = head;
if(head.next == null){
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.

Still stressed from student homework?
Get quality assistance from academic writers!