lecture7 - PIC 20A Lecture 7: Comparing Values, Boolean...

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

View Full Document Right Arrow Icon
PIC 20A Lecture 7: Comparing Values, Boolean Operators
Background image of page 1

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

View Full DocumentRight Arrow Icon
Comparing float and double values • do not test for equality since round-off errors are common • instead, test if two floating point numbers are “close enough” to each other • you specify how close they need to be by defining a very small constant called EPSILON
Background image of page 2
An Example // say x and y are of type double // close here means they differ at most by .01 final double EPSILON = 1E-2; if (Math.abs(x-y) <= EPSILON) {System.out.println(x + “ close to ” + y);}
Background image of page 3

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

View Full DocumentRight Arrow Icon
Comparing Objects • To see if two object references refer to the same object in memory, use == • The operator == returns true if the two references store the same memory address and false otherwise • Override the method boolean equals(Object o) to define what equality means for objects of your class
Background image of page 4
Rectangle r1 = new Rectangle(0,0,5,5); Rectangle r2 = new Rectangle(r1); Rectangle r3 = r1; r1 == r2 returns ?? r1 == r3
Background image of page 5

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

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

This note was uploaded on 04/12/2010 for the course PIC PIC 20A taught by Professor Keithouellette during the Spring '09 term at UCLA.

Page1 / 18

lecture7 - PIC 20A Lecture 7: Comparing Values, Boolean...

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

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