SlideShare a Scribd company logo
1 of 22
Introduction to Computer Science
Computer Programming
Lecture c
This material (Comp 4 Unit 4) was developed by Oregon Health & Science University, funded by the Department
of Health and Human Services, Office of the National Coordinator for Health Information Technology under
Award Number 90WT0001.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
Computer Programming
Learning Objectives - 1
• 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)
2
Computer Programming
Learning Objectives - 2
• 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)
3
Programming Constructs
• Declarations
(variables/constants)
• Assignment
Statements
• Expressions
• Input and Output
(I/O) Statements
• Control Structures
• Data Structures
• Modules
– Procedures
– Methods
– Objects
4
Variables
• Variables store data
– Referred to by unique names
– Point to memory locations
• 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)
5
Data Type
• Every variable and constant belongs to
some data type
– Knows how much memory to use
– Knows how to handle data
• Common data types
– Integer
– Floating point
– Character
– Boolean
6
Java Data Types
• Java is a strongly typed language
– All variables must be declared and their types
specified before they can be used
• Java data types
– Primitive
o int, double, float, char, boolean
– Class
o String
o Other user/library defined types
7
Declaration Statements in Java
• Variable declarations specifies name and
type; variable must be declared before use
int age;
• Java examples
double bmi;
char gender;
boolean completed;
Note: Most Java statements end with a semicolon
8
Assignment Statements
• An assignment statement is used to
assign a value to a variable
age = 42;
• The “equal sign” is the assignment
operator
9
Values and Expressions
• Values can be specified by literals such as
18
2.5
'a'
• Values can be specified by expressions
such as
weight/2
5 + age
3 + 2/5 * 15
n*m
10
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
11
Expression Examples
bmi = weight / (height * height);
age = age + 1;
tricky = 3 + 5 * 2;
What is the value of tricky after
the assignment?
12
Input and Output
• All programming languages support data
input
– Keyboard
– Files
• All programming languages support data
output
– Screen
– Files
13
Screen Output in Java
• Output can be written using
System.out.println() prints the specified
string and moves the cursor to a new line
System.out.print() prints the specified
string and does not move cursor to a new line
• Code examples
System.out.println("Hello World!");
System.out.print("My name is ");
System.out.println(name);
System.out.println(gender);
14
Keyboard Input in Java
• Keyboard input is more complicated
• One way is to include package java.util
• Must create object of the Scanner class
Scanner keyboard =new
Scanner(System.in);
• Use methods in Scanner class
next(); nextLine(); nextDouble();
nextInt();
• Example
age = keyboard.nextInt();
15
Example
• Write a Java program that
calculates BMI (body mass
index)
• Read in weight (kg)
• Read in height (m)
• Calculate BMI
• Output BMI
(tom, 2009, PD-US)
16
Program Design
• Prompt user for weight (kg)
• Read in weight
• Prompt user for height in m
• Read in height
• Calculate BMI using the formula
BMI = weight/(height * height)
• Output BMI
17
Example Java Program
18
Sample Output
Welcome to the BMI calculator
Enter weight in kg
68
Enter height in m
1.72
BMI is 22.985397512168742
Note: Values in purple are entered by the
user
19
Computer Programming
Summary – Lecture c
• There are constructs common for most
programming languages
• Variables store data and have a data type
• Variables can be assigned values or
expressions
• Java provides variable declaration,
assignment, and expression statements
• Java has input and output statements and
classes
20
Computer Programming
References – Lecture c
References
Eck, D. (2011). Introduction to Programming Using Java (6th ed.). Retrieved from
http://math.hws.edu/javanotes/
Morley, D., & Parker, C.S. (2010). Chapter 13: Program Development and Programming
Languages. In Understanding Computers Today and Tomorrow, 12th Edition
introductory. Boston: Course Technology.
Parsons, J.J., & 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. (2007, December). Retrieved March 21, 2011, from
http://java.sun.com/docs/overviews/java/java-overview-1.html
Sierra, K., & Bates, B. (2009). Head First Java (2nd Ed.). O’Reilly Media.
Images
Slide 16: Bathroom scale. [image on the Internet]. User: tom. (2009, April 1). Retrieved
November 12, 2011, from https://openclipart.org/detail/23591/bathroom-scale. This file
has been released into the Public Domain.
21
Introduction to Computer Science
Computer Programming
Lecture c
This material was developed by Oregon
Health & Science University, funded by the
Department of Health and Human Services,
Office of the National Coordinator for Health
Information Technology under Award
Number 90WT0001.
22

More Related Content

What's hot

Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Asfand Hassan
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ FundamentalsSubhasis Nayak
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#Dr.Neeraj Kumar Pandey
 
Mit4021 c# and .net
Mit4021   c# and .netMit4021   c# and .net
Mit4021 c# and .netsmumbahelp
 
B.sc CSIT 2nd semester C++ unit-1
B.sc CSIT  2nd semester C++ unit-1B.sc CSIT  2nd semester C++ unit-1
B.sc CSIT 2nd semester C++ unit-1Tekendra Nath Yogi
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - IntroductionMadishetty Prathibha
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperLee Greffin
 
C programming Lab Manual 15 me47p
C programming Lab Manual 15 me47p C programming Lab Manual 15 me47p
C programming Lab Manual 15 me47p THANMAY JS
 

What's hot (19)

Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
Oop basics
Oop basicsOop basics
Oop basics
 
Presentation c
Presentation cPresentation c
Presentation c
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
C language
C languageC language
C language
 
Core java part1
Core java  part1Core java  part1
Core java part1
 
Mit4021 c# and .net
Mit4021   c# and .netMit4021   c# and .net
Mit4021 c# and .net
 
B.sc CSIT 2nd semester C++ unit-1
B.sc CSIT  2nd semester C++ unit-1B.sc CSIT  2nd semester C++ unit-1
B.sc CSIT 2nd semester C++ unit-1
 
Bca
BcaBca
Bca
 
C++ Course module
C++ Course moduleC++ Course module
C++ Course module
 
Object oriented concepts ppt
Object oriented concepts pptObject oriented concepts ppt
Object oriented concepts ppt
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft DeveloperObject Oriented Programming Overview for the PeopleSoft Developer
Object Oriented Programming Overview for the PeopleSoft Developer
 
C programming Lab Manual 15 me47p
C programming Lab Manual 15 me47p C programming Lab Manual 15 me47p
C programming Lab Manual 15 me47p
 
Introduction to programing languages part 3
Introduction to programing languages part 3Introduction to programing languages part 3
Introduction to programing languages part 3
 
Introduction to ‘C’ Language
Introduction to ‘C’ LanguageIntroduction to ‘C’ Language
Introduction to ‘C’ Language
 
Introduction to programing languages part 2
Introduction to programing languages   part 2Introduction to programing languages   part 2
Introduction to programing languages part 2
 

Similar to Computer Programming - Lecture C

Similar to Computer Programming - Lecture C (20)

C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdf
 
Computer Programming - Lecture E
Computer Programming - Lecture EComputer Programming - Lecture E
Computer Programming - Lecture E
 
Programming in C [Module One]
Programming in C [Module One]Programming in C [Module One]
Programming in C [Module One]
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
unit2.pptx
unit2.pptxunit2.pptx
unit2.pptx
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
CP c++ programing project Unit 1 intro.pdf
CP c++ programing project  Unit 1 intro.pdfCP c++ programing project  Unit 1 intro.pdf
CP c++ programing project Unit 1 intro.pdf
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdf
 
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
2R-3KS03-OOP_UNIT-I (Part-A)_2023-24.pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
8844632.ppt
8844632.ppt8844632.ppt
8844632.ppt
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
history of c.ppt
history of c.ppthistory of c.ppt
history of c.ppt
 
Python for Physical Science.pdf
Python for Physical Science.pdfPython for Physical Science.pdf
Python for Physical Science.pdf
 

More from CMDLearning

What is Health Informatics - Lecture B
What is Health Informatics - Lecture BWhat is Health Informatics - Lecture B
What is Health Informatics - Lecture BCMDLearning
 
Evolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DEvolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DCMDLearning
 
Evolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CEvolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CCMDLearning
 
Evolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BEvolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BCMDLearning
 
Evolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture AEvolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture ACMDLearning
 
Public Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CPublic Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CCMDLearning
 
Public Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BPublic Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BCMDLearning
 
Public Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture APublic Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture ACMDLearning
 
Public Health (Part 1) Lecture C
Public Health (Part 1) Lecture CPublic Health (Part 1) Lecture C
Public Health (Part 1) Lecture CCMDLearning
 
Public Health (Part 1) Lecture B
Public Health (Part 1) Lecture BPublic Health (Part 1) Lecture B
Public Health (Part 1) Lecture BCMDLearning
 
Public Health (Part 1) Lecture A
Public Health (Part 1) Lecture APublic Health (Part 1) Lecture A
Public Health (Part 1) Lecture ACMDLearning
 
Regulating Healthcare - Lecture E
Regulating Healthcare - Lecture ERegulating Healthcare - Lecture E
Regulating Healthcare - Lecture ECMDLearning
 
Regulating Healthcare - Lecture D
Regulating Healthcare - Lecture DRegulating Healthcare - Lecture D
Regulating Healthcare - Lecture DCMDLearning
 
Regulating Healthcare - Lecture C
Regulating Healthcare - Lecture CRegulating Healthcare - Lecture C
Regulating Healthcare - Lecture CCMDLearning
 
Regulating Healthcare - Lecture A
Regulating Healthcare - Lecture ARegulating Healthcare - Lecture A
Regulating Healthcare - Lecture ACMDLearning
 
Regulating Healthcare - Lecture B
Regulating Healthcare - Lecture BRegulating Healthcare - Lecture B
Regulating Healthcare - Lecture BCMDLearning
 
Financing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CFinancing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CCMDLearning
 
Financing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BFinancing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BCMDLearning
 
Financing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture AFinancing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture ACMDLearning
 
Financing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DFinancing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DCMDLearning
 

More from CMDLearning (20)

What is Health Informatics - Lecture B
What is Health Informatics - Lecture BWhat is Health Informatics - Lecture B
What is Health Informatics - Lecture B
 
Evolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture DEvolution of and Trends in Health Care - Lecture D
Evolution of and Trends in Health Care - Lecture D
 
Evolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture CEvolution of and Trends in Health Care - Lecture C
Evolution of and Trends in Health Care - Lecture C
 
Evolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture BEvolution of and Trends in Health Care - Lecture B
Evolution of and Trends in Health Care - Lecture B
 
Evolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture AEvolution of and Trends in Health Care - Lecture A
Evolution of and Trends in Health Care - Lecture A
 
Public Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture CPublic Healthcare (Part 2) Lecture C
Public Healthcare (Part 2) Lecture C
 
Public Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture BPublic Healthcare (Part 2) Lecture B
Public Healthcare (Part 2) Lecture B
 
Public Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture APublic Healthcare (Part 2) Lecture A
Public Healthcare (Part 2) Lecture A
 
Public Health (Part 1) Lecture C
Public Health (Part 1) Lecture CPublic Health (Part 1) Lecture C
Public Health (Part 1) Lecture C
 
Public Health (Part 1) Lecture B
Public Health (Part 1) Lecture BPublic Health (Part 1) Lecture B
Public Health (Part 1) Lecture B
 
Public Health (Part 1) Lecture A
Public Health (Part 1) Lecture APublic Health (Part 1) Lecture A
Public Health (Part 1) Lecture A
 
Regulating Healthcare - Lecture E
Regulating Healthcare - Lecture ERegulating Healthcare - Lecture E
Regulating Healthcare - Lecture E
 
Regulating Healthcare - Lecture D
Regulating Healthcare - Lecture DRegulating Healthcare - Lecture D
Regulating Healthcare - Lecture D
 
Regulating Healthcare - Lecture C
Regulating Healthcare - Lecture CRegulating Healthcare - Lecture C
Regulating Healthcare - Lecture C
 
Regulating Healthcare - Lecture A
Regulating Healthcare - Lecture ARegulating Healthcare - Lecture A
Regulating Healthcare - Lecture A
 
Regulating Healthcare - Lecture B
Regulating Healthcare - Lecture BRegulating Healthcare - Lecture B
Regulating Healthcare - Lecture B
 
Financing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture CFinancing Healthcare (Part 2) Lecture C
Financing Healthcare (Part 2) Lecture C
 
Financing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture BFinancing Healthcare (Part 2) Lecture B
Financing Healthcare (Part 2) Lecture B
 
Financing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture AFinancing Healthcare (Part 2) Lecture A
Financing Healthcare (Part 2) Lecture A
 
Financing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture DFinancing Healthcare (Part 2) Lecture D
Financing Healthcare (Part 2) Lecture D
 

Recently uploaded

Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...
Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...
Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...gurkirankumar98700
 
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...Niamh verma
 
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋Sheetaleventcompany
 
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.ktanvi103
 
VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171Call Girls Service Gurgaon
 
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF ...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF  ...❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF  ...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF ...Gfnyt.com
 
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetHubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Jaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthan
Jaipur Call Girls 9257276172 Call Girl in Jaipur RajasthanJaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthan
Jaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthanindiancallgirl4rent
 
Bangalore call girl 👯‍♀️@ Simran Independent Call Girls in Bangalore GIUXUZ...
Bangalore call girl  👯‍♀️@ Simran Independent Call Girls in Bangalore  GIUXUZ...Bangalore call girl  👯‍♀️@ Simran Independent Call Girls in Bangalore  GIUXUZ...
Bangalore call girl 👯‍♀️@ Simran Independent Call Girls in Bangalore GIUXUZ...Gfnyt
 
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...chandigarhentertainm
 
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetCall Girls Service
 
Call Girls Service In Goa 💋 9316020077💋 Goa Call Girls By Russian Call Girl...
Call Girls Service In Goa  💋 9316020077💋 Goa Call Girls  By Russian Call Girl...Call Girls Service In Goa  💋 9316020077💋 Goa Call Girls  By Russian Call Girl...
Call Girls Service In Goa 💋 9316020077💋 Goa Call Girls By Russian Call Girl...russian goa call girl and escorts service
 
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋Sheetaleventcompany
 
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...Sheetaleventcompany
 
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Me
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near MeVIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Me
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Memriyagarg453
 
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...Call Girls Noida
 
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetCall Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meetpriyashah722354
 
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅gragmanisha42
 
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetChandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meetpriyashah722354
 

Recently uploaded (20)

Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...
Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...
Russian Call Girls Lucknow ₹7.5k Pick Up & Drop With Cash Payment 8923113531 ...
 
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...
Call Girls Service Chandigarh Gori WhatsApp ❤7710465962 VIP Call Girls Chandi...
 
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Mumbai Escort Service Call Girls, ₹5000 To 25K With AC💚😋
 
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.
Call Now ☎ 9999965857 !! Call Girls in Hauz Khas Escort Service Delhi N.C.R.
 
VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171VIP Call Girl Sector 10 Noida Call Me: 9711199171
VIP Call Girl Sector 10 Noida Call Me: 9711199171
 
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF ...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF  ...❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF  ...
❤️♀️@ Jaipur Call Girls ❤️♀️@ Jaispreet Call Girl Services in Jaipur QRYPCF ...
 
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetHubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Hubli Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Jaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthan
Jaipur Call Girls 9257276172 Call Girl in Jaipur RajasthanJaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthan
Jaipur Call Girls 9257276172 Call Girl in Jaipur Rajasthan
 
Bangalore call girl 👯‍♀️@ Simran Independent Call Girls in Bangalore GIUXUZ...
Bangalore call girl  👯‍♀️@ Simran Independent Call Girls in Bangalore  GIUXUZ...Bangalore call girl  👯‍♀️@ Simran Independent Call Girls in Bangalore  GIUXUZ...
Bangalore call girl 👯‍♀️@ Simran Independent Call Girls in Bangalore GIUXUZ...
 
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...
❤️Call girls in Jalandhar ☎️9876848877☎️ Call Girl service in Jalandhar☎️ Jal...
 
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real MeetMangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
Mangalore Call Girls 👙 6297143586 👙 Genuine WhatsApp Number for Real Meet
 
Call Girls Service In Goa 💋 9316020077💋 Goa Call Girls By Russian Call Girl...
Call Girls Service In Goa  💋 9316020077💋 Goa Call Girls  By Russian Call Girl...Call Girls Service In Goa  💋 9316020077💋 Goa Call Girls  By Russian Call Girl...
Call Girls Service In Goa 💋 9316020077💋 Goa Call Girls By Russian Call Girl...
 
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋
💚😋Kolkata Escort Service Call Girls, ₹5000 To 25K With AC💚😋
 
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...
Call Girl Amritsar ❤️♀️@ 8725944379 Amritsar Call Girls Near Me ❤️♀️@ Sexy Ca...
 
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Thane Just Call 9907093804 Top Class Call Girl Service Available
 
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Me
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near MeVIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Me
VIP Call Girls Noida Jhanvi 9711199171 Best VIP Call Girls Near Me
 
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...
Vip sexy Call Girls Service In Sector 137,9999965857 Young Female Escorts Ser...
 
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetCall Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Call Girls Chandigarh 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
 
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅
Russian Call Girls Kota * 8250192130 Service starts from just ₹9999 ✅
 
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real MeetChandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
Chandigarh Call Girls 👙 7001035870 👙 Genuine WhatsApp Number for Real Meet
 

Computer Programming - Lecture C

  • 1. Introduction to Computer Science Computer Programming Lecture c This material (Comp 4 Unit 4) was developed by Oregon Health & Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number 90WT0001. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
  • 2. Computer Programming Learning Objectives - 1 • 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) 2
  • 3. Computer Programming Learning Objectives - 2 • 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) 3
  • 4. Programming Constructs • Declarations (variables/constants) • Assignment Statements • Expressions • Input and Output (I/O) Statements • Control Structures • Data Structures • Modules – Procedures – Methods – Objects 4
  • 5. Variables • Variables store data – Referred to by unique names – Point to memory locations • 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) 5
  • 6. Data Type • Every variable and constant belongs to some data type – Knows how much memory to use – Knows how to handle data • Common data types – Integer – Floating point – Character – Boolean 6
  • 7. Java Data Types • Java is a strongly typed language – All variables must be declared and their types specified before they can be used • Java data types – Primitive o int, double, float, char, boolean – Class o String o Other user/library defined types 7
  • 8. Declaration Statements in Java • Variable declarations specifies name and type; variable must be declared before use int age; • Java examples double bmi; char gender; boolean completed; Note: Most Java statements end with a semicolon 8
  • 9. Assignment Statements • An assignment statement is used to assign a value to a variable age = 42; • The “equal sign” is the assignment operator 9
  • 10. Values and Expressions • Values can be specified by literals such as 18 2.5 'a' • Values can be specified by expressions such as weight/2 5 + age 3 + 2/5 * 15 n*m 10
  • 11. 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 11
  • 12. Expression Examples bmi = weight / (height * height); age = age + 1; tricky = 3 + 5 * 2; What is the value of tricky after the assignment? 12
  • 13. Input and Output • All programming languages support data input – Keyboard – Files • All programming languages support data output – Screen – Files 13
  • 14. Screen Output in Java • Output can be written using System.out.println() prints the specified string and moves the cursor to a new line System.out.print() prints the specified string and does not move cursor to a new line • Code examples System.out.println("Hello World!"); System.out.print("My name is "); System.out.println(name); System.out.println(gender); 14
  • 15. Keyboard Input in Java • Keyboard input is more complicated • One way is to include package java.util • Must create object of the Scanner class Scanner keyboard =new Scanner(System.in); • Use methods in Scanner class next(); nextLine(); nextDouble(); nextInt(); • Example age = keyboard.nextInt(); 15
  • 16. Example • Write a Java program that calculates BMI (body mass index) • Read in weight (kg) • Read in height (m) • Calculate BMI • Output BMI (tom, 2009, PD-US) 16
  • 17. Program Design • Prompt user for weight (kg) • Read in weight • Prompt user for height in m • Read in height • Calculate BMI using the formula BMI = weight/(height * height) • Output BMI 17
  • 19. Sample Output Welcome to the BMI calculator Enter weight in kg 68 Enter height in m 1.72 BMI is 22.985397512168742 Note: Values in purple are entered by the user 19
  • 20. Computer Programming Summary – Lecture c • There are constructs common for most programming languages • Variables store data and have a data type • Variables can be assigned values or expressions • Java provides variable declaration, assignment, and expression statements • Java has input and output statements and classes 20
  • 21. Computer Programming References – Lecture c References Eck, D. (2011). Introduction to Programming Using Java (6th ed.). Retrieved from http://math.hws.edu/javanotes/ Morley, D., & Parker, C.S. (2010). Chapter 13: Program Development and Programming Languages. In Understanding Computers Today and Tomorrow, 12th Edition introductory. Boston: Course Technology. Parsons, J.J., & 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. (2007, December). Retrieved March 21, 2011, from http://java.sun.com/docs/overviews/java/java-overview-1.html Sierra, K., & Bates, B. (2009). Head First Java (2nd Ed.). O’Reilly Media. Images Slide 16: Bathroom scale. [image on the Internet]. User: tom. (2009, April 1). Retrieved November 12, 2011, from https://openclipart.org/detail/23591/bathroom-scale. This file has been released into the Public Domain. 21
  • 22. Introduction to Computer Science Computer Programming Lecture c This material was developed by Oregon Health & Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number 90WT0001. 22

Editor's Notes

  1. Welcome to Introduction to Computer Science: Computer Programming. This is lecture c. The component, Introduction to Computer Science, provides a basic overview of computer architecture; data organization, representation, and structure; structure of programming languages; networking, and data communication. It also includes the basic terminology of computing.
  2. The learning objectives for this unit, Computer Programming, are to: Define the purpose of programming languages Differentiate between the different types of programming languages and list commonly used ones Explain the compiling and interpreting process for computer programs
  3. Learn basic programming concepts including variable declarations, assignment statements, expressions, conditional statements, and loops And describe advanced programming concepts including objects and modularity
  4. This lecture focuses on basic programming concepts and includes a program-writing exercise to calculate body mass index. This is the first of two lectures covering basic computing concepts. We turn now to the common constructs of most programming languages. A construct is a basic building block used by a programming language, or the language’s basic elements, key words, commands, and statements. For example, when programming in Java, some of the key words you might use are continue, for, switch, package, and Boolean. When programming in C, some of the constructs you might encounter are if…else, switch…case, and loops. Throughout this lecture, we will use examples from Java to illustrate the details.
  5. The first topic is variables – every programming language uses them and most require them to be declared. When a variable is declared, it means you are defining its data type and you may even set its initial value. Variables are constructs that store data in a computer program. Within a program, the variable is referred to by a unique name. When the program is executed, the variables point to memory locations that hold the corresponding data. The value of a variable is the value that is stored in the given memory location. This value can be changed an unlimited number of times during the program execution; every time the variable’s value changes, the content of the corresponding memory location is updated. There is a similar construct that holds values that remain the same during the program execution; these are called constants. They, too, are stored in memory, but their values cannot be changed.
  6. Every variable and constant belongs to some data type; this refers to what kind of data the variable or constant can be assigned. Data types can be integers, which are whole numbers; floating point numbers, which are numbers with a decimal part; characters, which are any alpha-numeric single character; and Boolean values, which are either true or false, among others. Among other things, the data type of a variable determines how much memory needs to be allocated to store its value. For example, floating point numbers require more space than integers. Also, the program and computer need to know how to handle the data. Different data types encode values into binary numbers differently; hence, a value stored in memory needs to have a type associated with it so that it can be used properly.
  7. Java is a strongly typed language. This means that all variables must be declared with a given data type before they can be used. Many other languages use implicit typing where declarations are not used. Instead, the data type is inferred from the values that are assigned to the variables. Java has two categories of data types: primitive and class types. Primitive types are used for single, simple variables. The slide lists some of the primitive types: int for integer; double and float for floating point numbers; char for character; and Boolean. Class types refer to data types used to create objects. Class types can be defined by users or within Java libraries. A commonly used class type is String, which is a series of characters.
  8. Declaration statements define variables – variables are given a name and their data type is specified, as is required for strongly typed languages. The variable's type determines what values can be assigned to it. For example, a floating point variable cannot hold a character. Typically, we will put variable declarations at the start of a module. Here are some examples of declaration statements in Java: A variable named age that is an integer A variable named bmi that is of type double, which is one of the floating point types A variable named gender that is of type char; this data type will hold one character A variable named completed that is of type Boolean; this type will hold the values true or false, in other words, a one or zero  Note that all these declaration statements have a semicolon at the end of them. Most Java statements end with a semicolon. It should also be noted that not every programming language requires one to explicitly declare variables; some allow implicit declaration of a variable to occur the first time the variable is referenced.
  9. After a variable is declared, then the variable can be used in the program. One way a variable is used in a program is to have a value assigned to it. That is called an assignment statement. In Java, it looks like this example: age equal sign 42 semicolon. The equal sign is called the assignment operator. Note that this is different from the equality operator, which will appear later.
  10. As demonstrated in the previous slide, values can be assigned to variables, but what exactly constitutes a valid value? We can use what are called literals – actual values such as 18, 2.5, or the character “a”. But values can also be specified by expressions. Expressions are composed of some combination of literals, variables, and operators as shown by these examples. The value of the variable “weight divided by 2” is an expression, as is “5 plus the value of the variable age”. An expression can be made up of all literals such as “3 plus 2 divided by 5 multiplied by fifteen”. Or it can be made up of all variables, such as the values of the variables “n” and “m” multiplied together.
  11. Arithmetic expressions are combinations of arithmetic operators, literals, and variables that evaluate to a value. The arithmetic operators include plus, minus, multiply, and divide. The order in which these operators are evaluated is determined by precedence. Any expression in parentheses is evaluated first. Then, any “multiplies and divides” are evaluated, and then the “adds and subtracts.” If all the operators have the same order of precedence, for example, all are additions, then the expression is evaluated in order from left to right.
  12. So, here are some examples of expressions used in assignment statements. Assignment statements set or reset the value stored in a variable. The first assignment assigns to the variable “bmi” the value of weight, divided by height times height. Note that because height times height is in parentheses, it will be evaluated first. Then the value will be divided into weight to finish the evaluation. Another assignment statement often seen is incrementing a variable. This is shown by the second example: age gets age plus one. Note that from a mathematical perspective, the statement age equals age plus one makes no sense. But, remember, equality is not the issue here – this is an assignment. So, if age had the value 30, then the assignment statement would change the age to 30 + 1 or 31. Finally, consider the order of operations to know what value the final assignment statement has. The variable “tricky” is assigned the value of 3 plus 5 times 2. This evaluates to thirteen since 5 times 2 is evaluated first, for a result of ten, then 3 is added to that which makes 13. Note that it is NOT evaluated in order from left to right – this computation would result in a total of sixteen, 3 plus 5, which is 8 times 2, which is sixteen, which is incorrect.
  13. All programming languages provide programming statements for inputting and outputting data; most programs need data in order to do anything useful. Programs can get data from users typing in values using the keyboard or by reading input files. If this were more advanced programming, data could come from databases or from elements on a graphical user interface. Similarly, all programs provide output onto the screen or written out to files. There are other options of output to printers or graphical user interfaces. For these examples in Java, we will focus on showing how to get input from the keyboard and display output to the computer screen.
  14. To write information to the screen in Java, different commands can be used. These are provided in the System.out class – they are “print” and “println”, which stands for printline. Print does not move the cursor to a new line, while println does. Here are some examples of how to use print statements in Java. The first statement, “System-dot-out-dot-println-open parenthesis-quote-hello world-exclamation point-quote-close parenthesis-semicolon” is how to write “Hello World!” to the computer screen. To output the text “Hello World!”, enclose it in quotes. Everything that we want to print goes between the parentheses that appear after the print or println method name. The print command works the same way. The statement “System-dot-out-dot-print-open parenthesis-quote-My name is-quote-close parenthesis-semicolon,” followed by the statement “System-dot-out-dot-println-open parenthesis-name-close parenthesis-semicolon,” would print “My name is” and whatever is stored in the variable name all on one line. Name does NOT have quotes around it, which means that it is a variable. To use a print statement to print a variable, it prints whatever the value is of that variable. Similarly, a user can print out the value of the variable gender by using the statement System-dot-out-dot-println-open parenthesis-gender-close parenthesis-semicolon.
  15. Getting input from the keyboard in Java, is more complicated than writing screen output. There are several ways to do it; the one shown here requires the java.util package. Packages in Java are libraries of code that provide special functionality. In this case, this package is predefined within Java itself but is not automatically included in programs as is System.out. To include the package, use the statement “import java-dot-util-dot-star-semicolon” at the top of the code. An example of this appears on slide 18. Next, it is necessary to create an object of the Scanner class and tell Scanner where to get input. In this case, input will be from the keyboard, which is defined as System.in. The statement to do this is shown here: Scanner keyboard gets new Scanner open parenthesis System-dot-in close parenthesis semicolon. These commands created an object called keyboard that is of type Scanner and gets its input from System.in, which is the keyboard. Methods that are defined in the Scanner class will get input from the keyboard. These methods are: next, which gets the next string or word of characters nextLine, which gets the next line of characters nextDouble, which gets the next double value nextInt, which gets the next integer value  Here is an example of using such a method call in Java programming. Because all these methods produce a value, use them wherever a value or an expression is used, like an assignment statement. In this case, a user assigns to the variable age the value returned by keyboard-dot-nextInt. To fully understand the Scanner class and how to use it, you need to understand object-oriented programming. The last lecture of this unit addresses that topic, as does any textbook on Java programming.
  16. To write a simple interactive program that will calculate body mass index, or BMI, you would enter weight in kilograms and height in meters. The program would then calculate the BMI and output it to the screen.
  17. The first thing to do before writing any code is to design the program. Because it will be a very simple program, the design will not look very different from the program statement. Nevertheless, the design requires listing all the details needed for the program, such as the formula for calculating BMI. Programs are designed using what is called pseudocode. Pseudocode is a written description of what the computer program will do, but written in natural language rather than in a programming language. The pseudocode begins with a prompt to the user for their weight in kilograms. Then, the pseudocode reads in the value entered for the weight and prompts the user to enter their height in meters. Then, the pseudocode reads in the value for their height and calculates the BMI. In metric values, the formula for BMI is weight divided by height squared. Height squared can be written as height times height. Then, the pseudocode outputs the BMI, which finishes the program.
  18. Here is what the Java program looks like, line by line. For reference, the line numbers are shown on the left; these are not part of the code. What is written on the right side of the double forward slashes are the comments. Comments are used to clarify and document a program and do not effect code execution. One, the first statement imports the package java-dot-util. Two, the next line is the first line of the class CalcBMI. This is the name of the class that contains the program. In Java all code must belong to a class; the name of this code is CalcBMI. Three, the next line is the open curly bracket, which is used to signify the start of a class, a method, and so forth. Four, the next line is the first line of the main method. All code in Java is either an instance variable declaration or part of a method definition. Every program in Java must contain a main method. Because this program is short, all the code can be put in the main method without being concerned about the exact syntax of how the main method is defined in this line. Five, the next line is another open curly bracket. This signifies the start of the main method. Six, now come the variable declarations, which are needed to store the weight, height, and BMI. The code gives them the data type “double” since they may have decimal values. Note that because they all have the same type, they can all be declared in one statement, with the variable names separated by commas. Seven, the Scanner object, named keyboard, is just like the example on the previous slide. Eight, the next line is blank to separate the variable declarations from the rest of the code. There can be as many blank lines in our code as necessary—Java does not care. Nine, now the programmer can write code. The first line of output will say, "Welcome to the BMI calculator.” Ten, the, the next output line gives the direction to the user to enter their weight in kilograms. Without this line, the user would not know what to do. Eleven, next, the nextDouble method in the keyboard object will read in the value the user entered and then assign it to the variable weight. Twelve, the program repeats the process for the height and outputs a prompt to the user to enter the height in meters. Thirteen, the nextDouble method in the keyboard object is used to read in the value that the user entered and then assign it to the variable height. Fourteen, now the program is ready to calculate the BMI, assigning the variable “bmi” to the value “weight divided by height times height.” Fifteen, then the program outputs the label "BMI is" without a line return. Sixteen, finally, the program outputs the value of the bmi variable. Seventeen, the next line is a close curly bracket which ends the main method. Eighteen, the final line is a close curly bracket which ends the CalcBMI class definition. And that's all the code!
  19. Here is a sample output from running the program. The statement "Welcome to the BMI calculator" is output. Then, the screen prompts the user to enter their weight in kilograms. In this case, the user enters 68 - all values shown in purple are user-entered values. Next, it prompts the user to enter the height in meters. The user enters 1.72. Finally, after calculating the BMI, the program outputs it. In this case, it is 22.985…and so on. Some more formatting would be necessary to output the decimal value with only 2 or 3 decimal places, but that is not necessary for this example. At this point, feel free to go back to the previous slides to review the code again, comparing it with what the computer outputs on the screen. It is not necessary to understand every little syntax detail, but it is valuable to understand the basics of variables, data types, declarations, assignment statements, and input and output statements.
  20. This concludes lecture c of Computer Programming. To summarize, there are constructs common for most programming languages. They provide ways to store values in variables. Because there are different types of values or data, there are different data types such as integer, character, and boolean, among others. The variables can be assigned values or expressions containing variables and values. Java requires that variables be declared with their data type before they are used. Values can be assigned to variables using the equals sign. Java also provides statements for getting input and output from the user during the execution of the program. One way to do output is to use System.out.print(). Input can be done using an object from the Scanner class.
  21. References slide. No audio.
  22. No audio.