Q:

Java methods&objects 1:many relationship

In my assignment I am to create two classes: Entry & Roster. There is a link between the two classes and the methods should be created using the following instructions:
Entry
Data-->
An Entry has three private instance variables: firstName, lastName, and grade.
The names are both String while the grade is integer.
Methods--> (Do not add other methods)
-Entry (String firstIn, String lastIn, int gradeIn) Constructor
-String toString () Return a String with the name and grade separated by a tab.
Format the name as last, first
-boolean equals(Entry) Return true if the name in the Entry parameter matches
the name in the current object. Return false if it does not match.
-int getGrade() Return the value of the grade in the object
Roster
Data--> A Roster has an ArrayList of Entry objects and a constant NOT_FOUND. You
must use an ArrayList for this assignment.
Methods (Do not add other methods-->
-Roster () Instantiate the ArrayList. The ArrayList initialized to be empty.
-void insert (Entry) Search the list using the private search method (below). If
the entry is not in the Roster, add it so it is the last in the list. If the name is already in the Roster, do nothing.
-void delete (Entry) Search the list using the private search method (below). If
there is an entry that matches, delete the entry. If there is no match, do
nothing. The order must remain the same.
-void printAll () Print all entries in the Roster, each on its own line.
-double average() Calculate the average grade in the Roster as a double
-private int search (Entry) Implement the linear search algorithm to work on an ArrayList of Entry. Use the equals method in the Entry
class to determine if there is a match. The grade is not used in the equality
check. Note: A solution using a for-loop or breaking from a loop is not
acceptable.
Here are the Entry and Roster classes that I coded:
    public class Entry {
private String firstName;
private String lastName;
private int grade;

public Entry(String firstIn, String lastIn, int gradeIn) {
    firstName = firstIn;
    lastName = lastIn;
    grade = gradeIn;
}

public String toString() {
    return (lastName + ", " + firstName + "\t" + grade);
}

public boolean equals(Entry entryIn) {
    if (firstName.equals(entryIn.firstName) 
            && lastName.equals(entryIn.lastName)) {
        return true;
    }
    else {
        return false;
    }
}

public int getGrade(){
    return grade;
}
}

Here is my Roster class
    import java.util.ArrayList;

public class Roster {
private static ArrayList<Entry> entries;
private final int NOT_FOUND = -1;

public Roster() {
    entries = new ArrayList<Entry>();
}

public void insert(Entry entryIn) {
    if (search(entryIn) > -1) {
        entries.add(entryIn);
    }
    else {

    }
}

public void delete(Entry entryIn) {
    int size = entries.size();
    if (search(entryIn) > -1) {
        entries.remove(search(entryIn));
        size--;
    }
}

public void printAll() {
    for (Entry entryIn : entries) {
        System.out.println(entryIn);
    }
}

public static double average() {
    double average = 0.0;
    for (int i = 1; i < entries.size(); i++) {
            average += entries.get(i).getGrade();
        } 
    return average/entries.size();
    }

private int search(Entry entryIn) {
    boolean found = false;
    int index = 0;
    while (index < entries.size() && !found) {
        if(entries.get(index).equals(entryIn)) {
            found = true;
        }
        else {
            index++;
        }
        if (index == entries.size()) {
            index = -1;
        }
    return index;
    }
    return index;

}
}

This is the main method I was given: 
    //package project7;
import java.util.*;

public class Project7 {

/* 
 * Eight test cases for Roster
 */

    /* main method to control tests to be performed.
     * 
     */
    public static void main (String [] args)
    {
        char ch;
        boolean end = false;
        do
        {
            ch = getCommand();
            switch (ch)
            {
                case '1' :  test1();
                            break;
                case '2' :  test2();
                            break;
                case '3' :  test3();
                            break;
                case '4' :  test4();
                            break;
                case '5' :  test5();
                            break;
                case '6' :  test6();
                            break;
                case '7' :  test7();
                            break;
                case '0' :  end = true;
                            break;
            }
        }
        while (!end);
        System.out.println ("Program complete");
    }       

    /* prompt the user to enter a test number and return it as a character
     * 
     */

    static char getCommand ()
    {
        final Scanner input = new Scanner (System.in);
        char command;
        boolean valid;

        do {
            System.out.println ();
            System.out.print ("Enter test number (1..7) (0 to stop): ");
            String answer = input.next();
            command = answer.charAt(0);
            valid = command >= '0' && command <= '7';
            if (!valid)
                System.out.println ("Entry not valid, enter again");
        } while (!valid);
        return command;
    }

    /* test 1 - empty book
     */

    static void test1()
    {

        System.out.println ("Test 1: Entry");

        Entry entry1 = new Entry ("Joe", "Smith", 100);

        System.out.println ("Expecting: Smith, Joe  100");
        System.out.println ("Result: " + entry1);
        System.out.println ();

        System.out.println ("Expecting: true");
        System.out.println ("Result: " + entry1.equals(entry1));
        System.out.println ();

        System.out.println ("Expecting: false");
        Entry entry2 = new Entry ("Bill", "Jones", 0);
        System.out.println ("Result: " + entry1.equals(entry2));
        System.out.println ();

        System.out.println ("Expecting: 100");
        System.out.println (entry1.getGrade());
    }

    /* test 2 - empty Roster
    */

    static void test2()
    {
        System.out.println ("Test2: empty");
        System.out.println ();

        Roster book = new Roster ();
        System.out.println ("Expecting nothing");
        book.printAll();

        System.out.println ("Expecting 0.0");
        System.out.println (book.average());
        System.out.println ();
    }

    /* test 3 - insert and search
    */

    static void test3()
    {
        System.out.println ("Test3: insert ");
        System.out.println ();

        Roster list = new Roster();
        Entry temp = new Entry ("John", "Smith", 99);
        list.insert(temp);
        System.out.println ("Expecting Smith, John  99");
        list.printAll();
        System.out.println ();

        list.insert (new Entry ("Tom", "Jones", 78));
        list.insert (new Entry ("Fred", "Flintstone", 55));
        list.insert(new Entry ("Jill", "St. John", 79));
        list.insert(new Entry ("Jim", "Smith", 88));
        System.out.println ("Expecting 5 entries");
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 79.8");
        System.out.println (list.average());
    }

    /* test 4 - insert with duplicates
    */

    static void test4()
    {
        System.out.println ("Test4: Duplicate Entries ");
        System.out.println ();

        Roster book = new Roster();
        book.insert(new Entry ("John", "Bob", 77));
        book.insert(new Entry ("Jim","Bob", 89));
        book.insert(new Entry ("John", "Bob", 89));
        book.insert(new Entry ("Jim","Bob", 55));

        System.out.println ("Expecting 2 entries");
        book.printAll();
        System.out.println ();
    }

    /* test 5 - deleting
    */      

static void test5()
    {
        System.out.println ("Test5: Deleting");
        System.out.println ();

        Roster list = new Roster ();
        list.insert(new Entry ("John", "Johnson", 77));
        list.insert(new Entry ("Tom","Thompson", 99));
        list.insert(new Entry ("Jeff", "Jefferson", 44));
        list.insert(new Entry ("Fred", "Fredrickson", 91));
        list.insert(new Entry ("Tina", "Tina", 95));

        System.out.println ("Expecting 5 entries");
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 4 entries");
        list.delete(new Entry ("John", "Johnson", 0));
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 3 entries");
        list.delete(new Entry ("Tina", "Tina", 0));
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 2 entries");
        list.delete(new Entry ("Fred", "Fredrickson", 0));
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 1 entry");
        list.delete(new Entry ("Tom", "Thompson", 0));
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 0 entries");
        list.delete(new Entry ("Jeff", "Jefferson", 0));
        list.printAll();

        System.out.println ();

    }

    /* test 6 - delete duplicates
     */

    static void test6() {
        System.out.println ("Test6: delete duplicates");
        System.out.println ();

        // create new book and fill
        Roster list = new Roster ();

        list.insert(new Entry ("John", "Johnson", 77));
        list.insert(new Entry ("Tom","Thompson", 99));
        list.insert(new Entry ("Jeff", "Jefferson", 44));
        list.insert(new Entry ("Fred", "Fredrickson", 91));
        list.insert(new Entry ("Tina", "Tina", 95));

        System.out.println ("Expecting all");
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 4 entries");
        list.delete(new Entry ("Jeff", "Jefferson", 0));
        list.printAll();
        System.out.println ();

        System.out.println ("Expecting 4 entries");
        list.delete(new Entry ("Jeff", "Jefferson", 0));
        list.printAll();
        System.out.println ();

    }       

    /* test 7- empty and fill
     */

    static void test7 () {
        Roster list = new Roster ();
        list.insert(new Entry ("John", "Johnson", 77));
        list.insert(new Entry ("Tom","Thompson", 99));
        list.insert(new Entry ("Jeff", "Jefferson", 44));

        System.out.println ("Expecting 3 entries");
        list.printAll();
        System.out.println ();

        list.delete(new Entry ("John", "Johnson", 0));
        list.delete(new Entry ("Tom", "Thompson", 0));
        list.delete(new Entry ("Jeff", "Jefferson", 0));

        System.out.println ("Expecting 0 entries");
        list.printAll();
        System.out.println ();

        list.insert(new Entry ("John", "Johnson", 87));
        list.insert(new Entry ("Tom","Thompson", 76));
        list.insert(new Entry ("Jeff", "Jefferson", 83));

        System.out.println ("Expecting 3 entries");
        list.printAll();
        System.out.println ();

    }   
}

The code runs but doesn't give the intended output. I had a hard time coding a linear search for an ArrayList in the search method and I think those errors trickled into the insert/delete methods. If anyone has advice on how to fix those methods I would be really appreciative. I'm very new to programming. Thanks!

A:

Here we go.
Test 2: 
Your average function doesn't need to be static. You should also check to see if the size of the list is empty in that function, so that you don't try to return a possible "something divided by 0". Just return 0.0 straight up if the list is empty.
if (entries.size() == 0) {
    return 0.0;
}

You may also want to change the for loop to this:
for (Entry x : entries) {
    average += x.getGrade();
}

Your current one is wrong. It starts counting from 0. The for each loop is much cleaner.
Test 3:
The problem lies in your search function. Several things are wrong here - first of all, this;
while (index < entries.size() && !found) {
    if (entries.get(index).equals(entryIn)) {
        found = true;
    } else {
        index++;
    }
    if (index == entries.size()) {
        index = -1;
    }
    return index;
}

This loop is fundamentally wrong, because at the end of the first iteration, you instantly quit the function. You're not actually looping through the entire list. Put the return index; inside the if (index == ....) block.
Second of all, .size() starts counting from 1, not 0. Indexes start from 0. This means that you'll never get to that condition. (BTW, your while loop condition is < size() which is correct, but this literally says that index will never == size()) 
Furthermore, that entire function can be simplified down to:
for (Entry x : entries) {
    if (x.equals(entryIn)) {
        return entries.indexOf(x); // if you find it, return the index.
    }
}

return -1; // if you didn't return already, you didn't find it. so return -1

But that's not enough to fix Test 3. You still have an error in your insert function.
if (search(entryIn) > -1) {
    entries.add(entryIn); // are you sure it should be here?
} else {

}

By putting the add entry in that if block, you're essentially saying. "If i search for this entry and find an index that is bigger than -1, add it." Which means "If it already exists, add it." IDK about you, but that doesn't make much sense to me. For future tests, try reading out loud the meaning behind your if/else checks, and ask yourself "Does it make sense?"
As you noticed yourself there were a ton of small errors in your code, just little things. If you test each one by itself and fix them one at a time, your program will straighten itself out really quickly. In fact, by fixing these things, your entire program passes all the tests (as far as I can tell anyways, since I didn't really read the requirements of your assignment).
Please read the how to ask link in the comments anyways, just in case you have another question. Do it as a favor for me, since I just spent the time to debug your code, please.

