Comp4 Unit5c Lecture Slides

20
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), 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.

Transcript of Comp4 Unit5c Lecture Slides

Page 1: Comp4 Unit5c Lecture Slides

Introduction to Information and Computer Science

Computer Programming

Lecture cThis material (Comp4_Unit5c), 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 Unit5c 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 c

Page 3: Comp4 Unit5c Lecture Slides

Programming Constructs• Declarations (variable/constant)• Assignment Statements• Expressions• Input and Output (I/O) Statements• Control Structures• Data Structures• Modules

– Procedures – Methods– Objects

3Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 4: Comp4 Unit5c Lecture Slides

Variables• Variables store data

– Implemented as memory locations– Referred to by a name

• Data stored by a variable is its value– Value is stored in the memory location

• Its value can be changed (i.e., variable)• Similar construct for constants (value cannot

change)

4Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 5: Comp4 Unit5c Lecture Slides

Data Type• Every variable and constant has a data type

– Knows how much memory to use– Knows how to handle data

• Common data types– Integer– Floating point – Character– Boolean

5Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 6: Comp4 Unit5c Lecture Slides

Java Data Types• Java is strongly typed

– All variables must be declared with a type• Java data types

– Primitiveint, double, float, char, boolean

– ClassStringOther user/library defined types

6Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 7: Comp4 Unit5c Lecture Slides

Declaration Statements• Variable declarations give the name and typeint age;– A variable’s type determines what kinds of values it

can hold– A variable must be declared before it is used in Java

• Java examplesdouble bmi;char gender;boolean completed;Note: Most Java statements end with a semicolon

7Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 8: Comp4 Unit5c Lecture Slides

Assignment Statements• An assignment statement is used to assign a

value to a variableage = 42;

• The “equal sign” is the assignment operator• Options include:

– “The variable age is assigned the value of 42”– “age is assigned 42”– "age gets 42"

8Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 9: Comp4 Unit5c Lecture Slides

Values and Expressions

• Values can be literals such as182.5'f'

• Values can be expressions such asweight/25 + age3 + 2/5 * 15n*m

9Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 10: Comp4 Unit5c Lecture Slides

Arithmetic Expressions• Arithmetic expressions contain operators and

evaluate to a value+, -, *, /

• Order of evaluation is determined by precedence1.Expressions in parentheses evaluated first2.Then *,/3.Then +, -4.Same order of precedence evaluated left to right

10Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 11: Comp4 Unit5c Lecture Slides

Expression Examples

bmi = weight / (height * height);age = age + 1;tricky = 3 + 5 * 2; What is the value of tricky after the assignment?

11Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 12: Comp4 Unit5c Lecture Slides

Input and Output

• All programming languages support data input– Keyboard– Files

• All programming languages support data output– Screen– Files

12Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 13: Comp4 Unit5c Lecture Slides

Screen Output in Java

• Output is done usingSystem.out.print() Does not include line

returnSystem.out.println() Includes line return

• Code examplesSystem.out.println("Hello World!");System.out.print("My name is ");System.out.println(name);System.out.println(gender);

13Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 14: Comp4 Unit5c Lecture Slides

Keyboard Input in Java• Keyboard input is more complicated• Must include package java.util• Must create object of Scanner class

Scanner keyboard = new Scanner(System.in);• Use methods in Scanner class

next(); nextLine(); nextDouble(); next Int();

• Exampleage = keyboard.nextInt();

14Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 15: Comp4 Unit5c Lecture Slides

Exercise• Write a Java program that calculates BMI (body

mass index)• Read in weight (kg)• Read in height (m)• Calculate BMI• Output BMI

15Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 16: Comp4 Unit5c Lecture Slides

Program Design

• Prompt user for weight in kg• Read in weight • Prompt user for height in m• Read in height• Calculate BMI

BMI = weight/(height * height)• Output BMI

16Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 17: Comp4 Unit5c Lecture Slides

1.import java.util.*; //import package for keyboard input2. public class CalcBMI //Start of class and program3. {4. public static void main(String[] args) //main 5. {6. double bmi, weight, height; //variables7. Scanner keyboard = new Scanner(System.in); //input8. 9. System.out.println("Welcome to the BMI calculator");10. System.out.println("Enter weight in kg");11. weight = keyboard.nextDouble();12. System.out.println("Enter height in m");13. height = keyboard.nextDouble();14. bmi = weight/(height*height);15. System.out.print("BMI is ");16. System.out.println(bmi);17. }18. }

17Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 18: Comp4 Unit5c Lecture Slides

Sample Output

Welcome to the BMI calculatorEnter weight in kg68 (green)Enter height in m1.72 (green)BMI is 22.985397512168742

Note: Values in green are entered by the user

18Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 19: Comp4 Unit5c Lecture Slides

Computer ProgrammingSummary – Lecture c

• Programming languages consist of common constructs

• Variables store data and have a data type• Variables can be assigned values or

expressions• Java provides statements for variable

declarations, assignments and expressions• Java provides statements and classes for input

and output

19Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Page 20: Comp4 Unit5c Lecture Slides

Computer ProgrammingReferences – Lecture c

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.

Images • Slide 16: Scale Image [image on the Internet]. c 2009 [Updated 4/2/2009; cited 11/12/2011]. Available from:

http://www.clker.com/clipart-26448.html.

20Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c