=
) is an operator
a += b
is same as a = a + b
+=
, -=
, *=
, /=
x++
is the same as x += 1
which is the same as x = x + 1
if
statement) and for
loop
==
— Equality
!=
— Inequality
<
>
<=
>=
equals
method (of the String class), rather than
==
. (There are other methods for greater, less, etc — we'll cover them later):
String s1, s2; … s1 = … … s2 = … … if (s1.equals(s2)) // NOT s1 == s2 … …
A medical office has hired us to write a program to screen out patients who should not be receiving the swine flu vaccine. The Department of Health has determined that children under the age of 3 and seniors over the age of 65 should NOT be receiving the vaccine. The program should prompt the user for the patients age and print out whether the vaccine may be administered.
import java.util.*;
public class Vaccines {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter patient's age: ");
int age = scanner.nextInt();;
if (age < 3 || age > 65)
System.out.println( "***** Do NOT ADMINISTER VACCINE *******");
else
System.out.println( "Go ahead and stick it to 'em");
}
}
!
'Not' operator: true if operand is false; false if operand is true
&&
'And' operator: true only if both operands are true
||
'Or' operator: true if either or both operands are true
^
'Xor' operator: true if either but not both operands are true
&&
has higher precedence than ||
.
If a
and b
are conditions, then:
!(a && b)
is the same as !a || !b
!(x == 6 && y == 8)
is the same as !(x == 6) || !(y == 8)
!=
: x != 6 || y != 8
!(a || b)
is the same as !a && !b
!(x == 6 || y == 8)
is the same as !(x == 6) &&| !(y == 8)
!=
: x != 6 && y != 8
if !(age <= 3 || age >= 65)transforms to (by DeMorgan's Law):
if (!(age <=3) && !(age >= 65))which then transforms to (through an understanding of the relational operators):
if (age > 3 && age < 65)
a && b
is always false when the condition a
is false.
a
is false, there is no reason to check whether b
is true or not.
a || b
is always true when a
is true.
a
is true, there is no reason to check whether b
is true or not.
payroll.data
consists of a header value for the number of employees followed by the the following data
for each employee:
The program should scan the file, and print out the info for any employee whose payroll record seems incorrect.
import java.io.*; import java.util.*; public class Payroll { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("payroll.data")); int numEmployees = scanner.nextInt(); for (int i = 1; i <= numEmployees; i++) { int id = scanner.nextInt(); String lastName = scanner.next(); String firstName = scanner.next();; String status = scanner.next();; int amount = scanner.nextInt();; if (status.equals("f") && amount < 10000 || status.equals("p") && amount > 50) System.out.println("Possibly corrupt record for employee " + lastName + ", " + firstName + " (" + id + ")"); } } }
isDataCorrupt
to determine whether the data is possibly corrupt.
import java.io.*; import java.util.*; public class Payroll { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("payroll.data")); int numEmployees = scanner.nextInt(); for (int i = 1; i <= numEmployees; i++) { int id = scanner.nextInt(); String lastName = scanner.next(); String firstName = scanner.next();; String status = scanner.next();; int amount = scanner.nextInt();; if (isDataCorrupt(status, amount) ) System.out.println("Possibly corrupt record for employee " + lastName + ", " + firstName + " (" + id + ")"); } } public static boolean isDataCorrupt(String status, int amount) { if (status.equals("f") && amount < 10000 || status.equals("p") && amount > 50) return true; return false; } }
boolean
int i, j, k; double d, e, f; string s, t, u;Consider the types of the following expressions:
Expression | Type |
---|---|
3 | int
|
4.6 | double
|
"Hello" | string
|
i+j | int
|
i/j | int
|
d/e | double
|
i/d | double
|
s | string
|
But what about
Expression | Type |
---|---|
i < j | ??? |
i == 6 | ??? |
d > e && f < 4.8 | ??? |
i < 3 || i > 65 | ??? |
boolean
boolean b;
true
and false
b = false; .... b = true;
if
statements and boolean
values can also be used as regular variables
boolean isEligibleToRetire; int age; ... if (age >= 62) isEligibleToRetire = true; else isEligibleToRetire = false; ... if (isEligibleToRetire) System.out.println("May retire");
More elegantly ("speaking Boolean")::
boolean isEligibleToRetire; int age; ... isEligibleToRetire = age >= 62; ... if (isEligibleToRetire) System.out.println("May retire");
boolean
variable, isCorrupted
, to true/false
and then uses that variable as the return value.
import java.io.*; import java.util.*; public class Payroll { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("payroll.data")); int numEmployees = scanner.nextInt(); for (int i = 1; i <= numEmployees; i++) { int id = scanner.nextInt(); String lastName = scanner.next(); String firstName = scanner.next();; String status = scanner.next();; int amount = scanner.nextInt();; if (isDataCorrupt(status, amount) ) System.out.println("Possibly corrupt record for employee " + lastName + ", " + firstName + " (" + id + ")"); } } public static boolean isDataCorrupt(String status, int amount) { boolean isCorrupted; if (status.equals("f") && amount < 10000 || status.equals("p") && amount > 50) isCorrupted = true; else isCorrupted = false; return isCorrupted; } }
import java.io.*; import java.util.*; public class Payroll { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("payroll.data")); int numEmployees = scanner.nextInt(); for (int i = 1; i <= numEmployees; i++) { int id = scanner.nextInt(); String lastName = scanner.next(); String firstName = scanner.next();; String status = scanner.next();; int amount = scanner.nextInt();; if (isDataCorrupt(status, amount) ) System.out.println("Possibly corrupt record for employee " + lastName + ", " + firstName + " (" + id + ")"); } } public static boolean isDataCorrupt(String status, int amount) { return status.equals("f") && amount < 10000 || status.equals("p") && amount > 50; } }
if/else
is:
if (<condition>) x = <value1> else x = <value2>i.e., assigning different values to the same variable (
x
in this case) based on some condition.
<condition> ? <value1> : <value2>
?
and :
, and three operands, the condition and two values
?
); otherwise the result of the operation
is the second value (the one after the :
)
x
as a conditional operator
x >= 0 ? x : -x
average >= 90 ? 'A' : average >= 80 ? 'B' : average >= 70 ? 'C' : average >= 60 ? 'D' : 'F'
average >= 90 ? 'A' : (average >= 80 ? 'B' : (average >= 70 ? 'C' : (average >= 60 ? 'D' : 'F')))