CS 303E Class 11: Boolean Logic, Boolean Expressions, and more on if statements' - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS 303E Class 11: Boolean Logic, Boolean Expressions, and more on if statements'

Description:

Monty Python and the Holy Grail. CS303E. Boolean Logic, Boolean Expressions, and more on if statements. 2. The Tax Rate Problem ... – PowerPoint PPT presentation

Number of Views:310
Avg rating:3.0/5.0
Slides: 12
Provided by: MikeS2
Category:

less

Transcript and Presenter's Notes

Title: CS 303E Class 11: Boolean Logic, Boolean Expressions, and more on if statements'


1
CS 303E Class 11Boolean Logic, Boolean
Expressions, and more on if statements.
So logically If she ways the same as a duck
she's made of wood ..and therefore A
WITCH!!!!! - Monty Python and the Holy Grail
2
The Tax Rate Problem
  • Last time we calculated tax rates according to
    marital status and income.
  • A series of nested if - else statements was
    needed to assign the correct tax rate
  • if(status SINGLE)
  • if(income lt 21450)
  • taxRate 0.15
  • else if(income lt 51900)
  • taxRate 0.28
  • else
  • taxRate 0.31
  • else
  • if(income lt 35800) // and so forth

3
An Alternate Approach
  • Like, most programming languages, Java contains
    another way of determining an outcome based on
    more than one condition.
  • The conditions we saw last time are example of
    boolean expressions
  • A boolean expression is an expression that
    evaluates to true or false
  • if( x gt 10)
  • There is another primitive data type in Java,
    boolean
  • The equals method return type is boolean

4
Logical Operators
  • There are three boolean operators that allow us
    to create more complex conditions
  • Logical AND, both condition must be true
    for the whole expression to be true
  • if(status SINGLE income lt 21450)
  • Logical OR, if one or both of the conditions
    is true the whole expression is true
  • if(month APRIL month JUNE month
    SEPTEMBER month NOVEMBER)
  • daysInMonth 30
  • ! Logical NOT, gives the opposite of the
    condition

5
Boolean Logic
  • If A and B are conditions (A is x lt 10, B is y gt
    5 for example) then the following are the truth
    tables for our three logical operators

6
Complex Mathematical Checks
  • Now we can create a programming language version
    of the check x lt y lt z
  • if( x lt y y lt z)
  • Style issues
  • Put a space before and after the comparison
    operators and logical operators.
  • Parenthesis may be used to contain each simple
    condition.
  • if( (x lt y) (y lt z) )

7
Complex Boolean Expressions
  • You can piece together as many conditions as
    desired with whatever logical operators are
    desired to create very complex boolean
    expressions
  • Precedence of Logical operators
  • ( ) parenthesis
  • ! logical NOT
  • logical AND
  • logical OR
  • If an expressions value is known from a condition
    early the rest of the expression is not evaluated
  • if(month APRIL month JUNE month
    SEPTEMBER month NOVEMBER)
  • daysInMonth 30

8
Creating Complex Boolean Expressions
  • Complete the following methods in the Student
    class
  • public String cluelessnessLevel
  • A student is "completely clueless" if they are a
    freshmen with a major besides business or a
    graduate student
  • A student is "clueless" if they are are a
    freshmen majoring in business or a sophomore or a
    computer science major and not a senior
  • A student is "a little clueless" if they are a
    junior or an education major.
  • A student is "informed" if they are a senior, but
    not a computer science major and not in the
    business school
  • A student is "informed" if they are a senior in
    the business school

9
More methods
  • Complete a method sittingInTraffic which returns
    true if a students commute is greater than 2
    miles and they do no have a car or their commute
    is over 10 miles and they do have a car
  • Complete a method bumming, which returns true if
    a student is a freshmen or a computer science
    major and taking 15 or more hours this term or
    the hardest class this term is CS303e

10
More Methods
  • Write a method estimatedGPA which returns a
    double base on the following
  • All freshmen students' estimated GPA is 3.0
  • All students in the college of Engineering and
    students in Natural Science majoring in math,
    chemistry, or computer science have an estimated
    GPA of 2.75
  • All students in the college of liberal arts and
    education have an estimated GPA of 3.95
  • Everyone else's GPA is estimated to be 3.5

11
boolean types
  • Variables of type boolean may be declared
  • boolean happy true
  • true and false are reserved words in Java
  • boolean variables may be used where a boolean
    expression may be used.
  • boolean variables may also be assigned the value
    of a boolean expression
  • happy (hours lt 12) (year ! FRESHMEN)
Write a Comment
User Comments (0)
About PowerShow.com