-1

I am trying to Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown needs to be identified by all object data String for their name, int id and double size. I join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (i.e., the first one) can leave the Parade. In addition, I write a method called isFront that takes a Clown as parameter and returns true if the passed clown is at the front of the parade otherwise returns false. Create a test application to demonstrate building a parade of three or four clowns and include your own name. Then, remove one or two, and add another one or two. Also, test the isFront method by passing different clowns to the method.

I have a code but it is not returning true for the isFront method, I am trying to use contains method I also tried to use Comparable interface Clown but it did not work that well. Not sure what to do.

import java.util.ArrayList;
public class Main
{
    public static void main(String[] args)
    {
        Parade circus = new Parade();

        circus.addClown(new Clown("Bobby",9,12.0));
        circus.addClown(new Clown("Clair", 2, 11.0));
        circus.addClown(new Clown("Tony",6,10.0));
        circus.addClown(new Clown("Sarah",3,5.0));
        circus.display();
        System.out.println(circus.isFront(new Clown("Bobby", 9, 12.0)));

    }

}
import java.util.ArrayList;
public class Parade
{
   private static ArrayList<Clown> parade;
   private int top;
   public Parade()
   {
      top=0;
      parade= new ArrayList<Clown>();
      System.out.println(parade);

   }
   public void addClown(Clown c)
   {
     parade.add(c);
     top++;
   }
   public void removeClown()   //(Clown c)
   {
     parade.remove(0);
     top--;
   }
   public void display()
   {
     System.out.println(parade);
   }
   public void getData()
   {
      parade.get(0);
   }
   public void setData()
   {
      parade.set(1,new Clown("Claire",2,5.0));
      System.out.println(parade);
   }
   public int getTop()
   {
      return top;
   }
   public boolean isFront(Clown c)
   {
      return !parade.isEmpty() && c.equals(parade.get(0));
   }

   //exceptions
}

public class Clown
{
    private String name;
    private int id;
    private double size;

    public Clown(String name, int id, double size)
    {
        this.name=name;
        this.id=id;
        this.size=size;
    }
    public String getName()
    {
        return name;
    }
    public int getId()
    {
        return id;
    }
    public double getSize()
    {
        return size;
    }
    public String toString()
    {
        return name.toString() + id + size;
    }
    public boolean equals(Object o) {
        if (o instanceof Clown c) {
            return this.getName().equals(c.getName()) && this.getId() == c.getId() && this.getSize() == c.getSize();
        }
        return false;
    }

}

their is not much info in our textbook about this stuff Java FOundations 5th e Lewis like working with objects and arraylists it skips it and assumes you already know it lol..

Read_Shevy
  • 35
  • 6
  • Your source code needs better formatting. Get the indents right, and separate the classes with blank lines. – Basil Bourque Jan 03 '21 at 05:43
  • when I pasted it in from my ide it was indented after it made me do all that odd formatting it changed it and wouldnt let me post.. – Read_Shevy Jan 03 '21 at 05:46
  • 2
    Formatting your code on Stack Overflow is quite simple, either indent 4 spaces or surround with triple back-ticks. Read the Help pages, and hit the “Edit” buttons on existing questions/answers for examples. – Basil Bourque Jan 03 '21 at 05:56
  • 1
    You are creating a new clown for the isFront() call. This would only work if you override Clown.equals() in order to test if the two clowns have the same data; the default equals() checks if they are the same object rather than that they have the same data. – NomadMaker Jan 03 '21 at 08:57
  • Why is your `ArrayList parade` static? – Amal K Jan 03 '21 at 11:28
  • According to your question, isn't `"Bobby"` the first clown for which `isFront()` should return `true`? – Amal K Jan 03 '21 at 11:32
  • What are additional features of Parade like top, data, etc.? They are not required, why did you use them in your class? – Roman C Jan 03 '21 at 20:57

1 Answers1

1

Firstly, objects in Java are, by default, compared by reference. So, even if you create two Clown objects with the exact same properties, Java sees them as different objects because both those object references are not the same, they are both referring to different memory locations. You can override this behavior and ask Java to compare it as you want by overriding the equals() method of the Object class:

public class Clown {
    private String name;
    private int id;
    private double size;

    public Clown(String name, int id, double size) {
        this.name=name;
        this.id=id;
        this.size=size;
    }
    public String getName() {
        return name;
    }
    public int getId() {
        return id;
    }
    public double getSize() {
        return size;
    }
    @Override
    public boolean equals(Object o) {
        if (o instanceof Clown) {
             Clown c = (Clown) o;
             return this.getName().equals(c.getName());
        } 
        return false;
    }
    @Override
   public int hashCode() {
        return this.getId();
    }
    @Override
    public String toString() {
        return name.toString() + id + size;
    }
}

This will help with contains()(it internally uses equals()). Secondly, you can just compare your clown with the first clown to see if it is the one at the front:

public boolean isFront(Clown c) {
    return !parade.isEmpty() && c.equals(parade.get(0));
}

The isFront() method will return true if the parade is not empty and the clown c is equal to to the first clown in the parade. get(0) retrieves the first clown in the parade. As per your comment, if you want that two clowns be equal only if all their properties are equal, change your equals method to:

@Override
public boolean equals(Object o) {
   if (o instanceof Clown) {
         Clown c = (Clown) o;
         return this.getName().equals(c.getName()) &&
                this.getId() == c.getId()          &&
                this.getSize() == c.getSize();
    } 
    return false;
}

The equals() method is of the Object class which is the parent class of all Java classes. It defines how to compare two objects. Its signature is as follows:

    public boolean equals(Object obj) 

As we're overriding, its signature must be the same in the derived class, in our case in class Clown. Its parameter is of type Object not Clown. Any type can be converted to Object, if I compare an object of Clown to another type, like:

Clown c = new Clown("X", 1, 10);
 if ( c.equals(objectOfAnotherType) ) {..}

it will still work. So we use the instanceof operator to check if that another object is also a Clown. If it is not an instance of Clown, we return false but if it is, we convert/cast that object to Clown, only then we can call getName() and other getter methods:

if (o instanceof Clown) {
         Clown c = (Clown) o; //Casting happens here
         return this.getName().equals(c.getName()) &&
                this.getId() == c.getId()          &&
                this.getSize() == c.getSize();
    } 
    return false;

Java 14 introduced a shortcut for this, instead of these steps:

if (o instanceof Clown) {
         Clown c = (Clown) o;

we can simply write:

if (o instance of Clown c)

which does the casting for us and stores it in c.

Lastly, I have also overriden Object.hashCode() because you have to when you override equals(), here's why.

Amal K
  • 4,359
  • 2
  • 22
  • 44
  • @Bluemustang Can you describe the error you're getting? – Amal K Jan 04 '21 at 04:18
  • I have updated the `equals()` method, check with the updated code. – Amal K Jan 04 '21 at 05:03
  • Yes, pattern matching is not supported in Java 11. I have updated `equals()` to work in Java 11. – Amal K Jan 04 '21 at 06:04
  • Yes, the methods are in the right classes. `isFront()` is a characteristic of Parade. And `equals()` is a characteristic of Clown. – Amal K Jan 04 '21 at 06:07
  • Yes, use that for Java 11. Is it working now? – Amal K Jan 04 '21 at 06:24
  • Pick one `equals()` method based on how you wanna compare. It will both work now in Java 11. – Amal K Jan 04 '21 at 06:29
  • Pleasure is all mine, I've included some explanation to make it a bit clearer, hope it helps. And that's very kind of you, you absolutely don't have to do that, you don't owe me anything. Here's my PayPal if you wish to anyway: paypal.me/amalkay – Amal K Jan 04 '21 at 16:59