2015-05-01 26 views
-3

在我的任務中,我將創建兩個類:條目&名冊。有兩個階級,應該創建方法使用下列指令之間的鏈接:Java方法與對象1:多種關係

進入

數據 - > 的條目有三個私有實例變量:名字,姓氏,和品位。 名稱都是字符串,而等級是整數。

方法 - >(不要添加其他方法) -entry(字符串firstIn,字符串lastIn,詮釋gradeIn)構造

-STRING toString()返回一個字符串的名稱和等級由製表符分隔。 將名稱格式化爲最後一個,第一個

-boolean equals(Entry)如果Entry參數中的名稱匹配 當前對象中的名稱,則返回true。如果不匹配,則返回false。

-int getGrade()返回在對象

名冊

數據的等級的值 - >名冊具有Entry對象和恆定NOT_FOUND的ArrayList。您 必須爲此分配使用ArrayList。

方法(不要加其它的方法 - >

-Roster()實例化的ArrayList ArrayList中初始化爲空

-void插入(輸入)使用專用的搜索搜索列表方法(如下)如果 條目不在名單中,則添加它以便它是列表中的最後一個。如果名稱已在名單中,則不執行任何操作。

-void delete(Entry)列表使用私人搜索方法(如下)如果 有匹配的條目,則刪除條目。沒有任何匹配,做 什麼都沒有。訂單必須保持不變。

-void printAll()列出名冊中的所有條目,每條都在其自己的行上。

-double平均()計算在名冊的平均等級作爲雙

- 私人INT搜索(條目)執行線性搜索算法對輸入的ArrayList工作。使用條目 中的equals方法來確定是否存在匹配。該等級不用於等於 檢查。注意:使用for循環或從循環中斷開的解決方案不是 可接受的。

下面是我編寫的進入和名冊類:

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進行線性搜索,我認爲這些錯誤流入了插入/刪除方法。如果任何人有關於如何解決這些方法的建議,我會非常感激。我對編程非常陌生。謝謝!

+5

1)什麼是錯誤? 2)做你自己的作業。我們不在這裏爲你做你的任務。然而,如果你被困在一個特定的地方,那麼你應該問 - 不要這樣問...... – Aify

+0

正如我在底部說的,我在名冊類遇到麻煩,我相信它是源於我與搜索方法混淆 – Skylinn

+0

我的評論中的第一點?什麼是錯誤?我確定地獄不會複製/粘貼那個龐大的碼塊併爲你找到錯誤。告訴我們錯誤是什麼,只包括所需的代碼。請閱讀[ask] – Aify

回答

0

我們走吧。

測試2:

你平均的功能並不需要是靜態的。您還應該檢查該函數中列表的大小是否爲空,以便您不要嘗試返回可能的「被0除的東西」。如果列表爲空,直接返回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 { 

} 

通過把添加條目,如果塊,你基本上是說。 「如果我搜索此條目並找到大於-1的索引,請將其添加。」這意味着「如果它已經存在,請添加它。」 IDK關於你,但這對我來說沒有多大意義。對於未來的測試,請嘗試大聲讀出if/else檢查背後的含義,然後問自己:「它有道理嗎?」

正如你注意到自己,你的代碼中有很多小錯誤,只是一些小事情。如果你自己測試每一個並且一次一個地修復它們,那麼你的程序會很快地將它自己擺正。事實上,通過修復這些東西,你的整個程序都通過了所有的測試(據我所知,因爲我沒有真正閱讀你的任務的要求)。

無論如何,請閱讀如何在評論中詢問鏈接,以防萬一您有其他問題。請幫我一下,因爲我只是花時間調試你的代碼。

+0

謝謝你的耐心!有一段時間不得不修補這些小東西,但我終於開始工作了!我很感謝你的知識,下次我想提問時,我會更加關注網站協議。再次感謝 – Skylinn

0

你的主要問題是在你的搜索方法,並要設置索引的方式/你是返回什麼指標....

你也有一個錯誤在你平均法,並在您的插入方法。有小而乏味的發現。

嘗試通過您的代碼進行跟蹤,並查看您是否可以看到當您逐步執行搜索方法時會發生什麼情況。評論你是否仍然無法弄清楚。這是一項家庭作業,我不只是給你答案,你不這樣學習。

此外,下次請務必將預期產出和實際結果。這可以防止我們不得不拿走你的代碼並運行它,因爲我們並不是所有人都有時間這樣做。