Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help removing errors from a java program. So I need help removing errors form this java program. Here is my code: package database; import

Need help removing errors from a java program. So I need help removing errors form this java program.

Here is my code:

package database; import javax.swing.JOptionPane;

public class DoublyLinkedList {//start DoublyLinkedList public static void main(String[] args) {//START MAIN UOAUtilities s = new UOAUtilities(); StudentListing f = new StudentListing();

String name; int choice; StudentListing c = new StudentListing(); SinglyLinkedList list = new SinglyLinkedList(); char ch; do { choice = Integer.parseInt(JOptionPane.showInputDialog( "Circular Doubly Linked List Operations "+ "1. insert at begining "+ "2. insert at last "+ "3. insert at stateition "+ "4. delete at stateition "+ "5. check empty "+ "6. get size "));

switch (choice) { case 1 : list.insertAtfirst()=(JOptionPane.showInputDialog("Enter integer element to insert"); break; case 2 : list.insertAtlast() = JOptionPane.showInputDialog("Enter integer element to insert"); break; case 3 : int num = Integer.parseInt(JOptionPane.showInputDialog("Enter integer element to insert")); int state = Integer.parseInt(JOptionPane.showInputDialog("Enter stateition")); if (state < 1 || state > list.getSize() ) System.out.println("Invalid stateition "); else list.insertAtstate(num, state); break; case 4 : int p; p = Integer.parseInt(JOptionPane.showInputDialog("Enter position")); if (p < 1 || p > list.getSize()) System.out.println("Invalid position "); else list.deleteAtstate(p); break; case 5 : JOptionPane.showMessageDialog(null,"Empty status = "+ list.isEmpty()); break; case 6 : JOptionPane.showMessageDialog(null,"Size = "+ list.getSize() +" "); break; default : JOptionPane.showMessageDialog(null,"INAVILD ENTRY "); break; }

}//END DO ****************************************************** package database; import javax.swing.JOptionPane; public class SinglyLinkedList {//START SinglyLinkedList CLASS private Node h;// list header private Node first; private Node last; public int size; public SinglyLinkedList() { first = null; last = null; size = 0; } public boolean isEmpty() { return first == null; } public int getSize() { return size; } public void insertAtfirst(int val) { Node ntest = new Node(val, null,null); if (first == null) { ntest.setLinklater(ntest); ntest.setLinkbefore(ntest); first = ntest; last = first; } else { ntest.setLinkbefore(last); last.setLinklater(ntest); first.setLinkbefore(ntest); ntest.setLinklater(first); first = ntest; } size++ ; } public void insertAtlast(int val) { Node ntest = new Node(val, null, null); if (first == null) { ntest.setLinklater(ntest); ntest.setLinkbefore(ntest); first = ntest; last = first; } else { ntest.setLinkbefore(last); last.setLinklater(ntest); first.setLinkbefore(ntest); ntest.setLinklater(first); last = ntest; } size++; }

public void insertAtstate(int val , int state) { Node ntest = new Node(val, null, null); if (state == 1) { insertAtfirst(val); return; } Node test = first; for (int i = 2; i <= size; i++) { if (i == state) { Node tmp = test.getLinklater(); test.setLinklater(ntest); ntest.setLinkbefore(test); ntest.setLinklater(tmp); tmp.setLinkbefore(ntest); } test = test.getLinklater(); } size++ ; } public void deleteAtstate(int state) { if (state == 1) { if (size == 1) { first = null; last = null; size = 0; return; } first = first.getLinklater(); first.setLinkbefore(last); last.setLinklater(first); size--; return ; } if (state == size) { last = last.getLinkbefore(); last.setLinklater(first); first.setLinkbefore(last); size-- ; } Node test = first.getLinklater(); for (int i = 2; i <= size; i++) { if (i == state) { Node p = test.getLinkbefore(); Node n = test.getLinklater();

p.setLinklater(n); n.setLinkbefore(p); size-- ; return; } test = test.getLinklater(); } } public void display() { System.out.println("Circular Doubly Linked List = "); Node test = first; if (size == 0) { System.out.print("empty "); return; } if (first.getLinklater() == first) { System.out.print(first.getinfo()+ " <-> "+test.getinfo()+ " "); return; } System.out.print(first.getinfo()+ " <-> "); test = first.getLinklater(); while (test.getLinklater() != first) { System.out.print(test.getinfo()+ " <-> "); test = test.getLinklater(); } System.out.print(test.getinfo()+ " <-> "); test = test.getLinklater(); System.out.print(test.getinfo()+ " ");

} }//end showAll private class Node {//satrt of inner class node private StudentListing l; private Node next; protected int info; protected Node foward, backward; public Node() {//start public Node foward = null; backward = null; info = 0; }//end public Node public Node(int d, Node n, Node p) { info = d; foward = n; backward = p; } public void setLinkfoward(Node n) { foward = n; } public void setLinkback(Node p) { backward = p; } public Node getLinkfoward() { return foward; } public Node getLinkback() { return backward; } public void setinfo(int d) { info = d; } public int getinfo() { return info; } }// end of inner class Node }//end SinglyLinkedList outer class

****************************************************** package database; import javax.swing.JOptionPane; public class StudentListing {//start StudentListing class private String name; private String idnumber; private String gpa; //private int info; //private StudentListing later, before; public StudentListing(String n, String id, String grd) {//start StudentListing name = n; idnumber = id; gpa = grd; }//end StudentListing StudentListing() { } public String toString( ) {//start toString return("Name: " + name + " ID number: " + idnumber + " GPA: " + gpa + " "); }//end toString public StudentListing deepCopy() {//start deepCopy StudentListing clone = new StudentListing(name, idnumber, gpa); return clone; }//end deepCopy public int compareTo(String targetKey) {//Start compareTo return(name.compareTo(targetKey)); }//End compareTo public void setIdnum(String id) {//Start setIdnum idnumber = id;// coded to demonstrate encapsulation }//end setIdnum public void inputNode() {//start inputNode name = JOptionPane.showInputDialog("Enter a name"); idnumber = JOptionPane.showInputDialog("Enter the ID Number"); gpa = JOptionPane.showInputDialog("Enter the GPA"); }//end inputNode public String getKey() { return name; } }//end studentListing class ****************************************************** package database; import javax.swing.*; public class UOAUtilities {//start UOAUtilities class private int next; private int size; private StudentListing[] data; public UOAUtilities() {//Start of constructor next = 0; size = 100; data = new StudentListing[size]; }//end of constructor public UOAUtilities (int s) {//Start of constructor next = 0; data = new StudentListing[s]; size = s; }//end of constructor public boolean insert(StudentListing newStudentListing) { //insert method starts if(next >= size) // if the structure is full return false; data[next]= newStudentListing.deepCopy( ); // store a deep copy of the clients node if(data[next] == null) return false; next = next + 1; // prepare for the next insert return true; }// end of insert method public StudentListing fetch(String targetKey) {//start of fetch method StudentListing node; StudentListing temp; // access the node using a sequential search int i = 0; while ( i < next && !(data[i].compareTo(targetKey) == 0)) { i++; } if(i == next) // if node not found {JOptionPane.showMessageDialog(null, "Node was not found"); return null; } //deep copy the node's information into the client's node node = data[i].deepCopy( ); // move the node up one position in the array, unless it is the first node if(i != 0) // bubble-up accessed node { temp = data[i-1]; data[i-1] = data[i]; data[i] = temp; } return node; }// end of fetch method

public boolean delete(String targetKey) {// access the node using a sequential search int i = 0; while (i < next && !(data[i].compareTo(targetKey) == 0)) { i++; } if(i == next) // if node not found { JOptionPane.showMessageDialog(null, "The node could not be delteted becasue it was not found"); }

//move the last node into the deleted node's position data[ i] = data[ next -1]; data[next-1] = null; next = next - 1; return true; // if node found and deleted }//end of the delete method

public boolean update(String targetKey, StudentListing StudentListings) {//start update method if(delete(targetKey) == false) {JOptionPane.showMessageDialog(null, "Node not in the structure, update was unsucessful"); return false; } else if( insert(StudentListings) == false) { JOptionPane.showMessageDialog(null, "Insufficient memory update was unsucessful"); return false; } else { JOptionPane.showMessageDialog(null, "The student was updated succesffuly"); return true; // node found and updated } }// end of update method public void showAll( ) {// Start showAll method for(int i = 0; i< next; i++) System.out.println( data[i].toString());//print out all data }// end showAll method }//end of class UOAUtilities ********************************************************** Also, here are the original instructions for the assignment as a guide. Please let me know if I'm doing this right as I am extremly lost at this point. Thank you.

Program 3

Sorted Doubly Linked list

Re-do Program 1(the Unsorted-Optimized Array structure) but this time store the nodes in a sorted,doublylinked list. First convert the singly linked list class to a sortedsingly linked list (70% see below). Then convert to a doubly linked (20% + 10% see below and read the footnote).

Client node definition class changes:

Add a getKey method to it..

Application class changes:

Change the class of the data structure object and, eliminate the asking of maximum number of nodes.

Data structure class SinglyLinkedList (pg 189) changes:

************************ 70% Sorted Singly linked *******************

Change the data structure class to the class on pages 189-190, and change the class name and the clients node definition class name from Listing to StudentListings

insert(sorted)

Add a getKey mehod to the StudentListings class

search list for insertion point: while (not at end of list && key < newNodesKey)

Insert a newNode and the deepCopy at insertion point

fetch and delete (sorted speed-up)

Same search as unsorted, but should return an error when the node is not in the listorwe get to a node that is greater than given key(speed-up).

***********************20% Doubly Linked insert and showAll**********

inner Node class:

Add the back pointer

insert (doubly linked):

Add the setting of the back pointer on all inserts (new last node a special case)

showAllBackward output the nodes in last to first order:

Make q trail p down the list till q points to the last node. Then use q to traverse the list backward.

********************** 10% delete*******************************

delete:

reset the back pointer on all deletes (delete last node a special case)

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

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions