1. if
| ok | Not ok |
| if (x
> 0) System.out.println("positive"); |
if x > 0 System.out.println("positive"); |
|
if (x > 0) System.out.println("positive"); |
|
|
if (x > 0); System.out.println("positive"); |
|
| if (x > 0) System.out.println("positive"); | if (x > 0) System.out.println("positive") |
| if (x
> 0) { System.out.println("positive"); } |
if (x > 0) ( System.out.println("positive"); ) |
| if (x
> 0) { System.out.println(x); System.out.println(" is positive"); } |
if (x > 0) System.out.println(x); System.out.println(" is positive"); |
2. else if
| ok | Not ok |
| if (x
> 0) System.out.println("positive"); else if (x < 0) System.out.println("negative"); |
if (x > 0) System.out.println("positive"); if (x < 0) System.out.println("negative"); |
| System.out.println("User pressed " + ch); |
if (ch == 'A') System.out.println("User pressed A"); else if (ch == 'B') System.out.println("User pressed B"); else if (ch == 'C') System.out.println("User pressed C"); else if (ch == 'D') System.out.println("User pressed D"); else if (ch == 'E') System.out.println("User pressed E"); |
3. else
| ok | Not ok |
| if (x
> 0) System.out.println("positive"); else System.out.println("not positive"); |
if (x > 0) System.out.println("positive"); else System.out.println("negative"); |
| if (x
> 0) System.out.println("positive"); else if (x < 0) System.out.println("negative"); else System.out.println("zero"); |
if (x > 0) System.out.println("positive"); else if (x < 0) System.out.println("negative"); else if (x == 0) System.out.println("zero"); |
|
System.out.print(x); if (x > 0) System.out.println(" is positive"); else System.out.println(" is not positive"); // or System.out.print(x + " is "); if (x <= 0) System.out.print(" not "); System.out.println("positive"); |
if (x > 0) { System.out.print(x); System.out.println(" is positive"); } else { System.out.print(x); System.out.println(" is not positive"); } |
4. && (and)
| ok | Not ok |
| if ((x
> 0) && (y > 0)) System.out.println("both positive"); |
if (x > 0) { if (y > 0) System.out.println("both positive"); } |
|
if (x > 0 && y > 0) System.out.println("both positive"); |
|
| if ((x
!= 0) && (y/x > z)) System.out.println("yes!"); |
if ((y/x > z) && (x !=
0)) System.out.println("yes!"); |
| if ((x
> 0) && (x < 10) && (y > 0) && (y < 10)) System.out.println("in bounds"); |
if ((x > 0) && (x <
10) && (y > 0) && (y < 10)) System.out.println("in bounds"); |
| if ((i
>= 0) && (i < s.length()) && (s.charAt(i) == 'A')) System.out.println("yes!"); |
if ((s.charAt(i) ==
'A') && (i >= 0) && (i < s.length()) System.out.println("yes!"); // or if ((i > 0) && (i < s.length()) && (s.charAt(i) == 'A')) System.out.println("yes!"); // or if ((i >= 0) && (i <= s.length()) && (s.charAt(i) == 'A')) System.out.println("yes!"); |
5. || (or)
| ok | Not ok |
| if ((x
> 0) || (y > 0)) System.out.println("one or both positive"); |
if (x > 0) System.out.println("one or both positive"); if (y > 0) System.out.println("one or both positive"); |
| if ((x
> 0) && ((y > Math.sqrt(x)) || (z > Math.sqrt(x)))) System.out.println("yes!"); // or if ((x > 0) && ((y > Math.sqrt(x)) || (z > Math.sqrt(x)))) System.out.println("yes!"); |
if ((x > 0) && ((y >
Math.sqrt(x)) || (z > Math.sqrt(x)))) System.out.println("yes!"); |
6. ! (not)
| ok | Not ok |
| if (x
<= 0) System.out.println("not positive"); |
if (!(x > 0)) System.out.println("not positive"); |
| if
(s.contains("abc")) System.out.println("red"); else System.out.println("blue"); |
if (!s.contains("abc")) System.out.println("blue"); else System.out.println("red"); |
|
if (b) System.out.println("true!"); // or if (b == true) System.out.println("true!"); |
if (!!b) System.out.println("true!"); // or if (b = true) System.out.println("true!"); // or if (b != false) System.out.println("true!"); |
| if
(!b) System.out.println("false!"); // or if (b == false) System.out.println("false!"); |
if (b = false) System.out.println("false!"); // or if (!(b == true)) System.out.println("false!"); // or if (b != true) System.out.println("false!"); |
7. nested if's
| ok | Not ok |
| if (x
> 0) { if (y > 0) System.out.println("both positive"); } else System.out.println("x is not positive"); |
if (x > 0) if (y > 0) System.out.println("both positive"); else System.out.println("x is not positive"); |
8. Some Practice
Exercise #1: Write Java programs that work as
described:
Write a program that reads in two integers and prints
out the smaller value without using Math.min or Math.max (use an "if"
statement).
Write a program that reads in two integers and prints
out true if they are of the same sign (both positive or both negative)
and false otherwise. Write this program as many different ways as
you can (using nested and un-nested if statements, &&'s, ||'s, !'s,
etc). Which is/are best? Why?
Write a program that uses only two variables and
reads in three integers and prints out the smallest value entered.
Write a program that reads in a string and a double.
The string indicates which function to apply to the double. So if
the string is "abs", print out the absolute value of the double.
if the string is "inv", print out the inverse of the double (that is,
one divided by the double), except if the double is zero, then print out
a divide-by-zero error message. If the string is "sqrt", print out
the square root of the double, unless the double is negative, then print
out a suitable error message. And if the string is "pow", read in
a second double and print out the first double raised to power of
the second double. If the string is not one of "abs", "inv", "sqrt",
or "pow", then print out an error message indicating that the user
entered an unknown function.
Rewrite your "leap year" solution using "if"
statements for clarity. That is: write a program that reads
in an integer and prints true if that integer represents a leap year and
false otherwise. A year is a leap year if it is divisible by 4, except
that years divisible by 100 are not leap years unless they are also
divisible by 400. So, for example, 1900 was not a leap year because it
is divisible by 100 but 2000 was a leap year because, whereas it is
divisible by 100, it is also divisible by 400.
Exercise #2: Write Java programs (each starting from HelloGraphics.java) that work like this:
Note that the program displays products for odd values and sums for even
values.
Note that the colors change depending on direction and that the
horizontal box is moving faster than the vertical ball.
Note that the blue box is the largest square you can draw with a 10-pixel
margin around it (so you will take the minimum of the width and height when
computing the size of the square), and that the circle pauses on the corners and is red while
it pauses.
BONUS [optional]:
Note that you can change the font with this command:
page.setFont(new
Font("Arial",Font.BOLD,36));
This particular example makes a 36-point, bold, "Arial" font.
Since font names vary among systems, stick with "Arial" for now.
BONUS [optional]:
Make another game of your choosing. Try to make it as attractive
and as fun (both to write and to play!) as possible. Be creative!