📄 testchapter7.java
字号:
import simple.*;
import patronBook.*;
import java.io.*;
/** This class tests the classes of Chapter 6. */
public class TestChapter7
{
static FileWriter outFile=null;
public static void main(String args[]) throws Exception
{
try{
outFile=new FileWriter("OutputTestChapter7.txt");
}catch(IOException e){};
outFile.write("\nTest lists\n");
useList();
useList2();
arrayList();
linkedList();
orderedList();
bilinkedList();
extendedListComparableBooks();
extendedListEqualsBook();
outFile.write("\nTest Books\n");
testBooks();
outFile.close();
}
static public void useList() throws Exception
{
ArrayedSimpleList p = new ArrayedSimpleList(10);
p.insertFirst(new Integer(17));
p.insertFirst(new Integer(-9));
p.insertFirst(new Integer(12));
p.insertFirst(new Integer(4));
p.insertFirst(new Integer(25));
outFile.write(p.toString());
outFile.write("\nThe first item of the list is " + ((Integer) p.firstItem()).intValue());
p.deleteFirst();
p.insertFirst(new Integer(14));
outFile.write("\n" + p);
p.deleteFirst();
p.deleteFirst();
p.deleteFirst();
outFile.write(p.toString());
}
static public void useList2()
{
ArrayedSimpleList p = new ArrayedSimpleList(10);
try
{
p.insertFirst(new Integer(17));
p.insertFirst(new Integer(-9));
p.insertFirst(new Integer(12));
p.insertFirst(new Integer(4));
p.insertFirst(new Integer(25));
} catch (ContainerFullUosException e) {} // do nothing
try
{
outFile.write("\n" + p);
outFile.write("\nThe first item of the list is " + p.firstItem());
} catch (ContainerEmptyUosException e)
{
/* Should not occur: p is not empty, so p.firstItem() should be valid. */
e.printStackTrace();
throw new Error("Error: possible fault in isFull()");
} catch (IOException e)
{
System.out.println("IOException " );
}
try
{
p.deleteFirst();
p.insertFirst(new Integer(14));
outFile.write("\n" + p);
p.deleteFirst();
p.deleteFirst();
p.deleteFirst();
outFile.write("\n" + p);
} catch (ContainerEmptyUosException e)
{
/* Should not occur: p is not empty, so p.deleteFirst() should be valid. */
e.printStackTrace();
} catch (ContainerFullUosException e)
{
/* Should not occur: capacity is 10 and only 5 items are in p at any time. */
e.printStackTrace();
} catch (IOException e)
{
System.out.println("IOException ");
}
}
static public void arrayList() throws Exception
{
outFile.write("\nTest the arrayed lists\n");
ArrayedSimpleList s=new ArrayedSimpleList(4);
if (!s.isEmpty())
outFile.write("Error");
s.insertFirst ("a");
s.insertFirst ("b");
s.insertFirst ("c");
outFile.write(s.capacity()+"\n");
outFile.write(s+"\n");
outFile.write("First item:" + s.firstItem()+"\n");
s.deleteFirst();
outFile.write("After delete first:"+s+"\n");
s.insertFirst ("d");
s.insertFirst ("y");
outFile.write(s+"\n");
if (!s.isFull())
outFile.write("Error\n");
outFile.write(s.firstItem()+"\n");
s.deleteFirst();
outFile.write(s.toString()+"\n");
}
static public void linkedList() throws Exception
{
outFile.write("\nTest the linked lists\n");
LinkedSimpleList l=new LinkedSimpleList();
if (!l.isEmpty())
outFile.write("Error");
l.insertFirst ("a");
l.insertFirst ("b");
l.insertFirst ("c");
outFile.write(l+"\n");
outFile.write("First item:" + l.firstItem()+"\n");
l.deleteFirst();
outFile.write("After delete first:"+l+"\n");
l.insertFirst ("d");
l.insertFirst ("y");
outFile.write(l+"\n");
if (l.isFull())
outFile.write("Error\n");
outFile.write(l.firstItem()+"\n");
l.deleteFirst();
outFile.write(l.toString()+"\n");
}
static public void orderedList() throws Exception
{
outFile.write("\nTest the Ordered lists\n");
OrderedSimpleList o=new OrderedSimpleList();
if (!o.isEmpty())
outFile.write("Error");
o.insert("a");
o.insert("b");
o.insert ("c");
outFile.write(o+"\n");
outFile.write("First item:" + o.firstItem()+"\n");
o.deleteFirst();
outFile.write("After delete first:"+o+"\n");
o.insert("d");
o.insert("y");
o.deleteFirst();
o.search("d");
outFile.write("item:" + o.item()+"\n");
o.search("y");
outFile.write("item:" + o.item()+"\n");
o.deleteItem();
o.insert("j");
outFile.write(o+"\n");
if (o.isFull())
outFile.write("Error\n");
outFile.write(o.firstItem()+"\n");
o.deleteFirst();
outFile.write(o.toString()+"\n");
outFile.write(o.firstItem()+"\n");
}
static public void bilinkedList() throws Exception
{
outFile.write("\nTest the Bilinked lists\n");
BilinkedSimpleList b=new BilinkedSimpleList();
if (!b.isEmpty())
outFile.write("Error");
b.insertFirst("c");
b.insertFirst("a");
b.insertFirst("b");
outFile.write(b+"\n");
outFile.write("First item:" + b.firstItem()+"\n");
b.deleteFirst();
outFile.write("After delete first:"+b+"\n");
b.insertFirst("d");
b.insertFirst("y");
b.deleteFirst();
b.search("d");
outFile.write("item:" + b.item()+"\n");
b.search("m");
if (b.itemExists())
outFile.write("item:" + b.item()+"\n");
b.search("c");
outFile.write("item:" + b.item()+"\n");
b.deleteFirst();
b.insertFirst("j");
outFile.write(b+"\n");
if (b.isFull())
outFile.write("Error\n");
outFile.write(b.firstItem()+"\n");
b.deleteFirst();
outFile.write(b.toString()+"\n");
outFile.write(b.firstItem()+"\n");
}
static public void extendedListComparableBooks() throws Exception
{
outFile.write("\nTest the Extended list of ComparableBooks\n");
SimpleListExtended bklist=new SimpleListExtended();
ComparableBook cb = new ComparableBook("a", "a", "a");
bklist.insertFirst(cb);
cb = new ComparableBook("cc", "cc", "cc");
bklist.insertFirst(cb);
cb = new ComparableBook("ff", "ff", "ff");
bklist.insertFirst(cb);
cb = new ComparableBook("bb", "bb", "bb");
bklist.insertFirst(cb);
outFile.write(bklist.toString()+"\n");
outFile.write("The max book is " + bklist.max().toString()+"\n");
outFile.write("The index of cb is " + bklist.findPos(cb)+"\n");
cb = new ComparableBook("bb", "bb", "ff");
outFile.write("The index of ff is " + bklist.findPos(cb)+"\n");
cb = new ComparableBook("bb", "bb", "bb");
outFile.write("The index of dd is " + bklist.findPos(cb)+"\n");
}
static public void extendedListEqualsBook() throws Exception
{
outFile.write("\nTest the Extended list of EqualsBooks\n");
SimpleListExtended bklist=new SimpleListExtended();
EqualsBook eb = new EqualsBook("a", "a", "a");
bklist.insertFirst(eb);
eb = new EqualsBook("cc", "cc", "cc");
bklist.insertFirst(eb);
eb = new EqualsBook("ff", "ff", "ff");
bklist.insertFirst(eb);
eb = new EqualsBook("bb", "bb", "bb");
bklist.insertFirst(eb);
outFile.write(bklist.toString()+"\n");
outFile.write("The index of eb is " + bklist.findPos(eb)+"\n");
eb = new EqualsBook("bb", "bb", "ff");
outFile.write("The index of ff is " + bklist.findPos(eb)+"\n");
EqualsBook neb = new EqualsBook("bb", "bb", "bb");
outFile.write("The index of dd is " + bklist.findPos(neb)+"\n");
bklist.insertBeforeValue(eb, neb);
eb = (EqualsBook) bklist.firstItem();
bklist.insertBeforeValue(eb, neb);
outFile.write(bklist.toString()+"\n");
outFile.write(bklist.clone().toString()+"\n");
outFile.write(bklist.listClone().toString()+"\n");
}
static public void testBooks() throws Exception
{
ComparableBook book1 = new ComparableBook("Losers", "Jeremy", "12345-6");
ComparableBook book2 = new ComparableBook("GWN", "Jeremy", "12345-0");
ComparableBook book3 = new ComparableBook("Losers", "Jeremy", "12345-6");
ComparableBook book4 = new ComparableBook("Lost", "Jeremy", "12345-9");
outFile.write(book3.compareTo(book1));
outFile.write(book3.compareTo(book4));
LibraryPatron p = new LibraryPatron("Lee");
p.borrowBook(book1);
p.borrowBook(book2);
outFile.write("The current status of the patron is " + p);
p.borrowBook(book3);
p.returnBook(book2);
p.borrowBook(book4);
outFile.write("The current status of the patron is " + p);
p.returnBook(book1);
p.returnBook(book4);
p.returnBook(book3);
outFile.write("The current status of the patron is " + p);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -