comp4 unit5d lecture slides

22
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015..

Upload: health-it-workforce-curriculum-2012

Post on 06-May-2017

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comp4 Unit5d Lecture Slides

Introduction to Information and Computer Science

Computer Programming

Lecture dThis material (Comp4_Unit5d) was developed by Oregon Health and Science University, funded by the Department of Health

and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015..

Page 2: Comp4 Unit5d Lecture Slides

Computer ProgrammingLearning Objectives

• Define the purpose of programming languages. (Lecture a)

• Differentiate between the different types of programming languages and list commonly used ones. (Lecture a)

• Explain the compiling and interpreting process for computer programs. (Lecture b)

• Learn basic programming concepts including variable declarations, assignment statements, expressions, conditional statements and loops. (Lectures c, d)

• Describe advanced programming concepts including objects and modularity. (Lecture e)

2Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 3: Comp4 Unit5d Lecture Slides

Control Structures• Control structures determine the execution of a

program• Conditional statements

– if– case or switch

• Repetitive statements – loops– while– for– do while

3Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 4: Comp4 Unit5d Lecture Slides

If Statements in Java

• If statements have a condition• When the condition is true, the body of the if

statement executes• Example:

if (weight < 0) //conditions{ //body of if System.out.println("Error!");}

4Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 5: Comp4 Unit5d Lecture Slides

If Else Statements in Java

• If statements can include an else clause

• Else clause executes when condition is false

if (weight < 0){ System.out.println("Error!");}else{ System.out.println("No

error");}

5Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 6: Comp4 Unit5d Lecture Slides

Nested If statements• If statements can

have multiple conditions

• When number is less than zero"Negative" and"Done" are printed to the screen

if (number < 0){ System.out.println("Negative");}else if (number > 0){ System.out.println("Positive");}else{ System.out.println("Zero");}System.out.println("Done")

6Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 7: Comp4 Unit5d Lecture Slides

Conditional Expressions

• Use comparator operators<, > (less than, greater than)<=, >= (less than or equal to, greater than or equal to)==, != (is equal to, is not equal to)

• Use logical operators to combine comparisons&& (AND): Both comparisons must be true|| (OR): Either comparison must be true! (NOT): Condition must be false

7Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 8: Comp4 Unit5d Lecture Slides

Code Example

• Write an if statement that will output the category for a calculated BMI (body mass index)

8Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

BMI Category< 18.5 Underweight

18.5 - 24.9999 Normal

25.0 - 29.9999 Overweight

>= 30 Obese

5.1 Table: Example of more complex conditional expressions.

Page 9: Comp4 Unit5d Lecture Slides

if (bmi < 18.5){ System.out.println("Underweight");}else if ((bmi >= 18.5) && (bmi < 25.0)){ System.out.println("Normal weight");}else if ((bmi >= 25.0) && (bmi < 30.0)){ System.out.println("Overweight");}else{ System.out.println("Obese");}

9Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 10: Comp4 Unit5d Lecture Slides

Loops in Java• Loops are sections of code that will continue to

repeat while a condition is true• “While” loop is simplest loop• Examplecount = 5;

while (count >= 0) // while condition { System.out.println(count); count = count - 1; //value changes

}

10Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 11: Comp4 Unit5d Lecture Slides

While Loop, contd.

• Output from statement543210

11Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 12: Comp4 Unit5d Lecture Slides

For Loop• “For” loop is another type of loop• Used when the number of iterations is known• Heading sets loop control variable, compares it,

and updates it• Example

for (i = 0; i < 5; i++) { System.out.println(i); }

12Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 13: Comp4 Unit5d Lecture Slides

For Loop, contd.

• Output from example01234

13Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 14: Comp4 Unit5d Lecture Slides

Exercise

• Modify BMI program – Output BMI category– Calculate BMI more than once

14Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 15: Comp4 Unit5d Lecture Slides

Program Design1. Read in weight (kg)2. Read in height (m)3. Calculate BMI

BMI = weight/(height * height)4. Output BMI5. Output BMI category6. Prompt user if want to calculate another BMI7. If yes, go back to step 1 8. If no, end

15Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 16: Comp4 Unit5d Lecture Slides

import java.util.*;public class CalcBMI{ public static void main(String[] args) { double bmi, weight, height; int anotherBMI; Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to the BMI calculator"); anotherBMI = 1; while (anotherBMI == 1) { System.out.println("Enter weight in kg"); weight = keyboard.nextDouble(); System.out.println("Enter height in m"); height = keyboard.nextDouble(); bmi = weight/(height*height); System.out.print("BMI is "); System.out.println(bmi);

16Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 17: Comp4 Unit5d Lecture Slides

… anotherBMI = 1; while (anotherBMI == 1) { …//input height, weight; calculate BMI if (bmi < 18.5) System.out.println("Underweight"); else if ((bmi >= 18.5) && (bmi < 25.0)) System.out.println("Normal weight"); else if ((bmi >= 25.0) && (bmi < 30.0)) System.out.println("Overweight"); else System.out.println("Obese"); System.out.println("Do you want to calculate another?"); System.out.println("Enter 1 for yes and 0 for no"); anotherBMI = keyboard.nextInt(); } System.out.println("Good Bye!"); }}

17Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 18: Comp4 Unit5d Lecture Slides

Sample OutputWelcome to the BMI calculatorEnter weight in kg68 (green)Enter height in m1.27 (green)BMI is 42.16008432016864ObeseDo you want to calculate another?Enter 1 for yes and 0 for no1 (green)Enter weight in kg55 (green)Enter height in m1.5 (green)BMI is 24.444444444444443Normal weightDo you want to calculate another?Enter 1 for yes and 0 for no0 (green)Good Bye!

18Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 19: Comp4 Unit5d Lecture Slides

Data Structures• Data structures are used for storing multiple

pieces of data together• Arrays are a simple data structure• Example

double[] grade = new double[10];Array of 10 doubles for storing gradesgrade[1] = 95.0;

• Other data structures available– Linked lists, trees, hash tables

19Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 20: Comp4 Unit5d Lecture Slides

Modules• Way of separating code, usually by function

– Allows for reuse– Easier to maintain

• Procedures, functions, methods are all modules• Objects are as well• Examplepublic void printAreaCircle(double radius){ double area = 3.14*radius*radius;

System.out.println("Area is " + area);}

20Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 21: Comp4 Unit5d Lecture Slides

Computer ProgrammingSummary – Lecture d

• Control structures affect the order of execution• “If” statements allow for conditional execution• Loops allow for repeated execution• Data structures allow for data to be grouped

together• Modules are used to encapsulate code

21Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d

Page 22: Comp4 Unit5d Lecture Slides

Computer ProgrammingReferences – Lecture d

References• Eck, David. (2011) Introduction to Programming Using Java, Sixth Edition. [updated 2011 Jul 18; cited 2011 Nov

13]: Available from: http://math.hws.edu/javanotes/• Morley Deborah, Parker Charles S. (2010). Chapter 13: Program Development and Programming Languages.

In: Understanding Computers Today and Tomorrow.12th ed. Boston: Course Technology.• Parsons JJ, Oja D. (2010). Chapter 12: Computer Programming. In: New Perspectives on Computer Concepts

2011: Comprehensive. 13th ed. Boston: Course Technology.• The Java Language: An Overview. [Webpage]. c 2007. [updated 2007 Dec 17; cited 21 March 2011]. Available

from: http://java.sun.com/docs/overviews/java/java-overview-1.html• Sierra Kathy, Bates Bert. (2009). Head First Java, Second Edition. O’Reilly Media.

Charts, Tables, Figures• 5.1 Table: Example of more complex conditional expressions.

22Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture d