• Keine Ergebnisse gefunden

Exercise 4 The equals Method

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise 4 The equals Method"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Kathrin Geilmann

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 1

Date of Issue: 17.04.12

Deadline: - (before the lecture as PDF via E-Mail)

Exercise 1 Lecture concepts

Explain the following lecture concepts by giving reasonable examples:

• encapsulation & information hiding

• polymorphism & subtyping

• declarative & model-based specifications

• functional & non-functional properties

Exercise 2 Java Programming

Implement the University Administration System from the lecture slides 8-11 in Java.

Exercise 3 Identity vs Equality of Objects

a) Use the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf) to ex- plain the output of the following program

public class IdentityEquality {

public static void main(String ... args) {

String a1="A";

String a2="A";

String b1=new String("B");

String b2=new String("B");

System.out.println("a1==a2: "+(a1==a2));

System.out.println("b1==b2: "+(b1==b2));

System.out.println("a1.equals(a2): "+(a1.equals(a2)));

System.out.println("b1.equals(b2): "+(b1.equals(b2)));

} }

b) Modify the previous example such that an identity comparison is sufficient to test for equality. Note: Have a look at the Java API documentation of theStringclass.

c) Which property of theStringclass is necessary for the previous solution.

Exercise 4 The equals Method

The JDK description of the equals-Method is as follows:

Theequalsmethod implements an equivalence relation on non-null object references:

(2)

• It is reflexive: for any non-null reference value x,x.equals(x)should return true.

• It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x)returns true.

• It is transitive: for any non-null reference values x, y, and z, if x.equals(y)returns true andy.equals(z) returns true, thenx.equals(z)should return true.

• It is consistent: for any non-null reference values x and y, multiple invocations ofx.equals(y)consistently return true or consistently return false, provided no information used inequalscomparisons on the objects is modified.

• For any non-null reference value x,x.equals(null)should return false.

The equals method for classObjectimplements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x

==y has the value true).

Note that it is generally necessary to override thehashCodemethod whenever this method is overridden, so as to maintain the general contract for thehashCodemethod, which states that equal objects must have equal hash codes.

a) Analyze the following code fragment checking for the aforementioned properties.

public class Date { private int y,m,d;

public Date(int y, int m, int d) { this.y = y;

this.m = m;

this.d = d;

}

public boolean equals(Object obj) { if(obj instanceof Date) {

Date date = (Date)obj;

return date.y == y && date.m == m && date.d == d;

}

return false;

} }

public class NamedDate extends Date { private String name;

public NamedDate(int y,int m,int d, String name) { super(y,m,d);

this.name = name;

}

public boolean equals(Object other) {

if(other instanceof NamedDate && !name.equals(((NamedDate)other).name)) return false;

return super.equals(other);

} }

b) Consider the following implementation forDate.equals. What are the advantages and disadvantages of this solution?

public boolean equals(Object obj) { if (getClass() == obj.getClass()) {

Date date = (Date)obj;

return date.y == y && date.m == m && date.d == d;

}

return false;

}

Referenzen

ÄHNLICHE DOKUMENTE

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method

”Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that a ff

Declarative & Model-based Specifications Declarative specifications just specify which properties hold, they do not say anything about how the holding is achieved. In

Advanced Aspects of Object-Oriented Programming (SS 2011) Practice Sheet 1 Date of Issue: 19.04.11. Deadline: - (until 10 a.m. Note: Have a look at the Java API documentation of

Our results are based on a new viability theorem for differential inclusions with dynamics measurable with respect to time and the state constraints, given by an absolutely

There are two major approaches in the finite-step methods of structured linear programming: decomposition methods, which are based on the Dantzig-Wolfe decomposition

0.3 M HCl by diluting concentrated HCl (Merck supra pure) 0.8 M ammonia by diluting 25% NH4OH (Fluka supra pure) 0.1M H2O2 by diluting 30% H2O2 (Merck supra pure)..

Table 5.1: Comparison of the prices of the EUR/PLN call options (maturing in 1 month and traded on August 12, 2009) obtained using the Black-Scholes model (A.5), the exact