comp4 unit5e lecture slides

14
Introduction to Information and Computer Science Computer Programming Lecture e This material (Comp4_Unit5e) 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

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comp4 Unit5e Lecture Slides

Introduction to Information and Computer Science

Computer Programming

Lecture eThis material (Comp4_Unit5e) 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 Unit5e 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 e

Page 3: Comp4 Unit5e Lecture Slides

Object-Oriented Programming• Object-oriented programming (OOP) is a

paradigm• Very popular today

– C++, C#, Java, Python, Ruby• Supports software engineering principles• Graphical user interface (GUI) programming

naturally conforms to OOP

3Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 4: Comp4 Unit5e Lecture Slides

Objects• Objects have

– Identity (name)– Attributes (instance variables)– Behavior (methods)

• OOP is a way of organizing code– Data and related methods stored together

• OOP allows for code reuse– Modularity– Inheritance

4Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 5: Comp4 Unit5e Lecture Slides

Classes vs. Objects

• Classes are the code definition for objects• They are the "blueprint“ for objects• Objects are created when the program runs

– Instantiation– Similar to declaring a variable

5Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 6: Comp4 Unit5e Lecture Slides

Procedural vs. OOPdouble circleArea(double

radius){ return 3.14*radius*radius;

}}

• In class, radius is stored with the calcArea method

• In procedure, radius is passed into calcArea as a parameter

• How to add circumference calculation?

class Circle{ double radius; void setRadius(double

rValue) { radius = rValue; } double calcArea() { return

3.14*radius*radius; }}

6Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 7: Comp4 Unit5e Lecture Slides

OOP Designs

• Programs are designed using tools

• UML (Unified Modeling Language) is very common

• Example for class of BMICalculator

BMICalculatordouble weightdouble heightdouble bmi

void setWeight(double wValue)void setHeight(double hValue)void calcBmi()void outputBmi()void outputBmiCategory()

5.2 Table: BMI Calculator

7Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 8: Comp4 Unit5e Lecture Slides

Inheritance

• Inheritance is a powerful feature of OOP• Classes can inherit methods and instance

variables• Makes coding less redundant• Allows for polymorphism

8Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 9: Comp4 Unit5e Lecture Slides

BankingAccountString accountNumdouble balancevoid setAccountNum(double aValue)void setBalance(double bValue)double getBalance()void printAccountInfo()

CheckingAccountdouble overdraft

void setOverdraft(double oValue)double getOverdraft()

SavingsAccountdouble interestRate

void setInterestRate (double iValue)void accrueInterest()

UML Diagram: Inheritance

5.3 Figure: Child classes inherit all methods and instance variables from parent class

9Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 10: Comp4 Unit5e Lecture Slides

Modularity

• Separation of code into components such as objects

• Non-OOP languages implement modularity – Procedures

• Allows for – Reuse of code– Maintainability

10Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 11: Comp4 Unit5e Lecture Slides

Encapsulation

• Objects can declare methods and instance variables to be private or public– Typically, instance variables are private– Some (all) methods are public

• Class definition controls– Valid ranges for values– Rules for setting values, calling methods– Details of implementation are hidden

• Interface is public methods and documentation

11Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 12: Comp4 Unit5e Lecture Slides

Computer ProgrammingSummary – Lecture e

• This lecture introduced:– Object-oriented programming– Inheritance– Modularity– Encapsulation– Differences between classes and objects

12Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 13: Comp4 Unit5e Lecture Slides

Computer ProgrammingSummary

• This unit covered:– The purpose of programming languages– Different types of programming languages– The compilation/interpreter process– Programming language constructs– Object-oriented programming (OOP)– How programs are designed and implemented– What code looks like– What objects are and why they are used

13Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e

Page 14: Comp4 Unit5e Lecture Slides

Computer ProgrammingReferences – Lecture e

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/• Lesson: Object-Oriented Programming Concepts inThe Java Tutorials. (2011). Retrieved 2011 Nov 13 from:

http://download.oracle.com/javase/tutorial/java/concepts/.• 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.2 Table: BMI Calculator (Hribar, 2011)• 5.3 Figure: Child classes inherit all methods and instance variables from parent class (Hribar, 2011).

14Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Computer Programming

Lecture e