在我的任務中,我將創建兩個類:條目&名冊。有兩個階級,應該創建方法使用下列指令之間的鏈接: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進行線性搜索,我認爲這些錯誤流入了插入/刪除方法。如果任何人有關於如何解決這些方法的建議,我會非常感激。我對編程非常陌生。謝謝!
1)什麼是錯誤? 2)做你自己的作業。我們不在這裏爲你做你的任務。然而,如果你被困在一個特定的地方,那麼你應該問 - 不要這樣問...... – Aify
正如我在底部說的,我在名冊類遇到麻煩,我相信它是源於我與搜索方法混淆 – Skylinn
我的評論中的第一點?什麼是錯誤?我確定地獄不會複製/粘貼那個龐大的碼塊併爲你找到錯誤。告訴我們錯誤是什麼,只包括所需的代碼。請閱讀[ask] – Aify