I recently completed this program for my class which did not perform all of the
ID: 3676725 • Letter: I
Question
I recently completed this program for my class which did not perform all of the works the professor was looking for. He is giving us the chance to try and correct any mistakes to make up for some of the lost credit.
The problem is...
The code for Classes 1 and 2 can be copied from our text's source code files then pasted into your project's classes and modified (change Node to StudentListing, …)
Class 1- The node definition class (Application dependent: Student information)
Code the class described in Exercise 19 on page 121 of our text. Name the class StudentListing. Test it and debug it.
Class 2- The data structure class (Application independent except the nodes it stores must be named StudentListings)
Use the data structure class UOAUtilities presented on page 103 of our text book, which is the optimized version of the unsorted array based structure. Modify it so that it stores objects defined in the above node definition class, StudentListing (change Node to StudentListing). You will eventually have to modify it so that it expands when it is full.
Class 3- The application class (Application dependent: Student data base at our college)
Code the application described in Exercise 21 on page 122 of your text, but use the data structure described in Class 2 above. The main method’s algorithm is given below.
// declare the data structure object and load the initial data set
// input and parsing of the maximum number of nodes, max
// declare an object in the data structure class (Class 2) called ds and pass max to its constructor
// input of the initial number of students, n
// declare one object in the node definition class (Class 1 above): sl
// in a for loop that executes n times
// invoke the StudentListing class’s input method (see pg 102) on sl
// Insert sl into the data structure by invoking the insert method on ds and pass it sl
// perform user requested operations on the data set
// output the menu to a message box
// parse the menu choice into an integer
// while (true)
// { switch statement to invoke insert, fetch, delete, update[1], showAll
// on your data structure object, or System.exit(0)
// output the menu message box
// parse the menu choice into an integer
// }
Modification: Make the data structure dynamic (expands when full) 20%
[1] If an operation fails output an appropriate error message. On a successful Insert, Delete and Update, output “Operation Complete”. On a successful Fetch operation, output use Class 2’s toSting method to output the contents of the fetched node.
Here is my coding with the teachers comments (there are 3 separate codes: 1 main class and 2 classes)
***Main Class: ***
package p1caccia;
/*
Grade = 70%: The client should not be invoking it
insert should invoke it -15%
Line 29 should not end in a ;, it should not contain !, and it should not
make a deep copy -5%
you have to let the user know if an operation was successful or what went wrong
e.g., on a delete: "Jones not in the structure" or "Jones deleted" -10%
*/
import javax.swing.JOptionPane;
public class P1Caccia
{
public static void main(String[] args)
{
String max;
max = JOptionPane.showInputDialog("How many nodes will be in the structure?");
StudentListing s = new StudentListing();
UOAUtilities ds = new UOAUtilities(Integer.parseInt(max));
int n = Integer.parseInt(JOptionPane.showInputDialog("How many students will be input?"));
for(int i = 0; i < n; i++)
{
s.input();
if(!ds.insert(s.deepCopy()));
{
System.out.println("The structure is full, please enter more nodes to continue.");
}
}
int choice = 0;
while (choice != 7)
{
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select your choice from these available options:"
+ " Enter 1 to Insert New Student Information"
+ " Enter 2 to Fetch a Student"
+ " Enter 3 to Delete a Student"
+ " Enter 4 to Update a Student"
+ " Enter 5 to Display all Students"
+ " Enter 6 to Expand the Size"
+ " Enter 7 to Exit"));
switch (choice) {
case 1:
s.input();
ds.insert(s);
break;
case 2:
s = ds.fetch(JOptionPane.showInputDialog("Please Enter the Student's Name to be Fetched: "));
System.out.println(s);
break;
case 3:
ds.delete(max);
break;
case 4:
String targetKey = JOptionPane.showInputDialog("Student to be Updated");
s.input();
ds.update(targetKey, s.deepCopy());
break;
case 5:
ds.showAll();
break;
case 6:
ds.expand();
break;
case 7:
System.exit(0);
default:
break;
}
}
}
}
***StudentListing Java Class***
package p1caccia;
import javax.swing.JOptionPane;
public class StudentListing
{
private String name;
private String idNumber;
private String gpa;
public StudentListing(String n, String i, String g)
{
name = n;
idNumber = i;
gpa = g;
}
public StudentListing()
{
this.name="";
this.idNumber="";
this.gpa="";
}
public String toString()
{
return("Student name is " + name +
" ID Number is " + idNumber +
" GPA is " + gpa + " ");
}
public StudentListing deepCopy()
{
StudentListing clone = new StudentListing(name, idNumber, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return(name.compareTo(targetKey));
}
public void input()
{
name = JOptionPane.showInputDialog("Enter a Name: ");
idNumber = JOptionPane.showInputDialog("Enter the ID Number: ");
gpa = JOptionPane.showInputDialog("Enter the GPA: ");
}
}
***UOAUtilities Class***
package p1caccia;
public class UOAUtilities
{
private int next;
private int size;
private StudentListing[] data;
public UOAUtilities()
{
next = 0;
size = 100;
data = new StudentListing[size];
}
public UOAUtilities(int s)
{
next = 0;
data = new StudentListing[s];
size = s;
}
public boolean insert(StudentListing newStudentListing)
{
if(next >= size)
return false;
data[next] = newStudentListing.deepCopy();
if(data[next] == null)
return false;
next = next + 1;
return true;
}
public StudentListing fetch(String targetKey)
{
StudentListing studentListing;
StudentListing temp;
int i = 0;
while(i < next && !(data[i].compareTo(targetKey) == 0))
{
i++;
}
if(i == next)
return null;
studentListing = data[i].deepCopy();
if(i != 0)
{
temp = data[i - 1];
data[i - 1] = data[i];
data[i] = temp;
}
return studentListing;
}
public boolean delete(String targetKey)
{
int i = 0;
while(i < next && !(data[i].compareTo(targetKey) == 0))
{
i++;
}
if(i == next)
return false;
data[i] = data[next - 1];
data[next - 1] = null;
next = next - 1;
return true;
}
public boolean update(String targetKey, StudentListing newStudentListing)
{
if(delete(targetKey) == false)
return false;
else if(insert(newStudentListing) == false)
return false;
else
return true;
}
public void showAll()
{
for(int i = 0; i < next; i++)
System.out.println(data[i].toString());
}
public boolean expand()
{
StudentListing[] temp = data;
if(data == null)
data = new StudentListing[size*2];
return false;
}
}
Thanks again!!
Explanation / Answer
package com.he.capillary.ste;
import javax.swing.JOptionPane;
public class P1Caccia {
public static void main(String[] args)
{
String max;
max = JOptionPane.showInputDialog("How many nodes will be in the structure?");
StudentListing s = new StudentListing();
UOAUtilities ds = new UOAUtilities(Integer.parseInt(max));
int n = Integer.parseInt(JOptionPane.showInputDialog("How many students will be input?"));
for (int i = 0; i < n; i++)
{
s.input();
if (!ds.insert(s.deepCopy()))
;
{
System.out.println("The structure is full, please enter more nodes to continue.");
}
}
int choice = 0;
while (choice != 7)
{
choice = Integer
.parseInt(JOptionPane.showInputDialog("Please select your choice from these available options:"
+ " Enter 1 to Insert New Student Information"
+ " Enter 2 to Fetch a Student"
+ " Enter 3 to Delete a Student"
+ " Enter 4 to Update a Student"
+ " Enter 5 to Display all Students"
+ " Enter 6 to Expand the Size"
+ " Enter 7 to Exit"));
switch (choice) {
case 1:
try {
s.input();
ds.insert(s);
JOptionPane.showMessageDialog(null, "Operation successfull.", null,
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Operation failed.", null, JOptionPane.ERROR_MESSAGE);
}
break;
case 2:
s = ds.fetch(JOptionPane.showInputDialog("Please Enter the Student's Name to be Fetched: "));
System.out.println(s);
break;
case 3:
try {
ds.delete(max);
JOptionPane.showMessageDialog(null, "Operation successfull.", null,
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Operation failed.", null, JOptionPane.ERROR_MESSAGE);
}
break;
case 4:
try {
String targetKey = JOptionPane.showInputDialog("Student to be Updated");
s.input();
ds.update(targetKey, s.deepCopy());
JOptionPane.showMessageDialog(null, "Operation successfull.", null,
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception exception) {
JOptionPane.showMessageDialog(null, "Operation failed.", null, JOptionPane.ERROR_MESSAGE);
}
break;
case 5:
ds.showAll();
break;
case 6:
ds.expand();
break;
case 7:
System.exit(0);
default:
break;
}
}
}
}
package com.he.capillary.ste;
public class UOAUtilities {
private int next;
private int size;
private StudentListing[] data;
public UOAUtilities()
{
next = 0;
size = 100;
data = new StudentListing[size];
}
public UOAUtilities(int s)
{
next = 0;
data = new StudentListing[s];
size = s;
}
public boolean insert(StudentListing newStudentListing)
{
if (next >= size) {
int count = (int) (size * .2);
StudentListing[] studentTemp = new StudentListing[count + size];
for (int i = 0; i < size; i++) {
studentTemp[i] = data[i].deepCopy();
}
data = studentTemp;
this.next = studentTemp.length;
}
data[next] = newStudentListing.deepCopy();
if (data[next] == null)
return false;
next = next + 1;
return true;
}
public StudentListing fetch(String targetKey)
{
StudentListing studentListing;
StudentListing temp;
int i = 0;
while (i < next && !(data[i].compareTo(targetKey) == 0))
{
i++;
}
if (i == next)
return null;
studentListing = data[i].deepCopy();
if (i != 0)
{
temp = data[i - 1];
data[i - 1] = data[i];
data[i] = temp;
}
return studentListing;
}
public boolean delete(String targetKey)
{
int i = 0;
while (i < next && !(data[i].compareTo(targetKey) == 0))
{
i++;
}
if (i == next)
return false;
data[i] = data[next - 1];
data[next - 1] = null;
next = next - 1;
return true;
}
public boolean update(String targetKey, StudentListing newStudentListing)
{
if (delete(targetKey) == false)
return false;
else if (insert(newStudentListing) == false)
return false;
else
return true;
}
public void showAll()
{
for (int i = 0; i < next; i++)
System.out.println(data[i].toString());
}
public boolean expand()
{
StudentListing[] temp = data;
if (data == null)
data = new StudentListing[size * 2];
return false;
}
}
Let me know if you need any change.