![]() |
![]() Second Summer 2002 |
if
statements.
if quarters > 0 then System.out.println(quarters + "quarters"); if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x; if (x = 1) y++; else if (x = 2) y = y + 2; if (x && y == 0) p = new Point2D.Double(x, y); if (1 <= x <= 10) { System.out.println("Enter y:"); y = console.readDouble(); } if (s != "nickels" || s != "pennies" || s != "dimes" || s != "quarters") System.out.print("Input error!"); if (input.equalsIgnoreCase("N") || "NO") return; int x = console.readDouble(); if (x != null) y = y + x; language = "English"; if (country.equals("USA")) if (state.equals("PR")) language = "Spanish"; else if (country.equals("China")) language = "Chinese";
if/else /else
statement and
nested if statements. Give an example for each.
if/else /else
statement where the order
of the tests does not matter. Give an example where the order of the tests matter.
"Tom", "Dick" "Tom", "Tomato" "church", "Churchill" "car manufacturer", "carburetor" "Harry" hairy" "C++", "Car" "Tom", "Tom" "Car", "Carl" "car", "bar"
p
, q
,
and r
.
p |
q |
r |
(p && q) || !r |
!(p && (q || !r)) |
---|---|---|---|---|
false | false | false | ? | ? |
false | false | true | ? | ? |
false | true | false | ? | ? |
? | ? | ? | ? | ? |
? | ? | ? | ? | ? |
? | ? | ? | ? | ? |
? | ? | ? | ? | ? |
? | ? | ? | ? | ? |
A && B
is the same as B && A
for any Boolean
conditions A
and B
?
ands = 0; if (x > 0) s++; if (y > 0) s++;
s = 0; if (x > 0) s++; else if (y > 0) s++;
!(x > 0 && y > 0) !(x != 0 || y != 0) !(country.equals("USA") && !state.equals("HI") && !state.equals("AK")) !(x % 4 != 0 || !(x % 100 == 0 && x % 400 == 0))
-else
problem, using the following statement. A student with a GPA of at least 1.5, but less
than 2, is on probation. With less than 1.5, the student is failing.
==
operator and the
equals
method when comparing strings.
andr == s
where bothr.equals(s)
r
and s
are of type Rectangle
.
r
is null
?
What happens when this code runs?
Rectangle r; ... if (r.equals(null)) r = new Rectangle(5, 10, 20, 30);
Line2D.Double
represent the same line when displayed
on the graphics screen. Do not use a.equals(b)
.
Hint: IfLine2D.Double a; Line2D.Double b; if (your condition goes here) g2.drawString("They look the same!", x, y);
p
and q
are
points, then Line2D.Double(p, q)
and
Line2D.Double(q, p)
look the same.
n
equals
10 and whether a floating-point number x
equals 10.
x
and y
such that Math.abs(x - y)
is larger than 1000, but x
and
y
are still identical except for a roundoff error.
Rewrite this code to eliminate the explicitPoint2D.Double p = ... boolean xInside = false; if (x1 <= p.getX() && p.getY() <= x2) xInside = true; boolean yInside = false; if (y1 <= p.getY() && p.getY() <= y2) yInside = true; if (xInside && yInside) g2.drawString("p is inside the rectangle.", x1, y1);
true
and false
values,
by setting xInside
and yInside
to the values of Boolean expressions.