Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Information and Computer Science Computer Programming Lecture e This material (Comp4_Unit5e) was developed by Oregon Health and Science.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Information and Computer Science Computer Programming Lecture b This material (Comp4_Unit5b), was developed by Oregon Health and Science.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 2.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 5.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
CompSci 230 S Programming Techniques
Introduction to Computer Science
Crash course in the Java Programming Language
Introduction to Computer Science
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to Computer Science
2.5 Another Java Application: Adding Integers
Building Java Programs
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Java Basics.
Building Java Programs
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
Java Programming First Program and Variables
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Building Java Programs
Presentation 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 IU24OC

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 c

Programming Constructs Declarations (variable/constant) Assignment Statements Expressions Input and Output (I/O) Statements Control Structures Data Structures Modules –Procedures –Methods –Objects 3 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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) 4 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 5 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 6 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 7 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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" 8 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

Values and Expressions Values can be literals such as 'f' Values can be expressions such as weight/2 5 + age 3 + 2/5 * 15 n*m 9 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 10 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

Expression Examples bmi = weight / (height * height); age = age + 1; tricky = * 2; What is the value of tricky after the assignment? 11 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

Input and Output All programming languages support data input –Keyboard –Files All programming languages support data output –Screen –Files 12 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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); 13 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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(); 14 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

Exercise Write a Java program that calculates BMI (body mass index) Read in weight (kg) Read in height (m) Calculate BMI Output BMI 15 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 16 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 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. } 17 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

Sample Output Welcome to the BMI calculator Enter weight in kg 68 (green) Enter height in m 1.72 (green) BMI is Note: Values in green are entered by the user 18 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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 19 Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c

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: 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 [updated 2007 Dec 17; cited 21 March 2011]. Available from: 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]. Available from: Health IT Workforce Curriculum Version 3.0/Spring 2012 Introduction to Information and Computer Science Computer Programming Lecture c