-2
public class CellAtt {
    private String brand;
    private long serial;
    private double price;

    public CellAtt(String brand, long serial, double price) {
        this.brand = brand;
        this.serial = serial;
        this.price = price;
    }
    public boolean Compare(Object c1,Object c2) {

    }

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CellAtt c1 = new CellAtt("nokia",4536895,3600.00);
        CellAtt c2 = new CellAtt("Samsung",4536895,3600.00);
    }
}

I have created two objects in main.java.

I want to compare two object properties, Brand and Price. If brand and price are the same, it must return true, or else false. Hope I'm clear.

CarenRose
  • 1,142
  • 11
  • 18
Srjk
  • 19
  • 1
  • 5
  • What is the meaning of comparing Brand and Price? – Blasanka Jul 15 '17 at 04:34
  • This is from my assignments, the question is in this way.. "The design should allow for one cell phone to be compared to another cell phone for equality. Two cell phone objects are considered equal if they have the same brand and the same price." – Srjk Jul 15 '17 at 04:39
  • So your assignment include to compare `"nokia" == 3600.00`? – Blasanka Jul 15 '17 at 04:40
  • Ok ok I get it now. You are asking something like this `obj1.price == obj2.price` and `obj1.brand.equals(obj2.brand)`. Since your attributes are `private`, you have implemented a method and you do not know how to implement that method. Is that the question? – Blasanka Jul 15 '17 at 04:42
  • No, They are two objects if the brand of object 1 is equal to brand of object 2 then true and same procedure follows with the price – Srjk Jul 15 '17 at 04:46
  • Exactly. How to implement it? – Srjk Jul 15 '17 at 04:49

6 Answers6

0

In your Compare method:

public boolean Compare(Object c1,Object c2){
   c1 = (CellAtt)c1;
   c2 = (CellAtt)c2; 
} 

method return true if c1.brand.equals(c2.brand) && c1.price == c2.price else return false.

Alternatively, you can override equals() method of Object class

sfjac
  • 6,386
  • 4
  • 42
  • 62
Isha Agarwal
  • 420
  • 4
  • 11
0

The best way to tackle this problem would be to override the Object.equals() function which is built for this very specific purpose. More details cand be found here: https://www.tutorialspoint.com/java/lang/object_equals.htm

0

Best and good way to do this is, override the equals() method in Object class:

public boolean equals(Object c){
    if (c == null)
        return false;

    if (!CellAtt.class.isAssignableFrom(c.getClass()))
        return false;

    CellAtt obj = (CellAtt) c;

    return this.brand.equals(obj.brand) && this.price == obj.price;
}

read this to understand how to override equals() in Object class?

If you strictly want to implement a method by yourself do this:

Modify the compare() in CellAtt class.

public boolean compare(CellAtt c){
    if (c == null)
        return false;

    return this.brand.equals(c.brand) && this.price == c.price;
}

Then in the Main class, you can invoke the method like below:

boolean res = c1.compare(c2);
System.out.println(res);//this is added to check output

UPDATE

Comlete code:

CellAtt class:

public class CellAtt {
    private String brand;
    private long serial;
    private double price;

    public CellAtt(String brand, long serial, double price) {
        this.brand = brand;
        this.serial = serial;
        this.price = price;
    }

    @Override
    public boolean equals(Object c){
        if (c == null)
            return false;

        if (!CellAtt.class.isAssignableFrom(c.getClass()))
            return false;

        CellAtt obj = (CellAtt) c;

        return this.brand.equals(obj.brand) && this.price == obj.price;
    }

    //public boolean compare(CellAtt c){
    //    if (c == null)
    //        return false;
    //
    //    return this.brand.equals(c.brand) && this.price == c.price;
    //}
}

Main class:

public class Main {
    public static void main(String[] args) {
        CellAtt c1 = new CellAtt("nokia",4536895,3600.00);
        CellAtt c2 = new CellAtt("samsung",4536895,3600.00);

        boolean res = c1.equals(c2);
//      boolean res = c1.compare(c2);
        System.out.println(res);
    }
}
Blasanka
  • 14,208
  • 6
  • 76
  • 90
-1
public boolean Compare(Object c1,Object c2) {
    return c1.brand == c2.brand && c1.price == c2.price;
}
xlm
  • 4,594
  • 13
  • 42
  • 47
Nonsouris
  • 3
  • 3
-1
public  boolean Compare(Object c1, Object c2) {

        if(c1 == null || c2 == null)
            return false;

        CellAtt ca1=(CellAtt)c1;
        CellAtt ca2=(CellAtt)c2;

        if(ca1.brand.equals(ca2.brand) && ca1.price == ca2.price)
            return true;

    return false;
}
anil_g
  • 54
  • 1
  • 8
-1
public class CellAtt {
    private String brand;
    private long serial;
    private double price;

    public CellAtt(String brand, long serial, double price) {
        this.brand = brand;
        this.serial = serial;
        this.price = price;
    }

    @Override
    public int hashCode() {
        int hash = 42;
        hash = 29 * hash + Objects.hashCode(this.brand);
        hash = 29 * hash + (int) (this.serial ^ (this.serial >>> 32));
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CellAtt other = (CellAtt) obj;
        if (this.serial != other.serial) {
            return false;
        }
        if (!Objects.equals(this.brand, other.brand)) {
            return false;
        }
        return true;
    }

}
Almas Abdrazak
  • 2,335
  • 2
  • 25
  • 55