Lesson 9 – Decisions

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept - if

Concept - else

Activity - if

Record your participation in Piazza clicker question Lesson9 Q1

In California a person can get a drivers license at 16 years of age. Which of the following is the best way to code that test?

a.
if (age >= 16)
    System.out.println("Old enough to drive");
else if (age < 16)
    System.out.println("You will have to wait");
b.
if (age >= 16)
    System.out.println("Old enough to drive");
else 
    System.out.println("You will have to wait");
c.
if (age >= 16)
    System.out.println("Old enough to drive");
if (age < 16)
    System.out.println("You will have to wait");

Activity - if

Record your participation in Piazza clicker question Lesson9 Q2

We want to modify the BankAccount deposit method so that the amount is added only if it is positive. Which is the best code to do that?

a.
public void deposit(double amount)
{
    if (amount > 0)
    {
       balance = balance + amount;
    }
    else 
    {
    }
}
b.
public void deposit(double amount)
{
    if (amount > 0)
    {
       balance = balance + amount;
    }
}
c.
public void deposit(double amount)
{
    if (amount < 0)
    {
    }
    else 
    {
         balance = balance + amount;
    }
}

Activity - equality

Record your participation in Piazza clicker question Lesson9 Q3

Which if statement will determine if age is 21?

  1. if (age = 21)
  2. if age = 21
  3. if (age == 21)
  4. if age == 21

Concept - String equality

Activity - String equality

Record your participation in Piazza clicker question Lesson9 Q4

Write a program that asks the user for his/her favorite color. If the color is blue, print "Mine, too" Otherwise print "That's pretty, too". Ignore case so that blue, Blue, and BLUE are considered the same.

Here is the skeleton IO project from last time

Concept - else if

Sometimes you want to do a whole series of things depending on some value. Use a structure line this

  if(...)
  {
     //...
  }
  else if (...)
  {
     //...
  }
  else if (...)
  {
     //...
  }
  else
  {
     //...
  }
  

 

Activity - Multiple comparisons

Record your participation in Piazza clicker question Lesson9 Q5

Java University does not use plus/minus grading. Any letter grade the user enters is translated to 0.0 through 4.0 or -1.0 if the grade is an error. The test is case insensitive

For example

In fact, anything starting with upper or lowercase B is translated to a 3.0.

Write a program that asks the user for a letter grade, then translates it to the appropriate grade point and prints it.

A or anything that starts with A or a -> 4.0
B or anything that starts with B or b -> 3.0
C or anything that starts with C or c -> 2.0
D or anything that starts with D or d -> 1.0
F or anything that starts with F or f -> 0.0
Anything else -> -1.0

Hint: You should get the first letter of the input using the substring method then convert the substring to uppercase and compare it to "A", "B", etc

Here is the skeleton IO project

Activity - Multiple comparisons

Record your participation in Piazza clicker question Lesson9 Q6

In the previous activity, Test with these values. What do you get?

  1. -1.0, 4.0, -1.0, -1.0
  2. 3.0, -1.0, -1.0, 2.0
  3. 3.0, 4.0, -1.0, 2.0
  4. 3.0, 4.0, 0.0, 2.0