comp4_unit5e_lecture_slides

advertisement
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..
Computer Programming
Learning 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)
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
2
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
3
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
4
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
5
Procedural vs. OOP
double circleArea(double
radius)
{
return
3.14*radius*radius; }
}
class Circle
{
double radius;
void setRadius(double
rValue)
{
radius = rValue;
}
double calcArea()
{
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
Introduction to Information and Computer Science
Health IT Workforce Curriculum
calculation?
Computer Programming
Version 3.0/Spring 2012
Lecture e
6
OOP Designs
• Programs are
designed using tools
• UML (Unified
Modeling Language)
is very common
• Example for class of
BMICalculator
BMICalculator
double weight
double height
double bmi
void setWeight(double wValue)
void setHeight(double hValue)
void calcBmi()
void outputBmi()
void outputBmiCategory()
5.2 Table: BMI Calculator
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
7
Inheritance
• Inheritance is a powerful feature of OOP
• Classes can inherit methods and instance
variables
• Makes coding less redundant
• Allows for polymorphism
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
8
UML Diagram: Inheritance
BankingAccount
String accountNum
double balance
void setAccountNum(double aValue)
void setBalance(double bValue)
double getBalance()
void printAccountInfo()
CheckingAccount
SavingsAccount
double overdraft
double interestRate
void setOverdraft(double oValue)
double getOverdraft()
void setInterestRate (double iValue)
void accrueInterest()
5.3 Figure: Child classes inherit all methods and instance variables from
parent class
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
9
Modularity
• Separation of code into components such as
objects
• Non-OOP languages implement modularity
– Procedures
• Allows for
– Reuse of code
– Maintainability
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
10
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
11
Computer Programming
Summary – Lecture e
• This lecture introduced:
–
–
–
–
–
Object-oriented programming
Inheritance
Modularity
Encapsulation
Differences between classes and objects
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
12
Computer Programming
Summary
• 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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture e
13
Computer Programming
References – 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).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
e
Lecture
14
Download