Java方法和对象1:很多关系

时间:2015-05-01 18:37:40

标签: java object methods arraylist

在我的任务中,我将创建两个类:Entry&名册。这两个类之间存在链接,应使用以下指示信息创建方法:

条目

数据 - > Entry有三个私有实例变量:firstName,lastName和grade。 名称都是String,而等级是整数。

方法 - > (不要添加其他方法) -Entry(String firstIn,String lastIn,int gradeIn)构造函数

-String toString()返回一个字符串,其名称和等级由制表符分隔。 将名称格式化为最后一个

-boolean equals(Entry)如果Entry参数中的名称匹配,则返回true 当前对象中的名称。如果不匹配则返回false。

-int getGrade()返回对象中等级的值

名册

数据 - > Roster有一个Entry对象的ArrayList和一个常量NOT_FOUND。您 必须使用ArrayList进行此分配。

方法(不要添加其他方法 - >

-Roster()实例化ArrayList。 ArrayList初始化为空。

-void insert(Entry)使用私有搜索方法搜索列表(如下所示)。如果 该条目不在名册中,添加它以使其成为列表中的最后一个。如果名称已在名册中,则不执行任何操作。

-void delete(Entry)使用私有搜索方法搜索列表(如下所示)。如果 有一个匹配的条目,删除条目。如果没有匹配,那就做 没有。订单必须保持不变。

-void printAll()打印名册中的所有条目,每个条目都在各自的行上。

-double average()计算名册中的平均成绩为双倍

-private int search(Entry)实现线性搜索算法以处理Entry的ArrayList。在条目中使用equals方法 用于确定是否存在匹配的类。等级不用于平等 校验。注意:使用for循环或从循环中断的解决方案不是 可以接受的。

以下是我编写的Entry和Roster类:

    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;
}
}

这是我的名册课程

    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;

}
}

这是我给出的主要方法:

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

    }   
}

代码运行但没有给出预期的输出。我很难在搜索方法中对ArrayList进行线性搜索编码,我认为这些错误会渗入插入/删除方法。如果有人就如何修复这些方法提出建议,我会非常感激。我对编程非常陌生。谢谢!

2 个答案:

答案 0 :(得分:0)

您的主要问题在于您的搜索方法以及设置索引的方式/您要返回的索引....

您的平均方法和插入方法中也存在错误。找到的东西很小但很乏味。

尝试跟踪您的代码,看看您是否可以看到当您单步执行搜索方法时会发生什么。评论,如果你仍然无法搞清楚。这是一个家庭作业,我不会只给你答案,你不会那样学习。

另外,下次确保预期输出和实际结果。这可以防止我们不得不接受您的代码并运行它,因为并非所有人都有时间这样做。

答案 1 :(得分:0)

我们走了。

测试2:

您的平均功能不需要是静态的。您还应检查该功能中列表的大小是否为空,这样您就不会尝试返回可能的&#34;除以0和#34;的内容。如果列表为空,只需直接返回0.0

if (entries.size() == 0) {
    return 0.0;
}

您可能还想将for循环更改为:

for (Entry x : entries) {
    average += x.getGrade();
}

你现在的错误。它从0开始计数。每个循环都更清晰。

测试3:

问题出在你的搜索功能上。这里有几件事是错的 - 首先,这个;

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;放在if (index == ....)块内。

其次,.size()从1开始计数,而不是0.索引从0开始。这意味着你永远不会达到那个条件。 (顺便说一句,你的while循环条件是< size()这是正确的,但这确实说索引永远不会== size()

此外,整个功能可以简化为:

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

但这还不足以修复测试3.您的插入功能仍然存在错误。

if (search(entryIn) > -1) {
    entries.add(entryIn); // are you sure it should be here?
} else {

}

通过在if块中添加add条目,你实际上是在说。 &#34;如果我搜索此条目并找到大于-1的索引,请添加它。&#34;这意味着&#34;如果已经存在,请添加它。&#34; IDK关于你,但这对我没有多大意义。对于将来的测试,请尝试大声读出if / else检查背后的含义,并问自己&#34;它是否有意义?&#34;

正如您所注意到的那样,您的代码中存在大量的小错误,只是一些小问题。如果你自己测试每一个并一次修复一个,你的程序将很快自我整理。事实上,通过修复这些内容,您的整个程序都会通过所有测试(据我所知,因为我没有真正阅读您的任务要求)。

请在评论中阅读如何询问链接,以防您有其他问题。这样做对我有利,因为我只是花时间来调试你的代码。