Download comp4_unit5c_lecture_slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 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 c
2
Programming Constructs
•
•
•
•
•
•
•
Declarations (variable/constant)
Assignment Statements
Expressions
Input and Output (I/O) Statements
Control Structures
Data Structures
Modules
– Procedures
– Methods
– Objects
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
3
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)
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
4
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
5
Java Data Types
• Java is strongly typed
– All variables must be declared with a type
• Java data types
– Primitive
int, double, float, char, boolean
– Class
String
Other user/library defined types
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
6
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
7
Assignment Statements
• An assignment statement is used to assign a
value to a variable
age = 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"
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
8
Values and Expressions
• Values can be literals such as
18
2.5
'f'
• Values can be expressions such as
weight/2
5 + age
3 + 2/5 * 15
n*m
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
9
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
10
Expression Examples
bmi = weight / (height * height);
age = age + 1;
tricky = 3 + 5 * 2;
What is the value of tricky after the assignment?
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
11
Input and Output
• All programming languages support data input
– Keyboard
– Files
• All programming languages support data output
– Screen
– Files
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
12
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);
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
13
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();
• Example
age = keyboard.nextInt();
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
14
Exercise
• Write a Java program that calculates BMI (body
mass index)
• Read in weight (kg)
• Read in height (m)
• Calculate BMI
• Output BMI
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
15
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
16
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. }
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
17
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
18
Computer Programming
Summary – 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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
19
Computer Programming
References – 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].
http://www.clker.com/clipart-26448.html.
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Computer Programming
Lecture c
Available from:
20