In: Computer Science
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown only needs to be identified by a String for her/his name. Always join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (the first one) can leave the Parade. Create a test application to demonstrate building a parade of 3 or 4 clowns (include your own name), then removing 1 or 2, then adding another 1 or 2.
Thanks for the question. Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change. Thank You !! Note: Please add your name in the placeholder given in the main class and please do up Vote : ) =================================================================================================== public class Clown { private String clownName; public Clown(String clownName) { this.clownName = clownName; } public String getClownName() { return clownName; } @Override public String toString() { return "Clown: " + getClownName(); } }
======================================================================================
import java.util.ArrayList; public class Parade { private ArrayList<Clown> clowns; public Parade() { this.clowns = new ArrayList<Clown>(); } // add clown at the end public void addClown(Clown clown){ clowns.add(clown); System.out.println(clown+" added."); } public void removeClown(){ // remove the clown at index 0 always if there is at least one clown if(clowns.size()!=0){ Clown c = clowns.get(0); clowns.remove(0); System.out.println(c+" removed."); }else{ System.out.println("There are no clowns."); } } }
======================================================================================
public class Boulevard { public static void main(String[] args) { Parade march = new Parade(); // add 4 clowns march.addClown(new Clown("Zeus")); march.addClown(new Clown("Othello")); march.addClown(new Clown("<My Name>")); march.addClown(new Clown("Dino")); // remove the first two that is Zeus and Othello march.removeClown(); march.removeClown(); // add two more march.addClown(new Clown("Frankestine")); march.addClown(new Clown("Macbeth")); // remove all clowns march.removeClown(); march.removeClown(); march.removeClown(); march.removeClown(); march.removeClown(); } }
======================================================================================