Hello Everyone,
I have broken my java program down into two classes; one is called a Student class and the other class is called an StudentListing class. I am just trying to figure out how I can correctly implement the four method operations right, so I can use them in the main method for testing purposes. Can someone please help? I am struggling. Here is my code for both classes.
public class Student{
private String name; // key field
private int ID;
private double GPA;
public Student(String name, int ID, double GPA){
this.name= name;
this.ID= ID;
this.GPA=GPA;
}
public void setName(String name){
this.name= name;
}
public String toString() {
return " Student Name: " + name + " Student ID: " + ID + "Student GPA: " + GPA;
}
public String getName() {
return name;
}
public void setID(int ID) {
this.ID= ID;
}
public int getID(){
return ID;
}
public void setGPA(double GPA){
this.GPA= GPA;
}
public double getGPA(){
return GPA;
}
}
public class StudentListings{
private Student[] data; // an array of Student objects
private int next;
private int size;
public StudentListings(){
next=0;
size=100;
data= new Student[size];
}
public StudentListings(int s){
next=0;
data= new Student[s];
size=s;
}
public boolean insert(Student newStudentListing) {
// the structure is full
if(next > = size)
return false;
// store a deep copy of the client's node
data[next]= newStudentListing.deepCopy();
if(data[next]== null)
return false;
next= next + 1; // prepare for the next insert
return true;
}
public StudentListings fetch(String targetKey){
StudentListings student;
StudentListings 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
student= data[i].deepCopy();
if(i!= 0) // bubble-up accessed node
{
temp= data[i-1];
data[i-1]=data[i];
data[i]= temp;
}
return student;
}
public boolean delete(String targetKey){
int i=0;
while(i < next && !(data[i].compareTo(targetKey)==0))
{
i++;
}
if(i==next) // node not found
// move the last node into the deleted node's position
return false;
data[i]= data[next-1];
data[next-1]=null;
next= next-1;
return true; // node found and deleted
}
public boolean update(String targetKey, Student 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 static void main(String[] args){
StudentListings obj1= new StudentListings();
Student l1 = new Student("Terrence", 1, 3.45);
Student l2 = new Student("Roberta", 2, 2.15);
Student l3 = new Student("George", 3, 1.50);
obj1.insert(l1);
obj1.insert(l2);
obj1.insert(l3);
obj1.ShowAll();
l3= obj1.fetch("Terrence");
System.out.println(l3.toString());
obj1.delete("Roberta");
obj1.ShowAll();
obj1.update("George");
obj1.ShowAll();
}