Skip to content

Commit 54c3da3

Browse files
author
java-tester-x
committed

File tree

7 files changed

+253
-12
lines changed

7 files changed

+253
-12
lines changed

make.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
javac -cp .;lib/junit-4.11.jar -d bin %1
1+
javac -cp .;lib/* -d bin %1.java

run.bat

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
javac -Xlint:unchecked -cp .;lib/* -d bin %1.java
2-
java -cp .;lib/*;bin/ %1
3-
pause
1+
java -cp .;lib/*;bin/ %1

src/Book.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
public class Book {
1515

16-
private String name;
17-
private double price;
18-
private Author author;
19-
private int qtyInStock = 0;
16+
private String name;
17+
private double price;
18+
private Author author;
19+
private int qtyInStock = 0;
2020

2121
public Book (String name, Author author, double price) {
22-
this.name = name;
23-
this.author = author;
24-
this.price = price;
22+
this.name = name;
23+
this.author = author;
24+
this.price = price;
2525
}
2626

2727
public Book (String name, Author author, double price, int qtyInStock) {
@@ -66,7 +66,7 @@ public String getAuthorEmail() {
6666
public char getAuthorGender() {
6767
return this.author.getGender();
6868
}
69-
69+
7070
public String toString() {
7171
return "'" + name +"' by " + author;
7272
}

src/Book2.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package src;
2+
3+
/**
4+
* A class called circle is designed as shown in the following class diagram. It contains:
5+
*
6+
* 1) Two private instance variables: radius (of type double) and color (of type String),
7+
* with default value of 1.0 and "red", respectively.
8+
* 2) Two overloaded constructors;
9+
* 3) Two public methods: getRadius() and getArea().
10+
*
11+
* The source codes for Circle is as follows:
12+
*/
13+
14+
public class Book2 {
15+
16+
private String name;
17+
private double price;
18+
private Author[] authors;
19+
private int qtyInStock = 0;
20+
21+
public Book2(String name, Author[] authors, double price) {
22+
this.name = name;
23+
this.authors = authors;
24+
this.price = price;
25+
}
26+
27+
public Book2(String name, Author[] authors, double price, int qtyInStock) {
28+
this.name = name;
29+
this.authors = authors;
30+
this.price = price;
31+
this.qtyInStock = qtyInStock;
32+
}
33+
34+
public String getName() {
35+
return this.name;
36+
}
37+
38+
public double getPrice() {
39+
return this.price;
40+
}
41+
42+
public Author[] getAuthors() {
43+
return this.authors;
44+
}
45+
46+
public void setPrice(double price) {
47+
this.price = price;
48+
}
49+
50+
public int getQtyInStock() {
51+
return this.qtyInStock;
52+
}
53+
54+
public void setQtyInStock(int qtyInStock) {
55+
this.qtyInStock = qtyInStock;
56+
}
57+
58+
public void printAuthors() {
59+
int authorNo = 0;
60+
for (Author a : this.authors) {
61+
System.out.println("("+(++authorNo)+") "+a);
62+
}
63+
}
64+
65+
public String toString() {
66+
return "'" + name +"' by " + authors.length + " authors";
67+
}
68+
}

src/Book3.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package src;
2+
3+
/**
4+
* A class called circle is designed as shown in the following class diagram. It contains:
5+
*
6+
* 1) Two private instance variables: radius (of type double) and color (of type String),
7+
* with default value of 1.0 and "red", respectively.
8+
* 2) Two overloaded constructors;
9+
* 3) Two public methods: getRadius() and getArea().
10+
*
11+
* The source codes for Circle is as follows:
12+
*/
13+
14+
public class Book3 {
15+
16+
private String name;
17+
private double price;
18+
private Author[] authors = new Author[5];
19+
private int numAuthors = 0;
20+
private int qtyInStock = 0;
21+
22+
public Book3(String name, double price) {
23+
this.name = name;
24+
this.price = price;
25+
}
26+
27+
public Book3(String name, double price, int qtyInStock) {
28+
this.name = name;
29+
this.price = price;
30+
this.qtyInStock = qtyInStock;
31+
}
32+
33+
public String getName() {
34+
return this.name;
35+
}
36+
37+
public double getPrice() {
38+
return this.price;
39+
}
40+
41+
public Author[] getAuthors() {
42+
return this.authors;
43+
}
44+
45+
public void setPrice(double price) {
46+
this.price = price;
47+
}
48+
49+
public int getQtyInStock() {
50+
return this.qtyInStock;
51+
}
52+
53+
public void setQtyInStock(int qtyInStock) {
54+
this.qtyInStock = qtyInStock;
55+
}
56+
57+
public void printAuthors() {
58+
int authorNo = 0;
59+
for (Author a : this.authors) {
60+
if (a == null) {
61+
continue;
62+
}
63+
System.out.println("("+(++authorNo)+") "+a);
64+
}
65+
}
66+
67+
public void addAuthor(Author author)
68+
{
69+
for (int i = 0; i < authors.length; i++)
70+
{
71+
if (authors[i] != null) {
72+
continue;
73+
}
74+
75+
authors[i] = author;
76+
++numAuthors;
77+
break;
78+
}
79+
}
80+
81+
public boolean removeAuthorByName(String authorName)
82+
{
83+
for (int i = 0; i < authors.length; i++)
84+
{
85+
if (authors[i] == null) {
86+
continue;
87+
}
88+
89+
if (! authorName.toUpperCase().equals(authors[i].getName().toUpperCase()) ) {
90+
continue;
91+
}
92+
93+
authors[i] = null;
94+
--numAuthors;
95+
return true;
96+
}
97+
98+
return false;
99+
}
100+
101+
public String toString() {
102+
return "'" + name +"' by " + numAuthors + " authors";
103+
}
104+
}

src/TestBook2.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package src;
2+
3+
public class TestBook2 {
4+
5+
public static void main(String[] args)
6+
{
7+
Author[] authors = new Author[2];
8+
authors[0] = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');
9+
authors[1] = new Author("Paul Tan", "Paul@nowhere.com", 'm');
10+
11+
Book2 aBook = new Book2("Java for dummy", authors, 19.95, 1000);
12+
13+
// Use an anonymous instance of Author
14+
Book2 anotherBook = new Book2(
15+
"more Java for dummy"
16+
, new Author[] {
17+
new Author("Bruce Eckel", "b.eckel@somewhere.com", 'm')
18+
, new Author("Paul Tan", "Paul@nowhere.com", 'm')
19+
}
20+
, 29.95, 888);
21+
22+
// additional task #1
23+
System.out.println("\nThe book: " + aBook);
24+
System.out.println("The authors are:");
25+
aBook.printAuthors();
26+
27+
System.out.println("\nThe book: " + anotherBook);
28+
System.out.println("The authors are:");
29+
anotherBook.printAuthors();
30+
}
31+
}

src/TestBook3.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package src;
2+
3+
public class TestBook3 {
4+
5+
public static void main(String[] args)
6+
{
7+
Book3 aBook = new Book3("Java for dummy", 19.95, 1000);
8+
aBook.addAuthor(new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm'));
9+
aBook.addAuthor(new Author("Paul Tan", "Paul@nowhere.com", 'm'));
10+
11+
// additional task #1
12+
System.out.println("\nThe book: " + aBook);
13+
System.out.println("The authors are:");
14+
aBook.printAuthors();
15+
16+
aBook.addAuthor(new Author("Bruce Eckel", "b.eckel@somewhere.com", 'm'));
17+
aBook.addAuthor(new Author("Kris Datel", "k.datel@somewhere.com", 'm'));
18+
System.out.println("\nThe book: " + aBook);
19+
System.out.println("The authors are:");
20+
aBook.printAuthors();
21+
22+
aBook.removeAuthorByName("Bruce Eckel");
23+
System.out.println("\nThe book: " + aBook);
24+
System.out.println("The authors are:");
25+
aBook.printAuthors();
26+
27+
aBook.addAuthor(new Author("Bruce Eckel", "b.eckel@somewhere.com", 'm'));
28+
aBook.addAuthor(new Author("Paul Tan Junior", "PaulJunior@nowhere.com", 'm'));
29+
System.out.println("\nThe book: " + aBook);
30+
System.out.println("The authors are:");
31+
aBook.printAuthors();
32+
33+
// ATTENTION: here new author wouldn't be added !!! NO errors - see code of Book3.java
34+
aBook.addAuthor(new Author("Bruce Eckel Junior", "b.eckel-junior@nowhere.com", 'm'));
35+
System.out.println("\nThe book: " + aBook);
36+
System.out.println("The authors are:");
37+
aBook.printAuthors();
38+
39+
}
40+
}

0 commit comments

Comments
 (0)