I will start by explaining the scenario.
I have to create a Vector to hold a list of Circles.
Then I create a circle object, and add it to the Vector.
Finally I have to print the state of each circle in the list (the state isn’t important to define, just it’s colour and position etc).
Basically my problem is, how do I hold the circles so that I can then pass in the name of one of them and it will print the correct circle’s state. It may be clearer to show you my code.
I have 3 classes, that main one called Lab83Test, the Vector one, called CircleList, and the Circle one, called Circle. (The formatting has gone weird and I can’t fix it, so apologies!)
Lab83Test:
public class Lab83Test {
public static void main(String[] args) {
makeNewCircle();
}
public static void makeNewCircle() {
CircleList newList = new CircleList();
Circle newCircle = new Circle();
newList.addCircle(newCircle);
newCircle.makeVisible();
newList.printDetails();
}
}
CircleList:
import java.util.Vector;
public class CircleList {
private Vector circleVector;
public CircleList() {
circleVector = new Vector();
}
public void addCircle(Circle circleName) {
circleVector.add(circleName);
printDetails();
}
public void addCircleToPlace(Circle circleName, int pos) {
circleVector.add(pos, circleName);
printDetails();
}
public void removeCircleFromPos(int pos) {
circleVector.remove(pos);
printDetails();
}
public void removeAllCircles(int pos) {
circleVector.clear();
printDetails();
}
public void printDetails() {
}
}
The circle class isn’t too important, and it has a lot of methods for moving the circle and stuff. The main thing is that it has to return its state.
If you just want to iterate over all
Circelin your list and print their name, you can do it this way:I don’t know how your
Circleclass looks so I just assumed some method names.If you want to find a specific circle by name:
Or you could use a
Map<String, Circle>instead of your vector and just doreturn circleMap.get(name);. YouraddCircle()would then look like this:Edit1: regarding your comment: Your vector does not have a defined type. Change this: