Lecture4

Lecture4 - Comparing Variable Comparing floating point...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Comparing Variable Comparing floating point values for equality Comparing characters Comparing strings Comparing objects Comparing Float Values Two floating point values are equal only if the match exactly • Computations often result in slight differences that may be irrelevant • To determine the equality of two floats, you may want to use the following technique: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal"); The tolerance could be set to any appropriate level, such as 0.000001
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Comparing Characters Java character is based on the Unicode character set Java uses the 16-bit Unicode values for all characters 38885 characters currently are defined with Unicode values. The low 8 bits are compatible with ASCII values. Unicode establishes a particular numeric value for each character, and therefore an ordering. if ('+' < 'J') System.out.println ("+ is less than J"); 97 through 122 a – z 65 through 90 A – Z 48 through 57 0 – 9 Unicode Values Characters
Background image of page 2
3 Comparing Strings with the equals() Method • The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The equals method returns a boolean result. String name1 = “Lewis”; String name2 = “Lewis”; if (name1.equals(name2)) System.out.println ("Same name");
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Comparing Objects ( object reference) with == and != Operators •The == operator can be applied to objects – it returns true if the two references are aliases of each other String name1 = “Lewis”; String name2 = “Lewis”; name2=name1; if (name1==name2) name1 “Lewis” name2 Name1 is equal to name2 String name1 = “Lewis” String name2 = “Lewis” if (name1==name2) Name1 “Lewis” Name2 “Lewis” Name1 is not equal to name2
Background image of page 4
5 Comparing Values of Object with the equals() Method class Car { String licensePlate; double speed; double maxSpeed; Car(String licensePlate, double speed, double maxSpeed) { this.licensePlate = licensePlate; this.speed = speed; this.maxSpeed = maxSpeed; } boolean equal(Car car1) { if (this.speed==car1.speed&& this.maxSpeed== car1.maxSpeed){ return true;} else{ return false;} } } class CarTest{ public static void main(String[] args){ Car c = new Car("Toronto66", 100, 200); Car d = new Car("Toronto66", 100, 200); if (c.equals(d)){ System.out.println("value is equal"); } else { System.out.println("value is not equal"); } if (c == d) { System.out.println(" Object is equal");} else{ System.out.println("Object is not equal"); } }}
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Using == instead of equals() with Objects When compare objects, you need to know whether you should use == to see if they are the same object , or equals() to see if they may be a different object, but have the same value .
Background image of page 6
7 Chapter 4 How Objects Behave: Methods use instance variables Method declaration and parameter passing Data hiding and encapsulation Difference between instance and local variables In Java, all methods are defined within a class A method takes input, performs actions, and produces output Method Declarations
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 28

Lecture4 - Comparing Variable Comparing floating point...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online