I have two arrays of objects Arr1 is Array with competitors, Arr2 is Array with obstacles like walls and tracks.
I need to iterate through every competitor going through obstacles jumping and running. If competitor can finish whole track it should print something like:
"John jumped over wall"
"John finished track"
"John jumped over wall"
When competitor can't jump or finish track program should say
"John failed to jump over wall"
and stop iterating through other obstacles.
Obstacle[] obstacles = {
new Track(random.nextInt(100, 3000)),
new Wall(random.nextDouble(0.5, 2.0)),
new Track(random.nextInt(100, 3000)),
new Wall(random.nextDouble(0.5, 2.0)),
};
List<Obstacle> obstacleList = new ArrayList<>(List.of(obstacles));
Competor[] competors = {
new Human("John", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
new Cat("Snowball", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
new Robot("Optimus", random.nextDouble(0.5, 2.0)*10/10, random.nextInt(100, 3000)),
};
List<Competor> competorList = new ArrayList<>(List.of(competors));
for (Competor c : competorList) {
for (Obstacle o : obstacleList) {
if (o instanceof Wall) {
c.jump(((Wall) o).getM());
}
if (o instanceof Track) {
c.run(((Track) o).getM());
}
}
}
With this code I'm getting something like this:
Human John have successfully jumped over '0,7'm. wall.
Human John have successfully finished '509'm. track.
Human John failed to jump over '1,9'm. wall.
Human John failed to finish '2439'm. track.
Cat Snowball failed to jump over '0,7'm. wall.
Cat Snowball have successfully finished '509'm. track.
Cat Snowball failed to jump over '1,9'm. wall.
Cat Snowball failed to finish '2439'm. track.
Robot Optimus have successfully jumped over '0,7'm. wall.
Robot Optimus have successfully finished '509'm. track.
Robot Optimus failed to jump over '1,9'm. wall.
Robot Optimus failed to finish '2439'm. track.
And I need like this:
Human John have successfully jumped over '0,7'm. wall.
Human John have successfully finished '509'm. track.
Human John failed to jump over '1,9'm. wall.
Cat Snowball failed to jump over '0,7'm. wall.
Robot Optimus have successfully jumped over '0,7'm. wall.
Robot Optimus have successfully finished '509'm. track.
Robot Optimus failed to jump over '1,9'm. wall.
Robot, Cat, Human is just classes based on mother Class Competor
(competitor):
public class Human extends Competor implements Run, Jump {
String type;
String name;
double jumpHeight;
int runDistance;
public Human(String name, double jumpHeight, int runDistance) {
this.type = this.getClass().getSimpleName();
this.name = name;
this.jumpHeight = jumpHeight;
this.runDistance = runDistance;
}
public double getJumpHeight() {
return jumpHeight;
}
public int getRunDistance() {
return runDistance;
}
@Override
public void jump(double height) {
if (jumpHeight > height) {
System.out.printf("%s %s have successfully jumped over '%.1f'm. wall. \n", type, name, height);
} else {
System.out.printf("%s %s failed to jump over '%.1f'm. wall. \n", type, name, height);
}
}
@Override
public void run(int distance) {
if (runDistance > distance) {
System.out.printf("%s %s have successfully finished '%d'm. track. \n", type, name, distance);
} else {
System.out.printf("%s %s failed to finish '%d'm. track. \n", type, name, distance);
}
}
}
Wall and track:
public class Wall extends Obstacle {
double height;
public double getM() {
return height;
}
public Wall(double height) {
this.height = height;
}
}
public class Track extends Obstacle{
int distance;
public int getM() {
return distance;
}
public Track(int distance) {
this.distance = distance;
}
}
i've also tried this since i can't remove elements from list while iterating:
ListIterator<Competor> competorListIter = competorList.listIterator();
while(competorListIter.hasNext()) {
for (Obstacle o : obstacleList){
if (o instanceof Wall) {
if (((Wall) o).getM() > competorListIter.next().getJumpHeight()) {
competorListIter.next().jump(((Wall) o).getM());
competorListIter.remove();
} else {
competorListIter.next().jump(((Wall) o).getM());
}
}
if (o instanceof Track) {
if (((Track) o).getM() > competorListIter.next().getRunDistance()) {
competorListIter.next().run(((Track) o).getM());
competorListIter.remove();
} else {
competorListIter.next().run(((Track) o).getM());
}
}
}
}
but this gives NoSuchElementException((( I guess thats because it is removing elements and loop still wants to iterate through them
CodePudding user response:
You can use break
operator to stop loop iteration. Example:
int[] heightArr = new int[] {3, 1, 4, 5};
int carlJumpHeight = 3;
for (int height : heightArr) {
if (carlJumpHeight < height) {
System.out.println("Carl can't jump so high!");
break;
}
}
CodePudding user response:
to elaborate on my comment, what you could do is something like this
public boolean passesJump(double height) {
if (jumpHeight > height) {
System.out.printf("%s %s have successfully jumped over '%.1f'm. wall. \n", type, name, height);
return true;
} else {
System.out.printf("%s %s failed to jump over '%.1f'm. wall. \n", type, name, height);
return false;
}
}
and then
for (Competor c : competorList) {
for (Obstacle o : obstacleList) {
if (o instanceof Wall && !c.passesJump(((Wall) o).getHeight());
break;
}
if (o instanceof Track && !c.passesRun(((Track) o).getDistance()) {
break;
}
}
}
or you simplify the passesX
methods, e.g.
@Override
public void passesRun(double height) {
return jumpHeight > height;
}
and do the printing elsewhere.