All pastes #618899 Raw Edit

Proves What?

public java v1 · immutable
·published 2007-07-14 08:21 UTC
rendered paste body
public class Test {    public static void main(String[] args) {        JObject j = new JObject("j");        JObject k = new JObject("k", 19);        System.out.println("");        System.out.println("Test 1:   ");             System.out.println("[ JObject j = new JObject('j'); JObject k = new JObject('k', 19); ]");        System.out.println("[ KObject a = new KObject ('farming', 100); KObject n = new KObject ('farming', 100); ]");        KObject a = new KObject ("farming", 100);        KObject b = new KObject ("fishing", 30);                System.out.println ("a.equals (b) : " + a.equals (b));        System.out.println ("b.equals (a) : " + b.equals (a));        System.out.println("");        System.out.println("Test 2:   ");                System.out.println("[JObject x = a; JObject y = b]");        JObject x = a;        JObject y = b;                System.out.println ("a.equals (y): " + a.equals (y));        System.out.println ("a.equals (x): " + a.equals (x));        System.out.println ("b.equals (y): " + b.equals (y));        System.out.println ("b.equals (x): " + b.equals (x));                System.out.println ("Test 2, reversed:");        System.out.println ("y.equals (b): " + y.equals (b));        System.out.println ("y.equals (a): " + y.equals (a));        System.out.println ("x.equals (b): " + x.equals (b));        System.out.println ("x.equals (a): " + x.equals (a));                System.out.println ("Test 3:");        System.out.println ("x.equals (y): " + x.equals (y));        System.out.println ("y.equals (x): " + y.equals (x));            }}class JObject {    private String name;     private int age;    public JObject() {        this("Unknown", 0);    }    public JObject(String name) {        this(name, 0);    }    public JObject(String name, int age) {        this.name = name;        this.age = age;    }    public String getName() {        return this.name;    }    public int getAge() {        return this.age;    }    public void setName(String name) {        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public boolean equals(JObject obj) {        return (this.compareTo(obj) == 0);    }    public int compareTo(JObject obj) {        int comparison = 0;        if(this.age == obj.age) {            // Could just as equally be case sensative... doesn't matter.            comparison = this.name.compareToIgnoreCase(obj.name);        } else {            if(this.age < obj.age) {                comparison = -1;            } else {                comparison = 1;            }        }        return comparison;    }    public int hashCode() {        return Integer.parseInt(this.name.hashCode() + "" + this.age);    }    public String toString() {        return ("JObject( name = '" + this.name + "', age = " + this.age + " )");    }}class KObject extends JObject {    private String skill;    private int speed;    public KObject(String skill, int speed) {        this(skill, speed, "Unknown");    }    public KObject(String skill, int speed, String name) {        this(skill, speed, name, 0);    }    public KObject(String skill, int speed, String name, int age) {        this.skill = skill;        this.speed = speed;        this.setName(name);        this.setAge(age);    }    public String getSkill() {        return this.skill;    }    public int getSpeed() {        return this.speed;    }    public void setSkill(String name) {        this.skill = name;    }    public void setSpeed(int age) {        this.speed = age;    }    public boolean equals(KObject obj) {        return (this.compareTo(obj) == 0);    }    public int compareTo(KObject obj) {        int comparison = 0;        if(super.equals(obj)) {            if(this.skill.equalsIgnoreCase(obj.skill)) {                // Could just as equally be case sensative... doesn't matter.                comparison = this.speed - obj.speed;            } else {                comparison = this.skill.compareToIgnoreCase(obj.skill);            }        } else {            comparison = super.compareTo(obj);        }        return comparison;    }    public int hashCode() {        return Integer.parseInt(super.hashCode() + "" + this.skill.hashCode() + this.speed);    }    public String toString() {        return ("KObject( name = '" + this.getName() + "', age = " + this.getAge() + ", skill = " + this.skill + ", speed = " + this.speed + " )");    }}