Question: Hi, I have to modify a data structure program that holds names grades and id numbers to what looks like to be a queue. Here
Hi, I have to modify a data structure program that holds names grades and id numbers to what looks like to be a queue. Here is the question:
Write an application to be used by the Registrar and the Dean at the end of the semester to review student GPAs contained in StudentListing nodes. The registrar will place student records (objects) in the data base and the Dean will review them in the order that they arrive. Once they are review by the Dean, they are no longer needed.
Add the class Queue (shown below) to your project. Then change the data structure used by the client code to a queue (an instance of this class) and change its menu prompts in main and the methods it invokes to reflect the fact that the data structure is now a queue.
Queue class:
package queueprogram;
public class Queue
{ private StudentListing[] data;
private int size;
private int numOfNodes;
private int front;
private int rear;
public Queue()
{ size = 100;
numOfNodes = 0;
front = 0;
rear = 0;
data = new StudentListing[100];
}
public Queue(int n)
{ size = n;
numOfNodes = 0;
front = 0;
rear = 0;
data = new StudentListing[n];
}
public boolean enque(StudentListing newNode)
{ if(numOfNodes == size)
return false; // ** overflow error **
else
{ numOfNodes = numOfNodes +1;
data[rear] = newNode.deepCopy();
rear = (rear +1) % size;
return true; // push operation successful
}
}
public StudentListing deque()
{ int frontLocation;
if(numOfNodes == 0)
return null; // ** underflow error **
else
{ frontLocation = front;
front = (front + 1) % size;
numOfNodes = numOfNodes -1;
return data[frontLocation];
}
}
public void showAll()
{ int i = front;
for(int c = 1; c <= numOfNodes; c++)
{ System.out.println(data[i].toString( ));
i = (i + 1) % size;
}
}
}
**********************************************************************
main
package queueprogram;
import javax.swing.*;
public class queueprogram
{
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
}
**********************************************************************
Node Definition Class (StudentListing)
package queueprogram;
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)
{
name = n;
idnumber = id;
gpa = grd;
}
StudentListing()
{
}
public String toString( )
{
return("Name: " + name +
" ID number: " + idnumber +
" GPA: " + gpa + " ");
}
public StudentListing deepCopy()
{
StudentListing clone = new StudentListing(name, idnumber, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return(name.compareTo(targetKey));
}
public void setIdnum(String id)
{
idnumber = id;// coded to demonstrate encapsulation
}
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 queueprogram;
public class UOAUtilities
{ private int next;
private int size;
private StudentListing[ ] data;
public UOAUtilities ( )
{ next = 0;
size = 100;
data = new StudentListing[size];
}//end of constructor
public UOAUtilities (int s)
{ next = 0;
data = new StudentListing[s];
size = s;
}//end of constructor
public boolean insert(StudentListing newNode)
{ if(next >= size) // the structure is full
return false;
data[next]= newNode.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)
{ 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) // node 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) // node not found
return false;
//move the last node into the deleted node's position
data[ i] = data[ next -1];
data[next-1] = null;
next = next - 1;
return true; // node found and deleted
}//end of the delete method
public boolean update(String targetKey, StudentListing newNode)
{ if(delete(targetKey) == false) // node not in the structure
return false;
else if( insert(newNode ) == false) // insufficient memory
return false;
else
return true; // node found and updated
}// end of update method
public void showAll( )
{ for(int i = 0; i< next; i++)
System.out.println(data[i].toString( ));
}// end showAll method
}//end of class UOAUtilities
**********************************************************************
Any help is appreciated Thank You
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
