Skip to content

Commit d00db76

Browse files
author
java-tester-x
committed
Add few classes
Add few classes
1 parent 40be11a commit d00db76

6 files changed

Lines changed: 182 additions & 7 deletions

File tree

src/Author.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package src;
2+
3+
public class Author {
4+
5+
private String name;
6+
private String email;
7+
private char gender;
8+
9+
public Author (String name, String email, char gender) {
10+
this.name = name;
11+
this.email = email;
12+
this.gender = gender;
13+
}
14+
15+
public String getName() {
16+
return this.name;
17+
}
18+
19+
public String getEmail() {
20+
return this.email;
21+
}
22+
23+
public void setEmail(String setEmail) {
24+
this.email = setEmail;
25+
}
26+
27+
public char getGender() {
28+
return this.gender;
29+
}
30+
31+
public String toString() {
32+
return name + " (" + gender + ") at " + email;
33+
}
34+
}

src/Book.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Book {
15+
16+
private String name;
17+
private double price;
18+
private Author author;
19+
private int qtyInStock = 0;
20+
21+
public Book (String name, Author author, double price) {
22+
this.name = name;
23+
this.author = author;
24+
this.price = price;
25+
}
26+
27+
public Book (String name, Author author, double price, int qtyInStock) {
28+
this.name = name;
29+
this.author = author;
30+
this.price = price;
31+
this.qtyInStock = qtyInStock;
32+
}
33+
34+
public String getName() {
35+
return this.name;
36+
}
37+
38+
public Author getAuthor() {
39+
return this.author;
40+
}
41+
42+
public double getPrice() {
43+
return this.price;
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 String toString() {
59+
return "'" + name +"' by " + author;
60+
}
61+
}

src/Circle.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ public class Circle {
1818

1919
// 1st constructor, which sets both radius and color to default
2020
public Circle() {
21-
radius = 1.0;
22-
color = "red";
21+
this.radius = 1.0;
22+
this.color = "red";
2323
}
2424

2525
// 2nd constructor with given radius, but color default
26-
public Circle(double r) {
27-
radius = r;
28-
color = "red";
26+
public Circle(double radius) {
27+
this.radius = radius;
28+
this.color = "red";
29+
}
30+
31+
// Construtor to construct a new instance of Circle with the given radius and color
32+
public Circle (double radius, String color) {
33+
this.radius = radius;
34+
this.color = color;
2935
}
3036

3137
// A public method for retrieving the radius
@@ -37,4 +43,23 @@ public double getRadius() {
3743
public double getArea() {
3844
return radius*radius*Math.PI;
3945
}
46+
47+
// Getter for instance variable color
48+
public String getColor() {
49+
return color;
50+
}
51+
52+
// Setter for instance variable radius
53+
public void setRadius(double radius) {
54+
this.radius = radius;
55+
}
56+
57+
// Setter for instance variable color
58+
public void setColor(String color) {
59+
this.color = color;
60+
}
61+
62+
public String toString() {
63+
return "Circle: radius=" + radius + " color=" + color;
64+
}
4065
}

src/TestAuthor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package src;
2+
3+
public class TestAuthor {
4+
5+
public static void main(String[] args)
6+
{
7+
Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');
8+
System.out.println(anAuthor); // call toString()
9+
10+
anAuthor.setEmail("paul@nowhere.com");
11+
System.out.println(anAuthor);
12+
}
13+
}

src/TestBook.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package src;
2+
3+
public class TestBook {
4+
5+
public static void main(String[] args)
6+
{
7+
Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');
8+
Book aBook = new Book("Java for dummy", anAuthor, 19.95, 1000);
9+
10+
// Use an anonymous instance of Author
11+
Book anotherBook = new Book(
12+
"more Java for dummy"
13+
, new Author("Bruce Eckel", "b.eckel@somewhere.com", 'm')
14+
, 29.95, 888);
15+
}
16+
}

src/TestCircle.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,39 @@ public static void main(String[] args) {
77
Circle c1 = new Circle();
88
// Use the dot operator to invoke methods of instance c1.
99
System.out.println("The circle has radius of "
10-
+ c1.getRadius() + " and area of " + c1.getArea());
10+
+ c1.getRadius() + " and area of " + c1.getArea()
11+
+ " and color of " + c1.getColor());
1112

1213
// Declare and allocate an instance of class circle called c2
1314
// with the given radius and default color
1415
Circle c2 = new Circle(2.0);
1516
// Use the dot operator to invoke methods of instance c2.
1617
System.out.println("The circle has radius of "
17-
+ c2.getRadius() + " and area of " + c2.getArea());
18+
+ c2.getRadius() + " and area of " + c2.getArea()
19+
+ " and color of " + c2.getColor());
20+
21+
// Declare and allocate an instance of class circle called c2
22+
// with the given radius and default color
23+
Circle c3 = new Circle(2.0, "blue");
24+
// Use the dot operator to invoke methods of instance c2.
25+
System.out.println("The circle has radius of "
26+
+ c3.getRadius() + " and area of " + c3.getArea()
27+
+ " and color of " + c3.getColor());
28+
29+
30+
Circle c4 = new Circle(); // construct an instance of Circle
31+
c4.setRadius(5.0); // change radius
32+
c4.setColor("green"); // change color
33+
34+
System.out.println("The circle has radius of "
35+
+ c4.getRadius() + " and area of " + c4.getArea()
36+
+ " and color of " + c4.getColor());
37+
38+
Circle c5 = new Circle(5.0);
39+
System.out.println(c5.toString()); // explicit call
40+
41+
Circle c6 = new Circle(1.2);
42+
System.out.println(c6); // println() calls toString() implicitly, same as above
43+
System.out.println("Operator '+' invokes toString() too: " + c6); // '+' invokes toString() too
1844
}
1945
}

0 commit comments

Comments
 (0)