Skip to content

Commit 0e646fc

Browse files
author
java-tester-x
committed
Add some task
1 parent d00db76 commit 0e646fc

File tree

6 files changed

+178
-1
lines changed

6 files changed

+178
-1
lines changed

src/Book.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ public void setQtyInStock(int qtyInStock) {
5555
this.qtyInStock = qtyInStock;
5656
}
5757

58+
public String getAuthorName() {
59+
return this.author.getName();
60+
}
61+
62+
public String getAuthorEmail() {
63+
return this.author.getEmail();
64+
}
65+
66+
public char getAuthorGender() {
67+
return this.author.getGender();
68+
}
69+
5870
public String toString() {
5971
return "'" + name +"' by " + author;
60-
}
72+
}
6173
}

src/MyCircle.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package src;
2+
3+
public class MyCircle {
4+
5+
private MyPoint center;
6+
private int radius = 1;
7+
8+
public MyCircle(int x, int y, int radius) {
9+
this.center = new MyPoint(x, y);
10+
this.radius = radius;
11+
}
12+
13+
public MyCircle(MyPoint center, int radius) {
14+
this.center = center;
15+
this.radius = radius;
16+
}
17+
18+
public int getRadius() {
19+
return this.radius;
20+
}
21+
22+
public void setRadius(int radius) {
23+
this.radius = radius;
24+
}
25+
26+
public MyPoint getCenter() {
27+
return this.center;
28+
}
29+
30+
public void setCenter(MyPoint center) {
31+
this.center = center;
32+
}
33+
34+
public int getCenterX() {
35+
return this.center.getX();
36+
}
37+
38+
public int getCenterY() {
39+
return this.center.getY();
40+
}
41+
42+
public void setCenterXY(int x, int y) {
43+
this.center.setXY(x, y);
44+
}
45+
46+
public double getArea() {
47+
return Math.PI * radius * radius;
48+
}
49+
50+
public String toString() {
51+
return "Circle @ "+this.center+" radius="+this.radius;
52+
}
53+
}

src/MyPoint.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package src;
2+
3+
public class MyPoint {
4+
5+
private int x = 0;
6+
private int y = 0;
7+
8+
public MyPoint() {}
9+
10+
public MyPoint(int x, int y) {
11+
this.x = x;
12+
this.y = y;
13+
}
14+
15+
public int getX() {
16+
return this.x;
17+
}
18+
19+
public void setX(int x) {
20+
this.x = x;
21+
}
22+
23+
public int getY() {
24+
return this.y;
25+
}
26+
27+
public void setY(int y) {
28+
this.y = y;
29+
}
30+
31+
public void setXY(int x, int y) {
32+
this.x = x;
33+
this.y = y;
34+
}
35+
36+
public double distance(int x, int y) {
37+
int xDiff = this.x - x;
38+
int yDiff = this.y - y;
39+
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
40+
}
41+
42+
public double distance(MyPoint another) {
43+
int xDiff = this.x - another.getX();
44+
int yDiff = this.y - another.getY();
45+
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
46+
}
47+
48+
public String toString() {
49+
return "(" + this.x + ", " + this.y + ")";
50+
}
51+
52+
}

src/TestBook.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ public static void main(String[] args)
1212
"more Java for dummy"
1313
, new Author("Bruce Eckel", "b.eckel@somewhere.com", 'm')
1414
, 29.95, 888);
15+
16+
// additional task #1
17+
System.out.println("Java for dummy author name is: " + aBook.getAuthor().getName());
18+
System.out.println("Java for dummy author email is: " + aBook.getAuthor().getEmail());
19+
20+
// additional task #2
21+
System.out.println("Java for dummy author name is: " + aBook.getAuthorName());
22+
System.out.println("Java for dummy author email is: " + aBook.getAuthorEmail());
23+
System.out.println("Java for dummy author gender is: " + aBook.getAuthorGender());
1524
}
1625
}

src/TestMyCircle.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package src;
2+
3+
public class TestMyCircle {
4+
5+
public static void main(String[] args)
6+
{
7+
MyCircle c1 = new MyCircle(3, 0, 3);
8+
MyCircle c2 = new MyCircle(new MyPoint(1,1), 5);
9+
10+
System.out.println("Circle c1: "+ c1);
11+
System.out.println("Circle c2: "+ c2);
12+
13+
c1.setRadius(5);
14+
System.out.println("Radius of circle c1 now is "+ c1.getRadius());
15+
16+
c2.setCenterXY(3, 3);
17+
System.out.println("Center of circle c2 now is "+ c2.getCenter());
18+
19+
c1.setCenter(new MyPoint(4,4));
20+
System.out.println("Center of circle c1 now is "+ c1.getCenter());
21+
22+
23+
System.out.println("Area of circle c1 is "+ c1.getArea());
24+
System.out.println("Area of circle c2 is "+ c2.getArea());
25+
}
26+
}

src/TestMyPoint.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package src;
2+
3+
public class TestMyPoint {
4+
5+
public static void main(String[] args)
6+
{
7+
// additional task #1
8+
MyPoint p1 = new MyPoint(3, 0);
9+
MyPoint p2 = new MyPoint(0, 4);
10+
11+
// Testing the overloaded method distance()
12+
System.out.println(p1.distance(p2)); // which version?
13+
System.out.println(p1.distance(5, 6)); // which version?
14+
15+
// additional task #2
16+
MyPoint[] points = new MyPoint[10];
17+
for (int i= 0; i < points.length; i++ ) {
18+
points[i] = new MyPoint((i+1), (i+1));
19+
}
20+
21+
for (MyPoint p : points) {
22+
System.out.println("Distance between p1"+ p1 +" and p"+ p +" is "+ p1.distance(p));
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)