Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Object is a program-along lab. It consists of an enum called Size that has t

ID: 3861753 • Letter: L

Question

Lab Object is a program-along lab.

It consists of an enum called Size that has the following enum constants: XS, S, M, L, and XL,

a class Balloon (see video),

and a class BalloonApp.
BalloonApp compares 2 identical Balloon objects, 2 separate objects with the same content, and 2 different objects.
If you missed class I recommend to look at those different options and to see how the implementation of equals and hashCode affects the outcome of the comparison.

As part of the lab we also create a gui generates 2 random balloon objects and compares them with each other ( see video)

How do you acomplish this task?

Explanation / Answer

package myProject;
//Enumeration definition
enum Size { XS, S, M, L, XL }
//Class Ballon definition
class Ballon
{
   //Instance variable
   int size;
   //Constructor to initialize instance variable
   Ballon(int size)
   {
       this.size = size;
   }//end of constructor
  
   //Returns the hash code
   public int hashCode()
   {
       return size;
   }//End of method
  
   //Overrides equals method
   public boolean equals( Object obj )
   {
       //Initially flag is false
       boolean flag = false;
       //Converts the obj to Ballon type
       Ballon bal = (Ballon)obj;
       //Compares. If equal flag is set to true
       if( bal.size == size )
           flag = true;
       //Return the flag
       return flag;
   }//end of method
}//end of class

//Driver class
public class BalloonApp
{
   //Main method
   public static void main(String[] args)
   {
       //Converts the enumeration data to integer
       int size = Size.L.ordinal();
       //Converts the enumeration data to integer
       int size1 = Size.XL.ordinal();
      
       //Creates objects
       Ballon bal1 = new Ballon(size);
       Ballon bal2 = new Ballon(size);
       Ballon bal3 = new Ballon(size1);
      
       //Displays the information
       System.out.println("bal1.equals(abl2) = " + bal1.equals(bal2));
       System.out.println("bal1.hashCode() = " + bal1.hashCode());
       System.out.println("bal1.hashCode() = " + bal2.hashCode());
      
       System.out.println("bal1.equals(abl2) = " + bal1.equals(bal3));
       System.out.println("bal1.hashCode() = " + bal1.hashCode());
       System.out.println("bal1.hashCode() = " + bal3.hashCode());
   }//End of main
}//End of class

Output:

bal1.equals(abl2) = true
bal1.hashCode() = 3
bal1.hashCode() = 3
bal1.equals(abl2) = false
bal1.hashCode() = 3
bal1.hashCode() = 4