if (condition) statement; else if (condition) statement; . . . else statement;
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"; . . .
if (richter >= 8.0) r = "Most structures fall"; if (richter >= 7.0) // omitted else--ERROR r = "Many buildings destroyed"
/** 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; } }
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:
Enter a magnitude on the Richter scale: 7.1
Many buildings destroyed
The if/else/else statement for the earthquake strength first tested for higher values, then descended to lower values. Can you reverse that order?
if (condition) { if (condition) statement; else statement; } else statement;
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% |
/** 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; } }
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:
Please enter your income: 50000 Are you married? (Y/N) N Tax: 11211.5
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?
public boolean isOverdrawn() { return balance < 0; // Returns true or false }
if (harrysChecking.isOverdrawn())
isDigit isLetter isUpperCase isLowerCase
if (Character.isUpperCase(ch)) . . .
if (in.hasNextInt()) n = in.nextInt();
if (0 < amount && amount < 1000) . . .
if (input.equals("S") || input.equals("M")) . . .
if (!input.equals("S")) . . .
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 |
married = input.equals("M");
if (married) . . . else . . . if (!married) . . .
if (married == true) . . . // Don't
Just use the simpler test
if (married) . . .
When does the statement
System.out.println (x > 0 || x < 0);
print false?
false
false
false
when x
is 0false
when x
is not 0What is the opposite of
x < 0 || x > 10
x < 0 && x > 10
x > 0 || x > 10
x >= 0 || x >= 10
x >= 0 && x <= 10
How many test cases do you need to cover all branches of the getDescription method of the Earthquake class?