Java methods&objects 1:many relationship

Skylinn

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!

Aify

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Many-to-Many Relationship (with properties) in Google App Engine for Java

From Java

Many to many relationship in java google ap engine

From Java

Eliminate circular JSON in Java Spring Many to Many Relationship

From Java

Eliminate circular JSON in Java Spring Many to Many Relationship

From Java

How to correctly delete a many to many relationship in java spring?

From Dev

Modeling a 1:Many relationship with an attribute

From Dev

MySQL JOIN for sorting a table with a 1 to many relationship

From Dev

DynamoDB - Design 1 to Many relationship

From Dev

How to return nested objects of many-to-many relationship with autoquery

From Dev

Filter objects if one to many relationship not exists

From Dev

java.lang.StackOverflowError When updating entity with many to many relationship

From Dev

How to have intermediate table for a 1 to many relationship?

From Dev

One to many relationship with objects using EF core

From Dev

two Java classes that share many of their methods but dont have a "is a" relationship

From Dev

One to many relationship with the mandatory participation at the 1: site

From Dev

Core Data - Order of objects in a to-many relationship

From Dev

In Many to One Relationship how to delete child objects

From Dev

Django one to many relationship: number of objects

From Dev

Get 'many' objects in one-to-many relationship

From Dev

Oracle Turn 1 to many relationship into single rows

From Dev

NSPredicate SUBQUERY for 1 to many to many relationship

From Dev

Entity Framework : how to create double relationship (1-1 and many-1) with same objects

From Dev

Manually map 1 to Many Relationship

From Dev

MySQL 1-many relationship issue

From Dev

1 to many relationship in Power BI

From Dev

One-to-many and one-to-many relationship 1 laravel

From Dev

Many to many relationship using N1ql couchbase query

From Dev

1 to 1 and 0 to many relationship Laravel not working

From Dev

How to filter distinct objects in a many-to-many relationship in Django?

Related Related

  1. 1

    Many-to-Many Relationship (with properties) in Google App Engine for Java

  2. 2

    Many to many relationship in java google ap engine

  3. 3

    Eliminate circular JSON in Java Spring Many to Many Relationship

  4. 4

    Eliminate circular JSON in Java Spring Many to Many Relationship

  5. 5

    How to correctly delete a many to many relationship in java spring?

  6. 6

    Modeling a 1:Many relationship with an attribute

  7. 7

    MySQL JOIN for sorting a table with a 1 to many relationship

  8. 8

    DynamoDB - Design 1 to Many relationship

  9. 9

    How to return nested objects of many-to-many relationship with autoquery

  10. 10

    Filter objects if one to many relationship not exists

  11. 11

    java.lang.StackOverflowError When updating entity with many to many relationship

  12. 12

    How to have intermediate table for a 1 to many relationship?

  13. 13

    One to many relationship with objects using EF core

  14. 14

    two Java classes that share many of their methods but dont have a "is a" relationship

  15. 15

    One to many relationship with the mandatory participation at the 1: site

  16. 16

    Core Data - Order of objects in a to-many relationship

  17. 17

    In Many to One Relationship how to delete child objects

  18. 18

    Django one to many relationship: number of objects

  19. 19

    Get 'many' objects in one-to-many relationship

  20. 20

    Oracle Turn 1 to many relationship into single rows

  21. 21

    NSPredicate SUBQUERY for 1 to many to many relationship

  22. 22

    Entity Framework : how to create double relationship (1-1 and many-1) with same objects

  23. 23

    Manually map 1 to Many Relationship

  24. 24

    MySQL 1-many relationship issue

  25. 25

    1 to many relationship in Power BI

  26. 26

    One-to-many and one-to-many relationship 1 laravel

  27. 27

    Many to many relationship using N1ql couchbase query

  28. 28

    1 to 1 and 0 to many relationship Laravel not working

  29. 29

    How to filter distinct objects in a many-to-many relationship in Django?

HotTag

Archive