this for java 1. In the TestStack class, define the copyStack() method, which ta
ID: 3573509 • Letter: T
Question
this for java 1. In the TestStack class, define the copyStack() method, which takes in as a parameter a Stack s and returns a new, independent copy of the Stack. That means, once the method is called and a copy is created, altering the original stack MUST NOT alter the copied stack, and vice versa. In copying your stack, you may not use any data structures other than a temporary Stack object (that means no arrays, no linked lists (except for a Stack), no random collection of integers, etc.). Write a meaningful test program in the main() method that thoroughly tests out your copyStack() method. It should demonstrate that the Stack methods work and that the copy or copies you create are in fact independent of one another (i.e., you can push/pop from one stack without altering the other). There is a writeStack() method defined in the Stack class that will print the elements of the Stack, which should be used in your main method to test your methods. 2. In the TestQueue class, define the copyQueue() method, which takes in as a parameter a MyQueue q and returns a new, independent copy of the MyQueue. That means, once the method is called and a copy is created, altering the original queue MUST NOT alter the copied queue, and vice versa. In copying your queue, you may not use any data structures other than a temporary MyQueue object (that means no arrays, no linked lists (except for a MyQueue), no random collection of integers, etc.). 2 of 2 Write a meaningful test program in the main() method that thoroughly tests out your copyQueue() method. It should demonstrate that the MyQueue methods work and that the copy or copies you create are in fact independent of one another (i.e., you can insert/remove from one queue without altering the other). There is a writeQueue() method defined in the MyQueue class that will print the elements of the MyQueue, which should be used in your main method to test your methods.
/**
* HashTable class - defines the structure for a HashTable of Record objects dividing into
* hash values from 0 to 9
*/
public class HashTable {
private static final int numHashes = 10;
//DEFINE NECESSARY PROPERTY
//Default constructor - starts the hash table as empty; also initializes each LinkedList
// contained in the array
public HashTable() {
//WRITE CODE
}
//insertRecord() - inserts Record r into the hash table into the LinkedList at the proper hash value
public void insertRecord(Record r) {
//WRITE CODE
}
//removeRecord() - removes Record r from the hash table if r exists
public void removeRecord(Record r) {
//WRITE CODE
}
//findRecord() - find Record r in the hash table and return the *Record*
public Record findRecord(Record r) {
//WRITE CODE
}
//writeHashTable() - write the data content of the entire Hash Table
// Should also print message before writing each LinkedList, stating the hash value
// corresponding to the following Records.
// For example:
// "Printing Hash Value 0:"
// [print all the records for hash value 0]
// "Printing Hash Value 1:"
// [print all the records for hash value 1] and so on
public void writeHashTable() {
//WRITE CODE
}
}
/**
* Sample solution to Assignment #14.
*/
import java.util.Scanner;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class HashTableDemo {
//TESTING method. DO NOT MODIFY
public static void main(String[] args) {
//declare and initialize the HashTable
HashTable ht = new HashTable();
//Use the generateTable() method you defined to create the HashTable
generateTable(ht);
//Write the hash table
ht.writeHashTable();
System.out.println();
System.out.println();
//For testing, there are three hard-wired records. Test the removeRecord() method
Record test1 = new Record("LeBron", "James", 41134);
Record test2 = new Record("Kevin", "Durant", 11459);
Record test3 = new Record("John", "Wall", 22418);
//Remove the three records
ht.removeRecord(test1);
ht.removeRecord(test2);
ht.removeRecord(test3);
System.out.println();
//Print hash table again. Output should show these three records are gone.
ht.writeHashTable();
}
//generateTable() - Given HashTable ht, read the contents of binary file "records.dat" and add the read in
// records to ht, checking for appropriate Exceptions
// READ THE ASSIGNMENT SHEET *CAREFULLY* FOR DETAILS ON HOW THE BINARY FILE IS STRUCTURED
public static void generateTable(HashTable ht) {
//WRITE CODE
}
}
these program are the ones that have to fill in for java
this is linked list used
/**
* LinkedList class - defines the structure for a linked list of Node objects and the
* methods used to carry out the basic linked list operations
* Modified for Assignment #14 to account for the fact that each Node contains a Record
* object instead of an int
*/
//REVIEW CAREFULLY BUT DO NOT MODIFY!!
public class LinkedList {
private Node listStart; //A linked list is referred just by the first Node of the list
//Default constructor - starts the list as empty
public LinkedList() {
listStart = null;
}
//Initializing constructor - creates a node to start the list using the value provided
public LinkedList(Record r) {
listStart = new Node(); //create a new Node to be the start
listStart.setData(r); //set the value for the start Node using the passed in parameter
listStart.setNext(null); //set the next pointer to null to signify that is the last Node
}
//newNode() - creates and returns a new node whose data is 0
public Node newNode() {
Node p = new Node();
p.setData(null);
p.setNext(null);
return p;
}
//newNode() - creates and returns a new node whose data is the parameter x
public Node newNode(Record r) {
Node p = new Node();
p.setData(r);
p.setNext(null);
return p;
}
//addFront() - creates a new node containing r AND inserts it at the *front* of the list
public void addFront(Record r) {
Node p = newNode(r);
p.setNext(listStart); //MUST set the next point of the new node to the old listStart so we don't
// lose the old list
listStart = p; //Once we create the new Node, set the new start of the LinkedList to it
}
//addRear() - creates a new node containing r AND inserts it at the *rear* of the list
public void addRear(Record r) {
Node p, q;
//Scan through the list to find the end
//Once this loop is finished, q will point to the last Node in the list
for (p = listStart, q = null; p != null; q = p, p = p.getNext());
//Invariant -- at this point p MUST be NULL, so we use it to hold a pointer to the new node
p = newNode(r);
//set the next pointer of q (the last Node) to point to our new Node
q.setNext(p);
}
//insertAfter() - Insert value r in a new node to be inserted after Node p
public void insertAfter(Record r, Node p) {
Node q = newNode(r); //create a new Node containing r
q.setNext(p.getNext()); //set the next pointer of the new Node to the Node after p
p.setNext(q); //set the next pointer of p to the new Node q
}
//isXThere() - Returns true if there is a Node in the list containing r; returns false otherwise
public boolean isXThere(Record r) {
Node p = listStart;
if (p == null) //list is empty, so we know it's not there
return false;
else {
//Scan through the list looking for Node containing x
while (p != null && (p.getData()).equals(r))
p = p.getNext();
//Invariant - at this point, either p contains x or we have gone through the entire list
// without finding it
if (p == null)
return false;
else
return true;
}
}
//find() - Return the Node containing r
public Node find(Record r) {
Node p;
//Scan through list looking for Node containing x
for (p = listStart; p != null && !(p.getData()).equals(r); p = p.getNext());
//Invariant -- at this point, either p contains x or is null (we have gone through entire list)
if (p != null)
return p;
else
return null; //Since we searched the entire list without finding it, return null
}
//removeNode() - Removes the Node containins r from the list (if it exists)
public void removeNode(Record r) {
Node p, q;
//Scan through the list to see if r is there;
// when loop is done, q will point to the Node before the Node with r
for (p = listStart, q = null; p != null && !(p.getData()).equals(r); p = p.getNext())
q = p;
//If you find it, remove it
if (p != null) {
if (q == null) //r is at the front so re-adjust pointer to remove the front Node
listStart = p.getNext();
else //splice out the Node with r from list
q.setNext(p.getNext());
System.out.println("Record: " + r + " removed.");
} else {
System.out.println("Record " + r + " not found.");
}
}
//writeLinkedList() - write the data content of every Node on the list
public void writeLinkedList() {
Node p;
System.out.println("Writing list:");
for (p = listStart; p != null; p = p.getNext())
System.out.println(p.getData());
}
//travLinkedList() - removed for assignment #14, not needed
}
this the node use
/**
* Implementation of one Node of a Linked List
* Modified for Assignment #14 so that the data is a Record object
*/
//REVIEW CAREFULLY BUT DO NOT MODIFY!
public class Node {
private Record data; //the data contained in this Node
private Node next; //pointer to the next Node in the linked list
//While we normally would define constructors, we only want to be able to create new Nodes in the
// context of a LinkedList object. Thus, we will leave only the default constructor defined here,
// leaving the setting of the data to be done when the linked list creates a new node.
//Accessors
public Record getData() {
return data;
}
public Node getNext() {
return next;
}
//Mutators
public void setData(Record r) {
data = new Record(r);
}
public void setNext(Node p) {
next = p;
}
}
and this is the record
/**
*
* Class to represent a Record containing a first name, last name, and ID#
* DO NOT MODIFY!
*/
import java.io.Serializable;
import java.util.Random;
//READ CAREFULLY, BUT DO NOT MODIFY!
public class Record implements Serializable {
private String first;
private String last;
private int ID;
//default constructor
public Record() {
first = new String("John");
last = new String("Doe");
Random generator = new Random();
ID = generator.nextInt(50000) + 1; //ID will be a random number between 1 and 50000.
}
//conversion constructor
public Record(String firstName, String lastName) {
first = firstName;
last = lastName;
Random generator = new Random();
ID = generator.nextInt(50000) + 1; //ID will be a random number between 1 and 50000.
}
//conversion constructor
public Record(String firstName, String lastName, int newID) {
first = firstName;
last = lastName;
ID = newID;
}
//copy constructor
public Record(Record otherObject) {
first = otherObject.first;
last = otherObject.last;
ID = otherObject.ID;
}
//toString() method to be used when printing
public String toString() {
return first + " " + last + " " + ID;
}
public boolean equals(Record r) {
return (first.equals(r.first) && last.equals(r.last) && (ID == r.ID));
}
//getHash() - creates a hash value for the Record based on the ID; returns value from 0 to 9
public int getHash() {
return (ID % 10);
}
}
the linked list, node and record do not have be altered or change it just the hash table and hashtable demo that have to be changed and it runs on java
Explanation / Answer
Here is the full implementation of class HashTable..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.