Skip to content

Commit 56acd49

Browse files
author
jason_yao
committed
update
1 parent 66455df commit 56acd49

2,462 files changed

Lines changed: 48198 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface Aggregate {
2+
public abstract Iterator iterator();
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Book {
2+
private String name = "";
3+
public Book(String name) {
4+
this.name = name;
5+
}
6+
public String getName() {
7+
return name;
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Vector;
2+
3+
public class BookShelf implements Aggregate {
4+
private Vector books;
5+
public BookShelf(int initialsize) {
6+
this.books = new Vector(initialsize);
7+
}
8+
public Book getBookAt(int index) {
9+
return (Book)books.get(index);
10+
}
11+
public void appendBook(Book book) {
12+
books.add(book);
13+
}
14+
public int getLength() {
15+
return books.size();
16+
}
17+
public Iterator iterator() {
18+
return new BookShelfIterator(this);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class BookShelfIterator implements Iterator {
2+
private BookShelf bookShelf;
3+
private int index;
4+
public BookShelfIterator(BookShelf bookShelf) {
5+
this.bookShelf = bookShelf;
6+
this.index = 0;
7+
}
8+
public boolean hasNext() {
9+
if (index < bookShelf.getLength()) {
10+
return true;
11+
} else {
12+
return false;
13+
}
14+
}
15+
public Object next() {
16+
Book book = bookShelf.getBookAt(index);
17+
index++;
18+
return book;
19+
}
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface Iterator {
2+
public abstract boolean hasNext();
3+
public abstract Object next();
4+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
BookShelf bookShelf = new BookShelf(4);
4+
bookShelf.appendBook(new Book("Around the World in 80 Days"));
5+
bookShelf.appendBook(new Book("Bible"));
6+
bookShelf.appendBook(new Book("Cinderella"));
7+
bookShelf.appendBook(new Book("Daddy-Long-Legs"));
8+
bookShelf.appendBook(new Book("East of Eden"));
9+
bookShelf.appendBook(new Book("Frankenstein"));
10+
bookShelf.appendBook(new Book("Gulliver's Travels"));
11+
bookShelf.appendBook(new Book("Hamlet"));
12+
Iterator it = bookShelf.iterator();
13+
while (it.hasNext()) {
14+
Book book = (Book)it.next();
15+
System.out.println("" + book.getName());
16+
}
17+
}
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface Aggregate {
2+
public abstract Iterator iterator();
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Book {
2+
private String name = "";
3+
public Book(String name) {
4+
this.name = name;
5+
}
6+
public String getName() {
7+
return name;
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class BookShelf implements Aggregate {
2+
private Book[] books;
3+
private int last = 0;
4+
public BookShelf(int maxsize) {
5+
this.books = new Book[maxsize];
6+
}
7+
public Book getBookAt(int index) {
8+
return books[index];
9+
}
10+
public void appendBook(Book book) {
11+
this.books[last] = book;
12+
last++;
13+
}
14+
public int getLength() {
15+
return last;
16+
}
17+
public Iterator iterator() {
18+
return new BookShelfIterator(this);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class BookShelfIterator implements Iterator {
2+
private BookShelf bookShelf;
3+
private int index;
4+
public BookShelfIterator(BookShelf bookShelf) {
5+
this.bookShelf = bookShelf;
6+
this.index = 0;
7+
}
8+
public boolean hasNext() {
9+
if (index < bookShelf.getLength()) {
10+
return true;
11+
} else {
12+
return false;
13+
}
14+
}
15+
public Object next() {
16+
Book book = bookShelf.getBookAt(index);
17+
index++;
18+
return book;
19+
}
20+
}

0 commit comments

Comments
 (0)