I have created a Java program that is designed to keep track of student's names, identification numbers, and GPA's, but I want to know how I can test each one of my methods given the code I have? Can someone help me? Thanks! Here is my code of what I have so far. Also, how will my instance variables come into play at when use my methods. Some of the methods that I have are the insert method, fetch method, delete, and update method.
Code:
public class StudentListings{
private String name; // key field
private int ID;
private double GPA;
private int next;
private int size;
private StudentListing[] data;
public StudentListings(){
this.name= name;
this.id= ID;
this.gpa=GPA;
}
public StudentListings(){
next=0;
data= new StudentListings[Size];
size= Size;
} // end of constructor
public boolean insert(StudentListings newStudentListing) {
if(next>=size) // the structure is full
return false;
// store a deep copy of the client's node
data[next]= new StudentListing.deepCopy();
if(data[next]== null)
return false;
next= next + 1; // prepare for the next insert
return true;
} // end of insert method
public StudentListings fetch(String targetKey){
StudentListings studentListings;
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
studentListings= data[i].deepCopy();
if(i!= 0) // bubble-up accessed node
{
temp= data[i-1];
data[i-1]=data[i];
data[i]= temp;
}
return studentListings;
} // end of fetch method
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
} // end of delete method
public boolean update(String targetKey, StudentListings newStudentListing){
if(delete(targetKey)== false) // node not in the structure
return false;
else if(insert(newStudentListing)==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 of showAll method
public static void main(String[] args){
StudentListings obj1= new StudentListings();
// how do I test each of this methods to see if they actually work?
obj1.insert();
obj1.fetch();
obj1.delete();
obj1.update();
obj1.showAll();
}
} // end of StudentListings class
Comment