CS 46A - Lecture 12

Cover page image

Pre-class reading

Multiple Alternatives: Sequences of Comparisons

ch05/quake/Earthquake.java

/**
   A class that describes the effects of an earthquake.
*/
public class Earthquake
{  
   private double richter;

   /**
      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;
   }
}

ch05/quake/EarthquakeRunner.java

import java.util.Scanner;

/**
   This program prints a description of an earthquake of a given magnitude.
*/
public class EarthquakeRunner
{  
   public static void main(String[] args)
   {  
      Scanner in = new Scanner(System.in);

      System.out.print("Enter a magnitude on the Richter scale: ");
      double magnitude = in.nextDouble();
      Earthquake quake = new Earthquake(magnitude);
      System.out.println(quake.getDescription());
   }
}

Program Run:

Lecture 12 Clicker Question 1

The if/else/else statement for the earthquake strength first tested for higher values, then descended to lower values. Can you reverse that order?

  1. No; if you do, the answer is always "Most structures fall"
  2. No; if you do, the answer is always "Negative numbers are not valid"
  3. Yes, you can reverse the order
  4. Yes, if you also reverse the comparisons (changing >= to <)

Multiple Alternatives: Nested Branches

Tax Schedule

If your filing status is Single If your filing status is Married
Tax Bracket Percentage Tax Bracket Percentage
$0 . . . $32,000 10% $0 . . . $64,000 10%
Amount over $32,000 25% Amount over $64,000 25%

Nested Branches

ch05/tax/TaxReturn.java

/**
   A tax return of a taxpayer in 2008.
*/
public class TaxReturn
{  
   public static final int SINGLE = 1;
   public static final int MARRIED = 2;

   private static final double RATE1 = 0.10;
   private static final double RATE2 = 0.25;
   private static final double RATE1_SINGLE_LIMIT = 32000;
   private static final double RATE1_MARRIED_LIMIT = 64000;

   private double income;
   private int status;

   /**
      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 tax1 = 0;
      double tax2 = 0;

      if (status == SINGLE)
      {  
         if (income <= RATE1_SINGLE_LIMIT)
         {
            tax1 = RATE1 * income;
         }
         else
         {
            tax1 = RATE1 * RATE1_SINGLE_LIMIT;
            tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT);
         }
      }
      else
      {  
         if (income <= RATE1_MARRIED_LIMIT)
         {
            tax1 = RATE1 * income;
         }
         else 
         {
            tax1 = RATE1 * RATE1_MARRIED_LIMIT;
            tax2 = RATE2 * (income - RATE1_MARRIED_LIMIT);
         }
      }

      return tax1 + tax2;
   }
}

ch05/tax/TaxCalculator.java

import java.util.Scanner;

/**
   This program calculates a simple tax return.
*/
public class TaxCalculator
{  
   public static void main(String[] args)
   {  
      Scanner in = new Scanner(System.in);

      System.out.print("Please enter your income: ");
      double income = in.nextDouble();

      System.out.print("Are you married? (Y/N) ");
      String input = in.next();
      int status;
      if (input.equalsIgnoreCase("Y")) 
         status = TaxReturn.MARRIED;
      else  
         status = TaxReturn.SINGLE;

      TaxReturn aTaxReturn = new TaxReturn(income, status);

      System.out.println("Tax: "
            + aTaxReturn.getTax());
   }
}

Program Run:

Lecture 12 Clicker Question 2

Some people object to higher tax rates for higher incomes, claiming that you might end up with less money after taxes when you get a raise for working hard. Let's add a 90% tax rate for income over $400,000. (This was actually in force during the Eisenhower administration.)  

Suppose you make $399,900. Your taxes are $95,175. You are offered a $200 raise. By how much does it increase your taxes?

  1. By a little more than $360,000
  2. By a little more than $265,000
  3. By a little more than $200
  4. By a little more than $100

Using Boolean Expressions: The boolean Type

Using Boolean Expressions: Predicate Method

Using Boolean Expressions: The Boolean Operators

&& and || Operators

Boolean Operators

Boolean Operators

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

Using Boolean Variables

Lecture 12 Clicker Question 3

When does the statement

System.out.println (x > 0 || x < 0);

print false?

  1. It always prints false
  2. It never prints false
  3. It prints false when x is 0
  4. It prints false when x is not 0

Lecture 12 Clicker Question 4

What is the opposite of

x < 0 || x > 10
  1. x < 0 && x > 10
  2. x > 0 || x > 10
  3. x >= 0 || x >= 10
  4. x >= 0 && x <= 10

Code Coverage

Lecture 12 Clicker Question 5

How many test cases do you need to cover all branches of the getDescription method of the Earthquake class?

  1. One
  2. Six
  3. Seven
  4. Ten