CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

csci 144 introduction to computer science
SMART_READER_LITE
LIVE PREVIEW

CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017 PLU Computer Science Before class Quiz on Monday Turn-in Buggy codes Solutions to Buggy


slide-1
SLIDE 1

PLU Computer Science

CSCI 144 - Introduction to Computer Science

Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017

1

slide-2
SLIDE 2

PLU Computer Science

Before class

  • Quiz on Monday
  • Turn-in Buggy codes
  • Solutions to Buggy codes
  • CAT QA
slide-3
SLIDE 3

PLU Computer Science

Boolean Expressions and If Statements

slide-4
SLIDE 4

PLU Computer Science

Sequential Execution

4

System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; hoursLeft = HOURS_NEEDED - creditHours; System.out.printf("Your GPA is %.2f\n", gpa); System.out.printf("You need %d more hours", hoursLeft); System.out.println(" to complete your degree."); Execution flow

slide-5
SLIDE 5

PLU Computer Science

5

Wear a coat. true Is it cold

  • utside?

Conditional Execution Java code

if( coldOutside ) wearCoat(); false

slide-6
SLIDE 6

PLU Computer Science

6

Wear a coat. true Is it cold

  • utside?

Wear a hat. Wear gloves. if( coldOutside ) { wearCoat(); wearHat(); wearGloves(); } Curly braces define a block of statements. false

slide-7
SLIDE 7

PLU Computer Science

7

Statement

  • r

block true Boolean Expression? if( boolean expression ) statement or block A boolean expression is something that evaluates to a boolean value (true or false) false

slide-8
SLIDE 8

PLU Computer Science

Think/Pair/Share

Write down at least two instances where you might have used a conditional in one of your previous lab assignments

  • r pair exercises.
slide-9
SLIDE 9

PLU Computer Science

Relational Operators

slide-10
SLIDE 10

PLU Computer Science

Boolean Operators

10

Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to

slide-11
SLIDE 11

PLU Computer Science

Boolean Expressions

11

Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y? x == y Is x equal to y? x != y Is x not equal to y?

slide-12
SLIDE 12

PLU Computer Science

GPA Example with Conditional Execution

12

System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; hoursLeft = HOURS_NEEDED - creditHours; System.out.printf("Your GPA is %.2f\n", gpa); if( gpa >= 3.7 ) System.out.println("Wow, you're a hard worker!"); System.out.printf("You need %d more hours to complete your degree.\n", hoursLeft); if( hoursLeft < 0 ) { System.out.println("You can graduate!"); System.out.println("See your advisor to get graduation clearance!"); }

Note the use of indentation and curly braces.

slide-13
SLIDE 13

PLU Computer Science

13

System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); gpa = gradePoints / creditHours; hoursLeft = HOURS_NEEDED - creditHours; System.out.printf("Your GPA is %.2f\n", gpa); if( gpa >= 3.7 ) System.out.println("Wow, you're a hard worker!"); System.out.printf("You need %d more hours to complete your degree.\n", hoursLeft); if( hoursLeft < 0 ) System.out.println("You can graduate!"); System.out.println("See your advisor to get graduation clearance!");

Indentation alone does not define a block. What does this do when hoursLeft == 20?

slide-14
SLIDE 14

PLU Computer Science

14

Wear a coat. true Is it cold

  • utside?

Wear shorts. false

if( coldOutside ) { wearCoat(); } else { wearShorts(); } if( boolean expression ) statement or block else statement or block In general The "else" part is

  • ptional
slide-15
SLIDE 15

PLU Computer Science

if-else

15

System.out.print("How many credit hours have you taken? "); creditHours = scan.nextInt(); System.out.print("How many grade points have you earned? "); gradePoints = scan.nextDouble(); if( creditHours > 0 ) { gpa = gradePoints / creditHours; } else { gpa = 0.0; } System.out.printf("Your GPA is %.2f\n", gpa); if-else avoids division by zero and computing with invalid (negative) input.

slide-16
SLIDE 16

PLU Computer Science

Practice

16

Write an if statement to calculate quizAve (sum divided by numQuizzes) only if numQuizzes is greater than 0.

slide-17
SLIDE 17

PLU Computer Science

Practice

17

if( numQuizzes > 0 ) { quizAve = sum / numQuizzes; }

Write an if statement to calculate quizAve (sum divided by numQuizzes) only if numQuizzes is greater than 0.

slide-18
SLIDE 18

PLU Computer Science

Practice

18

Write an if statement to calculate tax (totalSales times TAX_RATE) only if forProfit (char) is 'y'.

slide-19
SLIDE 19

PLU Computer Science

Practice

19

if( forProfit == 'y' ) { tax = totalSales * TAX_RATE; }

Write an if statement to calculate tax (totalSales times TAX_RATE) only if forProfit (char) is 'y'.

slide-20
SLIDE 20

PLU Computer Science

Practice

20

Write an if-else statement to print "Go Swimming!" and set cost to $5 if the temperature (temp) is at least 80 degrees, otherwise print "Take a hike" and set cost to $0.

slide-21
SLIDE 21

PLU Computer Science

Practice

21

if( temp >= 80 ) { System.out.println("Go Swimming!"); cost = 5.0; } else { System.out.println("Take a hike"); cost = 0.0; }

Write an if-else statement to print "Go Swimming!" and set cost to $5 if the temperature (temp) is at least 80 degrees, otherwise print "Take a hike" and set cost to $0.

slide-22
SLIDE 22

PLU Computer Science

Practice: IfPractice.java Check the class website and Turn-in on next Monday!

22