²Ä ¤ ³¹
¨M©w
5.1 if ±Ôz
if (amount <= balance)
balance = balance - amount;
if/else ±Ôz
if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;
Block ±Ôz
if (amount <= balance)
{
double newBalance = balance - amount;
balance = newBalance;
}
Syntax 5.1. if ±Ôz
|
if(condition)
statement
if (condition)
statement
else
statement
|
¨ÒÃD:
|
if (amount <= balance) balance = balance - amount;
if (amount <= balance) balance = balance - amount; else balance = balance - OVERDRAFT_PENALTY;
|
¥Î³~:
To execute a statement when a condition is true or false |
Syntax 5.2. Block ±Ôz
|
{
statement
statement
. . .
}
|
¨ÒÃD:
|
{
double newBalance = balance - amount;
balance = newBalance;
}
|
¥Î³~:
To group several statements together to form a single statement |
±Ôz«¬¦¡
- ²³æ±Ôz
balance = balance - amount;
- ½ÆÂø±Ôz
if (balance >= amount) balance = balance - amount;
Also while, for, etc. (see chapter 6)
- Block ±Ôz
{
double newBalance = balance - amount;
balance = newBalance;
}
5.2 ¼ÆÈªº¤ñ¸û
Ãö«Y¹Bºâ¤l
Java
|
Math Notation
|
Description
|
>
|
>
|
Greater than
|
>=
|
≥
|
Greater than or equal
|
<
|
<
|
Less than
|
<=
|
≤
|
Less than or equal
|
==
|
=
|
Equal
|
!=
|
≠
|
Not equal
|
¬Ûµ¥©M«ü©w
- == ´ú¸Õ¬O§_¬Ûµ¥:
if (x == 0) . . // if x equals zero
- = «ü©w¤@¼ÆÈµ¹¤@ÅܼÆ:
x = 0; // assign 0 to x
- ª`·N¤£n¥Î¿ù
¯BÂI¼Æ¤ñ¸û
¯BÂI¼Æ¤ñ¸û
- ¨â¼Æ´X¥G¬Ûµ¥, °²¦p
|x - y| <= ε
- ε ¬O¤@«Ü¤pªº¼Æ, ¦p 10-14
- °²¦p x, y «Ü¤j©Î«Ü¤p, ¤£¨¬¥H§PÂ_, ´N¥²¶·¥Î
|x - y| / max(|x|, |y|) <= ε
- ¦ý¬OBut if one of the numbers is zero, don't divide by max(|x
|, |y|)
¦r¦ê¤ñ¸û
- ¤£n¥Î == ¤ñ¸û¦r¦ê!
if (input == "Y") // WRONG!!!
- ¥Î equals ¤èªk:
if (input.equals("Y"))
- == ´ú¸Õ¨¥÷, equals ´ú¸Õ¤º®e¬O§_¬Û¦P
- ¤j¤p¼g¤£©ë®É (¦p "Y" or "y") ¥Î equalsIgnoreCase()
if (input.equalsIgnoreCase("Y"))
¨Ì¦r¨åªº¦¸§Ç¤ñ¸û(Lexicographic Comparison)
Java ©Ò¥Îªº¦¸§Ç¡A¼Æ¦r¦b«e¡A±µµÛ¤j¼g¦r¥À¡A¦A±µµÛ¤p¼g¦r¥À¡C
- s.compareTo(t) < 0 ªí¥Ü:
¦b¦r¨å¤¤ s ¬O¦b t ¤§«e
- "car" ¦b "cargo" ¤§«e
"cargo" ¦b "cathode" ¤§«e
- ©Ò¦³¤j¼g¦r¥À³£¦b¤p¼g¦r¥À¤§«e, ¦p:
"Hello" ¦b "car" ¤§«e
¨Ì¦r¨åªº¦¸§Ç¤ñ¸û
ª«¥ó¤ñ¸û
- == ´ú¸Õ¨¥÷, equals ´ú¸Õ¤º®e¬O§_¬Û¦P
- Rectangle cerealBox = new
Rectangle(5, 10, 20, 30);
Rectangle oatmealBox = new
Rectangle(5, 10, 20, 30);
Rectangle r = cerealBox;
- cerealBox != oatmealBox,
but
cerealBox.equals(oatmealBox)
- cerealBox == r
- Caveat: equals must be defined for the class (see chapter
11)
ª«¥ó¤ñ¸û
The null Reference
- null reference refers to no object at all
- ¥i¥Î©ó´ú¸Õ:
if (account == null) . . .
- ¥Î ==, ¤£¬O¥Î equals, ´ú¸Õ null
- °²¦p¥ÎªÌ«ö¤U¨ú®øÁä showInputDialog ¶Ç¦^ null:
String input = JOptionPane.showInputDialog("...");
if (input != null) { ... }
- null ¤£¬OªÅ¦r¦ê ""
5.3 ¦h«¿ï¾Ü(Multiple Alternatives)
- if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
- The first matching condition is executed.
- Order matters.
File Earthquake.java
/**
A class that describes the effects of an earthquake.
*/
public class Earthquake
{
/**
Constructs an Earthquake object.
@param magnitude the magnitude on the Richter scale
*/
public Earthquake(double magnitude)
{
richter = magnitude;
}
/**
Gets a description of the effect of the earthquake.
@return the description of the effect
*/
public String getDescription()
{
String r;
if (richter >= 8.0)
r = "Most structures fall";
else if (richter >= 7.0)
r = "Many buildings destroyed";
else if (richter >= 6.0)
r = "Many buildings considerably damaged, some collapse";
else if (richter >= 4.5)
r = "Damage to poorly constructed buildings";
else if (richter >= 3.5)
r = "Felt by many people, no destruction";
else if (richter >= 0)
r = "Generally not felt by people";
else
r = "Negative numbers are not valid";
return r;
}
private double richter;
}
File EarthquakeTest.java
import javax.swing.JOptionPane;
/**
A class to test the Earthquake class.
*/
public class EarthquakeTest
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog(
"Enter a magnitude on the Richter scale:");
double magnitude = Double.parseDouble(input);
Earthquake quake = new Earthquake(magnitude);
System.out.println(quake.getDescription());
System.exit(0);
}
}
¦h«¿ï¾Ü
- ´ú¸Õªº¥ý«á¦¸§Ç·|¼vúgµ²ªGOrder matters:
if (richter >= 0) // always passes
r = "Generally not felt by people";
else if (richter >= 3.5) // not tested
r = "Felt by many people, no destruction";
. . .
- ¤£nº|¤F else
if (richter >= 8.0)
r = "Most structures fall";
if (richter >= 7.0) // omitted else--ERROR
r = "Many buildings destroyed
¦h¼h¤À¤ä(Nested Branches)
©Ò±oµ|¥Ó³ø
°²¦p±B«Ãª¬ªp¬O³æ¨
©Ò±oÁ`ÃB¶W¹L
|
¦ý¥¼¶W¹L
|
µ|ÃB¬°
|
Á`ÃB¶W¹L
|
$0
|
$21,450
|
15%
|
$0
|
$21,450
|
$51,900
|
$3,217.50 + 28%
|
$21,450
|
$51,900
|
|
$11,743.50 + 31%
|
$51,900
|
°²¦p±B«Ãª¬ªp¬O¤w±B
©Ò±oÁ`ÃB¶W¹L
|
¦ý¥¼¶W¹L
|
µ|ÃB¬°
|
Á`ÃB¶W¹L
|
$0
|
$35,800
|
15%
|
$0
|
$35,800
|
$86,500
|
$5,370.00 + 28%
|
$35,800
|
$86,500
|
|
$19,566.00 + 31%
|
$86,500
|
©Ò±oµ|¥Ó³ø¬yµ{¹Ï
File TaxReturn.java
/**
A tax return of a taxpayer in 1992.
*/
class TaxReturn
{
/**
Constructs a TaxReturn object for a given income and
marital status.
@param anIncome the taxpayer income
@param aStatus either SINGLE or MARRIED
*/
public TaxReturn(double anIncome, int aStatus)
{
income = anIncome;
status = aStatus;
}
public double getTax()
{
double tax = 0;
if (status == SINGLE)
{
if (income <= SINGLE_CUTOFF1)
tax = RATE1 * income;
else if (income <= SINGLE_CUTOFF2)
tax = SINGLE_BASE2
+ RATE2 * (income - SINGLE_CUTOFF1);
else
tax = SINGLE_BASE3
+ RATE3 * (income - SINGLE_CUTOFF2);
}
else
{
if (income <= MARRIED_CUTOFF1)
tax = RATE1 * income;
else if (income <= MARRIED_CUTOFF2)
tax = MARRIED_BASE2
+ RATE2 * (income - MARRIED_CUTOFF1);
else
tax = MARRIED_BASE3
+ RATE3 * (income - MARRIED_CUTOFF2);
}
return tax;
}
public static final int SINGLE = 1;
public static final int MARRIED = 2;
private static final double RATE1 = 0.15;
private static final double RATE2 = 0.28;
private static final double RATE3 = 0.31;
private static final double SINGLE_CUTOFF1 = 21450;
private static final double SINGLE_CUTOFF2 = 51900;
private static final double SINGLE_BASE2 = 3217.50;
private static final double SINGLE_BASE3 = 11743.50;
private static final double MARRIED_CUTOFF1 = 35800;
private static final double MARRIED_CUTOFF2 = 86500;
private static final double MARRIED_BASE2 = 5370;
private static final double MARRIED_BASE3 = 19566;
private double income;
private int status;
}
File TaxReturnTest.java
/**
A tax return of a taxpayer in 1992.
*/
class TaxReturn
{
/**
Constructs a TaxReturn object for a given income and
marital status.
@param anIncome the taxpayer income
@param aStatus either SINGLE or MARRIED
*/
public TaxReturn(double anIncome, int aStatus)
{
income = anIncome;
status = aStatus;
}
public double getTax()
{
double tax = 0;
if (status == SINGLE)
{
if (income <= SINGLE_CUTOFF1)
tax = RATE1 * income;
else if (income <= SINGLE_CUTOFF2)
tax = SINGLE_BASE2
+ RATE2 * (income - SINGLE_CUTOFF1);
else
tax = SINGLE_BASE3
+ RATE3 * (income - SINGLE_CUTOFF2);
}
else
{
if (income <= MARRIED_CUTOFF1)
tax = RATE1 * income;
else if (income <= MARRIED_CUTOFF2)
tax = MARRIED_BASE2
+ RATE2 * (income - MARRIED_CUTOFF1);
else
tax = MARRIED_BASE3
+ RATE3 * (income - MARRIED_CUTOFF2);
}
return tax;
}
public static final int SINGLE = 1;
public static final int MARRIED = 2;
private static final double RATE1 = 0.15;
private static final double RATE2 = 0.28;
private static final double RATE3 = 0.31;
private static final double SINGLE_CUTOFF1 = 21450;
private static final double SINGLE_CUTOFF2 = 51900;
private static final double SINGLE_BASE2 = 3217.50;
private static final double SINGLE_BASE3 = 11743.50;
private static final double MARRIED_CUTOFF1 = 35800;
private static final double MARRIED_CUTOFF2 = 86500;
private static final double MARRIED_BASE2 = 5370;
private static final double MARRIED_BASE3 = 19566;
private double income;
private int status;
}
¨Ï¥Î¥¬ªL(boolean)¦¡
boolean «¬¦¡
- George Boole (1815-1864): ÅÞ¿è¬ã¨sªº¥ýÅXªÌ
- ¦¡¤lÈ amount < 1000 ¬O true ©Î
false.
- boolean «¬¦¡: true ©Î
false
Â_©w©Ê¤èªk(Predicate Method)
- ¦^Ȫº«¬¦¡¬° boolean
- ¨Ò¤l:
public boolean isOverdrawn()
{
return balance < 0;
}
- ¥Î©ó´ú¸Õ±ø¥ó¤¤
if (harrysChecking.isOverdrawn()) ...
- Character Ãþ§O¤¤, ¦³¥ÎªºÂ_©w©Ê¤èªk¦³:
isDigit
isLetter
isUpperCase
isLowerCase
- if (Character.isUpperCase(ch)) ...
¥¬ªL¹Bºâ¤l
- && and
- || or
- ! not
- if (0 < amount && amount < 1000) ...
- if (input.equals("S") || input.equals("M")) ...
&& ©M || ¹Bºâ¤l
¯u¹êªí(Truth Tables)
A
|
B
|
A && B
|
true
|
true
|
true
|
true
|
false
|
false
|
false
|
Any
|
false
|
A
|
B
|
A || B
|
true
|
Any
|
true
|
false
|
true
|
true
|
false
|
false
|
false
|
A
|
! A
|
true
|
false
|
false
|
true
|
De Morgan's ©w«ß
- §_©w¤@½ÆÂø±ø¥ó®e©ö¤Þ°_²V²c:
if (!(0 < amount && amount < 1000))
- De Morgan's ©w«ß»¡©ú
!(A && B) ©M !A || !B ¬Û¦P
!(A || B) ©M !A && !B ¬Û¦P
- ª`·N ! ²¾¤J¬A©·¤º && ©M || ¹ï½Õ
- (!(0 < amount && amount < 1000)) §Y
!(0 < amount) || !(amount < 1000) , §Y
0 >= amount || amount >= 1000
- ª`·N < ªº¬Û¤Ï¬O >=
¥¬ªLÅܼÆ
- private boolean married;
- ³]©wªº¬O¯u¹êÈ:
married = input.equals("M");
- ¦b±ø¥ó¤¤¨Ï¥Î:
if (married) ... else ...
if (!married) ...
- ¤£n´ú¸Õ¥¬ªLÅܼƬO§_µ¥(©Î¤£µ¥)©ó¯u¹êȤ§¤@, ¦p:
if (married == true) // DON'T
if (married == false) // DON'T
if (married != false) // NO!!!