Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I get this code to work without using the java.util Using java in netbeans please post full answer with what the ending output

How do I get this code to work without using the java.util Using java in netbeans please post full answer with what the ending output
Create a class called ManyStructures. It encapsulates an instance variable that is a long array. The length of the array should be dependent on an argument passed into a constructor. Include get and set methods so you can assign values to the elements of the array.
The ManyStructures class should also have the following methods.
getArray()-- which returns a copy of the instance variable array. Note that I said "copy"!
getQueue()-- which returns the data in the instance variable as a queue. The queue should have the functionality of the basic queue from your textbook.
getStack()-- which returns the data in the instance variable array as a stack. The stack should have the functionality of the basic stack from your textbook.
getLinkedList()-- which returns the data in the instance variable array as a linked list. The linked list should have the functionality of the basic linked list from your textbook. Note that in order to do this you will have to create something equivalent to the Link class from exercise 5A and embed the aray's long data within objects of that class.
Your project should contain any other classes you need to complete the test. Include a ManyStructuresDriver class that tests the ManyStructures class, its methods, and the data structures returned by the methods listed above..
NOTE: Just because it says "like the book" don't copy from the book, also don't use any java.util.* packages
import java.util.Arrays;
class ManyStructures {
private long[] dataArray;
public ManyStructures(int length){
dataArray = new long[length];
}
public long[] getArray(){
return dataArray.clone();
}
public void setElement(int index, long value){
if (index >=0 && index < dataArray.length){
dataArray[index]= value;
} else {
System.out.println("Index out of bounds.");
}
}
public long getElement(int index){
if (index >=0 && index < dataArray.length){
return dataArray[index];
} else {
System.out.println("Index out of bounds.");
return -1; // You can choose a suitable default value.
}
}
public Queue getQueue(){
return new Queue(dataArray.clone());
}
public Stack getStack(){
return new Stack(dataArray.clone());
}
public LinkedList getLinkedList(){
LinkedList linkedList = new LinkedList();
for (long value : dataArray){
linkedList.add(value);
}
return linkedList;
}
}
class Queue {
private long[] data;
private int front;
private int rear;
private int size;
public Queue(long[] dataArray){
this.data = dataArray.clone();
this.size = dataArray.length;
this.front =0;
this.rear = size -1;
}
// Implement the basic queue methods (enqueue, dequeue, isEmpty, isFull, etc.) here.
}
class Stack {
private long[] data;
private int top;
private int size;
public Stack(long[] dataArray){
this.data = dataArray.clone();
this.size = dataArray.length;
this.top = size -1;
}
// Implement the basic stack methods (push, pop, isEmpty, isFull, etc.) here.
}
class LinkedList {
private Node head;
public void add(long value){
Node newNode = new Node(value);
if (head == null){
head = newNode;
} else {
Node current = head;
while (current.getNext()!= null){
current = current.getNext();
}
current.setNext(newNode);
}
}
// Implement other linked list methods as needed.
}
class Node {
private long data;
private Node next;
public Node(long data){
this.data = data;
this.next = null;
}
public long getData(){
return data;
}
public Node getNext(){
return next;
}
public void setNext(Node next){
this.next = next;
}
}
class ManyStructuresDriver {
public static void main(String[] args){
// Test ManyStructures class and its methods here
int arrayLength =5;
ManyStructures manyStructures = new ManyStructures(arrayLength);
// Test getArray(), setElement(), and getElement() methods
System.out.println("Original Array: "+ Arrays.toString(manyStructures.getArray()));
manyStructures.setElement(2,100);
System.out.println("Modified Array: "+ Arrays.toString(manyStructures.getArray()));
System.out.println("Element at index 2: "+ manyStructures.getElement(2));
// Test getQueue(), getStack(), and getLinkedList() me

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

Step: 3

blur-text-image

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

Students also viewed these Databases questions