Download presentation
Presentation is loading. Please wait.
Published byQuentin Welch Modified over 9 years ago
1
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) 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..
2
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) 2 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
3
Control Structures Control structures determine the execution of a program Conditional statements –if –case or switch Repetitive statements – loops –while –for –do while 3 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
4
If Statements in Java If statements have a condition When the condition is true, the body of the if statement executes Example: if (weight < 0) //conditions { //body of if System.out.println("Error!"); } 4 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
5
If Else Statements in Java If statements can include an else clause Else clause executes when condition is false if (weight < 0) { System.out.println("Error!"); } else { System.out.println("No error"); } 5 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
6
Nested If statements If statements can have multiple conditions When number is less than zero " Negative" and "Done" are printed to the screen if (number < 0) { System.out.println("Negative"); } else if (number > 0) { System.out.println("Positive"); } else { System.out.println("Zero"); } System.out.println("Done") 6 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
7
Conditional Expressions Use comparator operators (less than, greater than) = (less than or equal to, greater than or equal to) ==, != (is equal to, is not equal to) Use logical operators to combine comparisons && (AND): Both comparisons must be true || (OR): Either comparison must be true ! (NOT): Condition must be false 7 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
8
Code Example Write an if statement that will output the category for a calculated BMI (body mass index) 8 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d BMICategory < 18.5Underweight 18.5 - 24.9999Normal 25.0 - 29.9999Overweight >= 30Obese 5.1 Table: Example of more complex conditional expressions.
9
if (bmi < 18.5) { System.out.println("Underweight"); } else if ((bmi >= 18.5) && (bmi < 25.0)) { System.out.println("Normal weight"); } else if ((bmi >= 25.0) && (bmi < 30.0)) { System.out.println("Overweight"); } else { System.out.println("Obese"); } 9 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
10
Loops in Java Loops are sections of code that will continue to repeat while a condition is true “While” loop is simplest loop Example count = 5; while (count >= 0) // while condition { System.out.println(count); count = count - 1; //value changes } 10 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
11
While Loop, contd. Output from statement 5 4 3 2 1 0 11 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
12
For Loop “For” loop is another type of loop Used when the number of iterations is known Heading sets loop control variable, compares it, and updates it Example for (i = 0; i < 5; i++) { System.out.println(i); } 12 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
13
For Loop, contd. Output from example 0 1 2 3 4 13 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
14
Exercise Modify BMI program –Output BMI category –Calculate BMI more than once 14 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
15
Program Design 1.Read in weight (kg) 2.Read in height (m) 3.Calculate BMI BMI = weight/(height * height) 4.Output BMI 5.Output BMI category 6.Prompt user if want to calculate another BMI 7.If yes, go back to step 1 8.If no, end 15 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
16
import java.util.*; public class CalcBMI { public static void main(String[] args) { double bmi, weight, height; int anotherBMI; Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to the BMI calculator"); anotherBMI = 1; while (anotherBMI == 1) { System.out.println("Enter weight in kg"); weight = keyboard.nextDouble(); System.out.println("Enter height in m"); height = keyboard.nextDouble(); bmi = weight/(height*height); System.out.print("BMI is "); System.out.println(bmi); 16 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
17
… anotherBMI = 1; while (anotherBMI == 1) { …//input height, weight; calculate BMI if (bmi < 18.5) System.out.println("Underweight"); else if ((bmi >= 18.5) && (bmi < 25.0)) System.out.println("Normal weight"); else if ((bmi >= 25.0) && (bmi < 30.0)) System.out.println("Overweight"); else System.out.println("Obese"); System.out.println("Do you want to calculate another?"); System.out.println("Enter 1 for yes and 0 for no"); anotherBMI = keyboard.nextInt(); } System.out.println("Good Bye!"); } 17 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
18
Sample Output Welcome to the BMI calculator Enter weight in kg 68 (green) Enter height in m 1.27 (green) BMI is 42.16008432016864 Obese Do you want to calculate another? Enter 1 for yes and 0 for no 1 (green) Enter weight in kg 55 (green) Enter height in m 1.5 (green) BMI is 24.444444444444443 Normal weight Do you want to calculate another? Enter 1 for yes and 0 for no 0 (green) Good Bye! 18 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
19
Data Structures Data structures are used for storing multiple pieces of data together Arrays are a simple data structure Example double[] grade = new double[10]; Array of 10 doubles for storing grades grade[1] = 95.0; Other data structures available –Linked lists, trees, hash tables 19 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
20
Modules Way of separating code, usually by function –Allows for reuse –Easier to maintain Procedures, functions, methods are all modules Objects are as well Example public void printAreaCircle(double radius) { double area = 3.14*radius*radius; System.out.println("Area is " + area); } 20 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
21
Computer Programming Summary – Lecture d Control structures affect the order of execution “If” statements allow for conditional execution Loops allow for repeated execution Data structures allow for data to be grouped together Modules are used to encapsulate code 21 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
22
Computer Programming References – Lecture d 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/http://math.hws.edu/javanotes/ Morley Deborah, Parker Charles S. (2010). Chapter 13: Program Development and Programming Languages. In: Understanding Computers Today and Tomorrow.12 th 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.htmlhttp://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.1 Table: Example of more complex conditional expressions. 22 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture d
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.