Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.

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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Hello, world! Dissect HelloWorld.java Compile it Run it.
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;
Basic Elements of C++ Chapter 2.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
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?
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Introduction to Information and Computer Science Computer Programming Lecture b This material (Comp4_Unit5b), was developed by Oregon Health and Science.
Chapter 2: Java Fundamentals
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.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CompSci 230 S Programming Techniques
Chapter Topics The Basics of a C++ Program Data Types
Crash course in the Java Programming Language
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to Computer Science
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
INPUT STATEMENTS GC 201.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 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
Java so far Week 7.
Fundamentals 2.
Java Basics.
Java Programming Review 1
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
Primitive Types and Expressions
Presentation transcript:

Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3 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 IU24OC

Unit 5 Objectives a)Define the purpose of programming languages. b)Define the different types of programming languages. c)Explain the continuum of programming languages from machine code and assembly languages through scripting languages and high level structured programming languages. d)Explain the compiling and interpreting process for computer programs. e)Use the following components of programming languages to build a simple program: variables, loops and conditional statements. f)Introduce additional programming concepts such as objects and modularity. Component 4/Unit 5-32 Health IT Workforce Curriculum Version 2.0/Spring 2011

Programming Writing a program is called programming Programming languages have common constructs We will be using Java for programming examples Component 4/Unit 5-33 Health IT Workforce Curriculum Version 2.0/Spring 2011

Programming Constructs Declarations (variable/constant) Assignment Statements Expressions Input and Output (I/O) Statements Control Structures Data Structures Modules –Procedures –Methods –Objects Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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) Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

Assignment Statements An assignment statement is used to assign a value to a variable. age = 42; The “equal sign” is the assignment operator. We can say, –“The variable age is assigned the value of 42” – “ age is assigned 42” –" age gets 42" Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

Expression Examples bmi = weight / (height * height); age = age + 1; tricky = * 2; What is the value of tricky after the assignment? Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

Input and Output All programming languages support data input –Keyboard –Files All programming languages support data output –Screen –Files Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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); Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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(); Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

Exercise Write a Java program that calculates BMI Read in weight (kg) Read in height (m) Calculate BMI Output BMI Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

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 Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

import java.util.*; //import package for keyboard input public class CalcBMI //Start of class and program { public static void main(String[] args) //main { double bmi, weight, height; //variables Scanner keyboard = new Scanner(System.in); //input System.out.println("Welcome to the BMI calculator"); 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); } Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring

Sample Output Welcome to the BMI calculator Enter weight in kg 68 Enter height in m 1.72 BMI is Component 4/Unit 5-3 Health IT Workforce Curriculum Version 2.0/Spring Note: values in green are input by the user