²Ä ¤­ ³¹

¨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«¬¦¡

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


¯BÂI¼Æ¤ñ¸û

¯BÂI¼Æ¤ñ¸û

¦r¦ê¤ñ¸û

¨Ì¦r¨åªº¦¸§Ç¤ñ¸û(Lexicographic Comparison)

Java ©Ò¥Îªº¦¸§Ç¡A¼Æ¦r¦b«e¡A±µµÛ¤j¼g¦r¥À¡A¦A±µµÛ¤p¼g¦r¥À¡C

¨Ì¦r¨åªº¦¸§Ç¤ñ¸û


ª«¥ó¤ñ¸û

ª«¥ó¤ñ¸û


The null Reference

5.3 ¦h­«¿ï¾Ü(Multiple Alternatives)

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­«¿ï¾Ü

¦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 «¬¦¡

Â_©w©Ê¤èªk(Predicate Method)

¥¬ªL¹Bºâ¤l

&& ©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«ß

¥¬ªLÅܼÆ