Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey I need some help modifying some java code to get a getKey method to the student listing class. Here what I need to do:

Hey I need some help modifying some java code to get a getKey method to the student listing class. Here what I need to do:

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

And heres my program that stores student information and grades:

package database; import javax.swing.JOptionPane; public class StudentListing {//start StudentListing class private String name; private String idnumber; private String gpa; 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 }//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 *****************************************************************************

package database; import javax.swing.*; public class Main {//START CLASS public static void main(String[] args) {//START MAIN UOAUtilities s = new UOAUtilities(); StudentListing f = new StudentListing(); int choice; String name; StudentListing c = new StudentListing(); do {//START DO choice = Integer.parseInt(JOptionPane.showInputDialog( "1 to insert a new student's information "+ "2 to fetch and output a student's information "+ "3 to delete a student's information "+ "4 to update a student's information "+ "5 to output all the student information in sorted order "+ "6 to quit the program")); if(choice < 1 || choice > 6) {//if out of range JOptionPane.showMessageDialog(null, "Number entered is out of range."); } switch(choice) { //start switch statement case 1: f.inputNode(); s.insert(f); break; case 2: JOptionPane.showMessageDialog(null, s.fetch(name = JOptionPane.showInputDialog("search for name:"))); break; case 3: JOptionPane.showMessageDialog(null, s.delete(name = JOptionPane.showInputDialog("student to delete"))); break; case 4: c.inputNode(); s.update(JOptionPane.showInputDialog(null, "student name to update:"), c); break; case 5: s.showAll(); break; }//end switch statement }//END DO while(choice != 6);//quit the program }//END MAIN }//END CLASS *****************************************************************************

package database; public class SinglyLinkedList {//START SinglyLinkedList CLASS private Node h; // list header public SinglyLinkedList() { h = new Node(); // dummy node h.l = null; h.next = null; } public boolean insert(StudentListing newListing) {//start insert Node n = new Node(); if(n == null) // out of memory return false; else {//start else n.next = h.next; h.next = n; n.l = newListing.deepCopy(); return true; }//end else }//end insert public StudentListing fetch(String targetKey) {//start fetch Node p = h.next; while (p != null && !(p.l.compareTo(targetKey) == 0)) { p = p.next; } if(p != null) return p.l.deepCopy(); else return null; }//end fetch public boolean delete(String targetKey) {//start delete Node q = h; Node p = h.next; while (p != null && !(p.l.compareTo(targetKey) == 0)) {//start while q = p; p = p.next; }//end while if(p != null) {//start if q.next = p.next; return true; }//end if else return false; }//end delete public boolean update(String targetKey, StudentListing newListing) {//start update if(delete(targetKey) == false) return false; else if(insert(newListing) == false) return false; return true; }//end update public void showAll() {//start showAll Node p = h.next; while (p != null) //continue to traverse the list {// start while System.out.println(p.l.toString( )); p = p.next; }//end while }//end showAll public class Node {//start of inner class node private StudentListing l; private Node next; public Node() {//start public Node }//end public Node }// end of inner class Node }//end SinglyLinkedList outer class

If you have any questions please ask and any help is appreciated.

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

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions

Question

What was the role of the team leader? How was he or she selected?

Answered: 1 week ago

Question

What were the issues and solutions proposed by each team?

Answered: 1 week ago