PLU Computer Science
CSCI 144 - Introduction to Computer Science
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017
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
PLU Computer Science
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Fall 2017
PLU Computer Science
PLU Computer Science
PLU Computer Science
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
PLU Computer Science
5
Wear a coat. true Is it cold
Conditional Execution Java code
if( coldOutside ) wearCoat(); false
PLU Computer Science
6
Wear a coat. true Is it cold
Wear a hat. Wear gloves. if( coldOutside ) { wearCoat(); wearHat(); wearGloves(); } Curly braces define a block of statements. false
PLU Computer Science
7
Statement
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
PLU Computer Science
PLU Computer Science
PLU Computer Science
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
PLU Computer Science
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?
PLU Computer Science
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.
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?
PLU Computer Science
14
Wear a coat. true Is it cold
Wear shorts. false
if( coldOutside ) { wearCoat(); } else { wearShorts(); } if( boolean expression ) statement or block else statement or block In general The "else" part is
PLU Computer Science
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.
PLU Computer Science
16
PLU Computer Science
17
if( numQuizzes > 0 ) { quizAve = sum / numQuizzes; }
PLU Computer Science
18
PLU Computer Science
19
if( forProfit == 'y' ) { tax = totalSales * TAX_RATE; }
PLU Computer Science
20
PLU Computer Science
21
if( temp >= 80 ) { System.out.println("Go Swimming!"); cost = 5.0; } else { System.out.println("Take a hike"); cost = 0.0; }
PLU Computer Science
22