import java.util.LinkedList;
import static java.lang.System.*;
class Cannon {
String name;
public boolean loaded;
public Cannon(String name, boolean loaded) {
this.name = name;
this.loaded = loaded;
}
public String getName() {
return name;
}
public boolean fire() {
if (loaded == true) {
out.println("Cannon loaded");
} else {
out.println("Load cannon");
}
return loaded;
}
}
public class Program {
public static void main(String[] args) {
LinkedList<Cannon> cannons = new LinkedList<>();
cannons.add(new Cannon("cannon1", true));
cannons.add(new Cannon("cannon2", false));
cannons.add(new Cannon("cannon3", false));
cannons.add(new Cannon("cannon4", true));
cannons.add(new Cannon("cannon5", false));
cannons.add(new Cannon("cannon6", true));
out.println("NIezaładowane działa");
for (Cannon cannon : cannons) {
if (cannon.fire() == false) {
out.println(cannon.getName());
}
}
}
}
I want to see which cannons are not loaded
and I should load the unloaded ones
but I haven't gotten to that step yethow to improve cannons so that they display the correct result ?
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa L
31 May 2022, 08:47
you should write these methods
void isLoaded() -> should return the current loaded value
void fire() -> sets loaded to false and outputs that the cannon has fired
void load() -> sets loaded to true and outputs that the cannon has been loaded
now change your loop a little bit
if the current cannon isLoaded fire(), else load()
0
Karol Grzeszczak
31 May 2022, 09:58
I messed it up, but at least at this point it shows me the loaded cannons
in the line for unloaded: D
and only one type not like before
in last minut i changed
so i get propper result
I don't know if I am thinking correctly
ok i have to go to work
0
Lisa L
31 May 2022, 10:10
+1
Karol Grzeszczak
1 June 2022, 05:26
Now i understand ;) thank you
0