Introduction to Information and Computer Science Computer Programming Lecture c This material...

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

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.

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

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

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

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

Java Data Types

• Java is strongly typed– All variables must be declared with a type

• Java data types– Primitive

int, double, float, char, boolean

– ClassString

Other user/library defined types

6Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Declaration Statements

• Variable declarations give the name and type

int 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 examples

double 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

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

Values and Expressions

• Values can be literals such as18

2.5

'f'

• Values can be expressions such asweight/2

5 + age

3 + 2/5 * 15

n*m

9Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture c

Arithmetic Expressions

• Arithmetic expressions contain operators and evaluate to a value+, -, *, /

• Order of evaluation is determined by precedence

1.Expressions in parentheses evaluated first

2.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

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

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

Screen Output in Java

• Output is done using

System.out.print() Does not include line return

System.out.println() Includes line return• Code examples

System.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

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 classnext(); 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

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

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

1.import java.util.*; //import package for keyboard input

2. public class CalcBMI //Start of class and program

3. {

4. public static void main(String[] args) //main

5. {

6. double bmi, weight, height; //variables

7. Scanner keyboard = new Scanner(System.in); //input

8.

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

Sample Output

Welcome to the BMI calculator

Enter weight in kg

68 (green)

Enter height in m

1.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

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

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