Computer Science 15-100 (Sections T & U), Fall 2007
Class Notes, Day 6: Tue 18-Sep-2007
Logistics
Topic Outline:
Polygon p1 = new Polygon(); p1.addPoint(10,10); p1.addPoint(40,80); p1.addPoint(70,10); Polygon p2 = new Polygon(); p2.addPoint(10,110); p2.addPoint(40,180); p2.addPoint(70,110); page.setColor(Color.blue); p1.fill(page); page.setColor(Color.red); p2.fill(page);
Polygon p1 = new Polygon();
p1.addPoint(10,10);
p1.addPoint(40,80);
p1.addPoint(70,10);
Polygon p2 = new Polygon();
p2.addPoint(10,110);
p2.addPoint(40,180);
p2.addPoint(70,110);
Polygon p = p1;
p.addPoint(40,30);
page.setColor(Color.blue);
p1.fill(page);
page.setColor(Color.red);
p2.fill(page); Polygon p1 = new Polygon();
p1.addPoint(10,10);
p1.addPoint(40,80);
p1.addPoint(70,10);
Polygon p2 = new Polygon();
p2.addPoint(10,110);
p2.addPoint(40,180);
p2.addPoint(70,110);
Polygon p = p1;
p.addPoint(40,30);
p = p2;
p.addPoint(40,30);
page.setColor(Color.blue);
p1.fill(page);
page.setColor(Color.red);
p2.fill(page);a) For integer values
class MyCode {
public static void main(String[] args) {
int a = 2, b = 2, c;
c = a;
System.out.println(c == a); // true
System.out.println(c == b); // true
}
}
b) For floating point values
We have already seen the dangers in using "==" with floating point
values. They are approximate, and "==" will not work right with them.
So: do not use "==" with floating point values (that is, floats and
doubles).
c) For objects
class MyCode {
public static void main(String[] args) {
String a = new String("2"), b = new String("2"), c;
c = a;
System.out.println(c == a); // true
System.out.println(c == b); // false
// we probably meant to do this:
System.out.println(c.equals(b)); // true
}
}
d) For objects cached by the compiler
class MyCode {
public static void main(String[] args) {
String a = "2", b = "2", c;
c = a;
System.out.println(c == a); // true
System.out.println(c == b); // true!!!
}
}
e) The Moral of the Story
* Use "==" without worries for integers and booleans.
* Do not use "==" with other types (unless you are certain it is
appropriate, and then Be Careful!).
Polygon p1; p1 = new Polygon(); p1 = new Polygon(); // What happened to our first polygon?
See Figure 3.1 (p. 119)
See Figure 3.2 (p. 123)
See Figure 3.4 (p. 128)
Note: The Math class is
static. You do not create
instances. You call the methods by prefacing them with "Math.".
For example:
import java.util.Random;
class MyCode {
public static void main(String[] args) {
Random random = new Random();
int a = random.nextInt(30), b = random.nextInt(5);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Math.max(a,b) = " + Math.max(a,b));
System.out.println("Math.min(a,b) = " + Math.min(a,b));
System.out.println("Math.pow(a,b) = " + Math.pow(a,b));
System.out.println("Math.sqrt(a) = " + Math.sqrt(a));
System.out.println("Math.random() = " + Math.random());
System.out.println("Math.random() = " + Math.random());
System.out.println("Math.random() = " + Math.random());
}
}Carpe diem!