-
Notifications
You must be signed in to change notification settings - Fork 1
/
4.11.7: Comparing Circles
78 lines (68 loc) · 1.78 KB
/
4.11.7: Comparing Circles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class CircleTester extends ConsoleProgram
{
public void run()
{
Circle one = new Circle(10, "blue", 50, 35);
Circle two = new Circle(10, "blue", 50, 35);
Circle three = new Circle(20, "red", 0, 0);
Circle four = three;
// Modify this program to correctly compare objects
// We should not be comparing objects using ==
if(one == two)
{
System.out.println("Circles one and two are equal!");
System.out.println(one);
System.out.println(two);
}
if(three == four)
{
System.out.println("Circles three and four are equal!");
System.out.println(three);
System.out.println(four);
}
}
}
_____________________________________________________________________
public class Circle
{
private int radius;
private String color;
private int x;
private int y;
public Circle(int theRadius, String theColor, int xPosition, int yPosition)
{
radius = theRadius;
color = theColor;
x = xPosition;
y = yPosition;
}
public int getRadius()
{
return radius;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public String getColor()
{
return color;
}
// Implement a toString method and an equals method here!
public String toString()
{
return color + " circle with a radius of " + radius + " at position (" + x + ", " + y + ")";
}
public boolean equals(Circle other)
{
// Change this!
if( toString () .equals(other.toString() ) )
return true;
else
return false;
}
}