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 

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.

Recommended Answers

All 19 Replies

There seems to be a fundamental problem with this code.
There are two kinds of things in the application, students, and a student listing.
You have tried to use the same class to represent both, and that's not going to work (or, even if it does work it still won't make any sense!)

A Student has a ID, a name, and a GPA
A Student Listing has a list of Students with a max and current size, and methods to add Students (etc).

So the first thing is fix that by splitting your code into those two classes.

Then you can create a public static void main method somewhere that creates some Students, creates a StudentList and addes the Students to the list, removes some, (etc) for testing

@ JamesCherrill, So create a Student class with instances variables and access modifer methods like set and get? Then create an instance of that class (Student obj1= new Student();). Then create another class named StudentListings that extends Students? In the StudentLisitngs class, would I then have all showAll() and four operation methods? Do you think you can provide an example? Just a pseudocode example of how this could be done? Thanks! I will update my code in a few hours!

That's right, except that StudentList does NOT extend Student. The extends relationship means "is a kind of" as in Cat extends Mammal means Cat is kind of Mammal. A student list is not a kind of student.
To help decide which methods go in which class, ask youself "is it reasonable to ask this object to do that?"
eg Is it reasonable to ask a list to add a new student? (yes). Show all the Students in the list? (yes). Is it reasonable to ask a student to fetch another student from a list? (No)

So some pseudo code...

define Student class

define StudentList class

main method:
   create an instance of StudentList
   create a few Students
   add them to the list
   list.showAll() // should show the students you added
   remove a student from the list
   list.showAll() // should show the students you added less the one you removed
   (etc until you have tested all the methods)
public class Student{

     private String name;   // key field 

     private int ID; 

     private double GPA; 

   public Student(name, ID, 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; 

   }

}

@ JamesCherrill I created the Student class, but when I create a StudentLisitng will I only have showAll(), insert, delete, update, and fetch methods just in that class? I wouldn't have to add the fields again from the Student class into the StudentListing class, right?

when I create a StudentLisitng will I only have showAll(), insert, delete, update, and fetch methods just in that class?

Yes

I wouldn't have to add the fields again from the Student class into the StudentListing class

Definitely not
Now you have a Student class your StudentList can just insert Student objects without having to know anything about the fields inside Student. And itt can showAll by just using each Student's toString() method without knowing anything about the individual fields.
But for delete/update/fetch the StudentList will have to know about the method to get the Student's key field because it uses that to find a particular student. (You said that the students name is the key, but names can have duplicates... it would be more usual to use the unique ID as the key).

public class Student{

     private String name;   // key field 

     private int ID; 

     private double GPA; 

   public Student(name, ID, 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; 

     }

   Student obj1= new Student(); 
   Student obj2= new Student(); 
   Student obj3= new Student(); 

   obj1.SetName("Terrence"); 
   obj1.SetID(1); 
   obj1.SetGPA(3.73); 

   obj2.SetName("Bobby"); 
   obj2.SetID(2); 
   obj2.SetGPA(3.47); 

   obj3.SetName("Johhnson"); 
   obj3.SetID(3); 
   obj3.SetGPA(3.79); 

}

@JamesCherrill I also created the objects inside of the class as well. Is that okay, because I figured that you should have already created the objects before you can perform operations on them in the StudentListing class.

Yes, you do need to create objects before you can perform operations on them.

Where to create them? Not in the Student class. The whole idea is that Student is something you can re-use anywhere that a programmer needs to deal with students. You can't do that if the class creates an arbitrary set of Students.
You can say the same about StudentList.

I think the best thing is to let each of those classes do just what it needs to do, and have third class eg StudentTester that has the job of testing them.

In your code above you put the test code (lines 5-19) simply inside the Student class definition. You can't do that. Executable code like that needs to be inside a method (not strictly true, but let's keep it simple for now).
Remember also that every Java program must have a
public static void main(String[[] args)
method.

So I would put the main method in the StudentTester class, and in that method I would have all the code that creates a list and some students and tries all their public methods to test them for correct results.

Think about it this way... maybe next term you will have to create a GUI Student Management system. Wouldn't it be great to use the Student and StudentList classes from this project?

   public Student(name, ID, GPA){

       this.name= name; 
       this.id= ID; 
       this.gpa=GPA; 
   } 

This doesn't compile. Learn to crawl before trying to walk. Java requires you to explicitly declare the types of parameters passed to functions. Learn this before worrying about the more advanced topics like what classes should extend what other classes, managaing more than one Student, testing your classes, etc., etc. These are important things to learn, but the basics like creating a simple constructor come first. You need to compile as you go. You clearly are not or this error would have been caught.

commented: Yes +14

That's good advice. I was ignoring your syntax errors because (a) the question was about how to test and (b) you would discover them yourself as soon as you tried to compile.

But do what AssertNull says and compile as you go.

Real programmers compile their modules early and compile them often. That way you discover your mistakes one a time rather all in one horrendous car crash at the end. That makes it a lot easier to diagnose and fix them one at a time.

commented: Great advice! +15
public class StudentListings{

private Student[] data; // an array of Student objects
private int next;
private int size;

 StudentListings(){ 

  data= new Student[size];
  next=0; 
  size= s; 

} 

public boolean insert(Student newStudentListing) { 

} 

 public StudentListings fetch(String targetKey){ 

}

public boolean delete(String targetKey){ 

}

 public boolean update(String targetKey, Student newStudentListing){ 

} 

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(100);

      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.ShowAll();

}
 public Student( String name, int ID, double GPA){

@JamesCherrill, what do I need to put in between the four operations to make it work? @AssertNull I went to fix my errors:

       this.name= name; 
        this.ID= ID; 
       this.GPA=GPA; 
   }

what do I need to put in between the four operations to make it work?

That's your homework for you to do. I'll help you if you're stuck, but I'm not going to do it for you.

Let's try a new rule here: don't post any code here until you have tried to compile it and have fixed all the errors you are able to. If you fix all the complie errors try to execute it and see what happens. That will help you learn, and save our time.

ps: In those methods be careful about where you want a StudenListing and where you want a Student.

I won't write your code for you either. I will however fix up a little what you clearly intended to do. It should now compile fine and run fine and do absolutely nothing, so the running "fine" part is a bit of a stretch, but as far as JAVA is concerned it runs fine since it runs to completion without error. Note the "Write This Function!" placeholders. For fun, try calling one of those functions WITHOUT changing that function and see what happens.

Note the constant spacing and bracket placement. Every "level" of indentation is four spaces. Every bracket has an ending bracket and that ending bracket is very easy to find due to the use of four spaces every single time.

public class StudentListings{

    private Student[] data; // an array of Student objects
    private int next;
    private int size;

    StudentListings(int s){ 
        data= new Student[s];
        next=0; 
        size= s;
    } 

    public boolean insert(Student newStudentListing) { 
        throw new UnsupportedOperationException("Write this function!");
    } 

    public StudentListings fetch(String targetKey){ 
        throw new UnsupportedOperationException("Write this function!");
    }

    public boolean delete(String targetKey){ 
        throw new UnsupportedOperationException("Write this function!");
    }

    public boolean update(String targetKey, Student newStudentListing){ 
        throw new UnsupportedOperationException("Write this function!");
    } 

    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(100);
        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.showAll();
    }
}

To echo James' point I am a little confused on this one...

public StudentListings fetch(String targetKey)

Make sure that's what you want.

public class StudentListings2{
    private Student[] data; 
    private int next;

    private int size;

   public StudentListings2()
   { 

       next=0; 
       size=100;
       data= new Student[size];
    } 

   public boolean insert(Student newStudentListing)
   {

      if(next>= size)
        return false;
       data[next]= newStudentListing.deepCopy();

        if(data[next] == null)
          return false;
         next= next + 1; 
         return true; 
    } 

   public Student fetch(String targetKey){ 

     Student student; 
     Student temp; 

    int i=0; 
   while(i < next && !(data[i].compareTo(targetKey) == 0))
  { 
       i++; 
    } 
    if(i == next) 
     return null; 

    student= data[i].deepCopy();

    if(i != 0)
    { 
         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) 
      return false; 
     data[i] = data[next-1];
     data[next-1] = null; 
     next = next + 1; 
     return true; 
  } 

  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){ 

      StudentListings2 obj1= new StudentListings2();

      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(); 

  } 
 }

@AssertNull, I have compiled and ran the StudentListings2 (I changed it for now) class, but I am still confused about how to call each of four methods in my main method. Here is the edited version for each of the four basic operation algorithm methods.

public class StudentListings2{
    private Student[] data; 
    private int next;

    private int size;

   public StudentListings2()
   { 

       next=0; 
       size=100;
       data= new Student[size];
    } 

   public boolean insert(Student newStudentListing)
   {

      if(next>= size)
        return false;
       data[next]= newStudentListing.deepCopy();

        if(data[next] == null)
          return false;
         next= next + 1; 
         return true; 
    } 

   public Student fetch(String targetKey){ 

     Student student; 
     Student temp; 

    int i=0; 
    while(i < next && !(data[i].compareTo(targetKey) == 0))
    { 
       i++; 
    } 
    if(i == next) 
     return null; 

    student= data[i].deepCopy();

    if(i != 0)
    { 
         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) 
      return false; 
     data[i] = data[next-1];
     data[next-1] = null; 
     next = next + 1; 
     return true; 
  } 

  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){ 

      StudentListings2 obj1= new StudentListings2();
      Student temp;

      Student l1 = new Student("Terrence", 1, 3.45);
      Student l2 = new Student("Roberta", 2, 2.15);
      Student l3 = new Student("George", 3, 1.50);

       System.out.println(obj1.insert(l1));
       System.out.println(obj1.insert(l2));
       System.out.println(obj1.insert(l3));

       temp= obj1.fetch("Terrence");
       System.out.println(temp.toString());

       System.out.println(l3.toString());

       System.out.println(obj1.delete("Roberta"));
       System.out.println(obj1.delete("Terrence"));
       System.out.println(obj1.fetch("Terrence"));

       System.out.println(obj1.update("l3", l2));

       obj1.showAll(); 
  } 

 }

@AssertNull, I don't know why I get an exception at the end of running my updated code above!

Look at your delete function. What does next represent? I see you incrementing next on line 64. Do you want to do that?

Also, I see a potential problem on the if test in line 56. Compound tests can be tricky. Perhaps write out what you want to test in English on that line and see if the computer code corresponds to that thought. Ditto line 64.

Actually forget the part about the compound if test above. Concentrate on line 64. Pretty sure you don't want to be INCREASING next on a deletion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.