Вот класс Cirkle
, который вам нужно создать (как я написал в своем комментарии к вашему вопросу). Я назвал его Cirkle , чтобы не путать его с другими существующими классами с именем Circle
, которые являются частью библиотеки классов JDK. Класс содержит радиус круга и координаты X и Y его центра.
public class Cirkle {
private int centerX;
private int centerY;
private int radius;
public Cirkle() {
this(0);
}
public Cirkle(int radius) {
this(radius, radius);
}
public Cirkle(int radius, int center) {
this(radius, center, center);
}
public Cirkle(int radius, int centerX, int centerY) {
this.radius = radius;
this.centerX = centerX;
this.centerY = centerY;
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public int getRadius() {
return radius;
}
public boolean equals(Object obj) {
boolean equal = false;
if (obj != null) {
Class<?> objClass = obj.getClass();
if (objClass.equals(getClass())) {
Cirkle other = (Cirkle) obj;
equal = radius == other.radius &&
centerX == other.centerX &&
centerY == other.centerY;
}
}
return equal;
}
public int hashCode() {
return (radius * 100) + (centerX * 10) + centerY;
}
public String toString() {
return String.format("Cirkle: radius = %d, center(%d, %d)", radius, centerX, centerY);
}
}
- Метод
toString()
используется для «отображения» Cirkle
объекта. - Метод
equals()
определяет, что два Cirkle
объекта равны, если они оба имеют одинаковый радиус и центр. - Метод
hashCode()
требуется при переопределении метода equals()
.
Теперь вам просто нужно заменить «мешок» строк в классе LinkedBagDemo1
на мешок Cirkle
s, как показано ниже в моей модифицированной версии класса, которая появляется в вашем вопросе.
public class LinkedBagDemo1 {
public static void main(String[] args) {
System.out.println("Creating an empty bag.");
BagInterface<Cirkle> aBag = new LinkedBag1<>();
testIsEmpty(aBag, true);
displayBag(aBag);
Cirkle[] contentsOfBag = new Cirkle[]{new Cirkle(5),
new Cirkle(5, 20),
new Cirkle(5, 100, 50)};
testAdd(aBag, contentsOfBag);
testIsEmpty(aBag, false);
}
private static void testIsEmpty(BagInterface<Cirkle> bag, boolean empty) {
System.out.print("\nTesting isEmpty with ");
if (empty)
System.out.println("an empty bag:");
else
System.out.println("a bag that is not empty:");
System.out.print("isEmpty finds the bag ");
if (empty && bag.isEmpty())
System.out.println("empty: OK.");
else if (empty)
System.out.println("not empty, but it is: ERROR.");
else if (!empty && bag.isEmpty())
System.out.println("empty, but it is not empty: ERROR.");
else
System.out.println("not empty: OK.");
}
private static void testAdd(BagInterface<Cirkle> aBag, Cirkle[] content) {
System.out.print("Adding the following Cirkles to the bag: ");
for (int index = 0; index < content.length; index++) {
if (aBag.add(content[index]))
System.out.print(content[index] + " ");
else
System.out.print("\nUnable to add " + content[index] + " to the bag.");
}
System.out.println();
displayBag(aBag);
}
private static void displayBag(BagInterface<Cirkle> aBag) {
System.out.println("The bag contains the following Cirkle(s):");
Object[] bagArray = aBag.toArray();
for (int index = 0; index < bagArray.length; index++) {
System.out.print(bagArray[index] + " ");
}
System.out.println();
}
}
А вот вывод, когда я запускаю класс LinkedBagDemo1
Creating an empty bag.
Testing isEmpty with an empty bag:
isEmpty finds the bag empty: OK.
The bag contains the following string(s):
Adding the following strings to the bag: Cirkle: radius = 5, center(5, 5) Cirkle: radius = 5, center(20, 20) Cirkle: radius = 5, center(100, 50)
The bag contains the following string(s):
Cirkle: radius = 5, center(100, 50) Cirkle: radius = 5, center(20, 20) Cirkle: radius = 5, center(5, 5)
Testing isEmpty with a bag that is not empty:
isEmpty finds the bag not empty: OK.