Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
CSci 1130 Intro to Programming in Java
Chapter Three Arithmetic Expressions and Assignment Statements
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
COMP 14 Introduction to Programming
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
String Escape Sequences
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Primitive Types, Strings, and Console I/O Chapter 2.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Primitive Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Examples of Primitive Values
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Introduction to Java, and DrJava
Expressions and Assignment Statements
Expressions and Assignment
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Presentation transcript:

Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming

SE15: Types Expressions and Simple I/O5–25–2 Todays Learning Objectives To become familiar with the Java data types Learn about the assignment statement and expressions Learn about simple keyboard input and screen output

SE15: Types Expressions and Simple I/O5–35–3 Lecture Outline Identifiers Primitive Types Expressions Keyboard and Screen I/O

SE15: Types Expressions and Simple I/O5–45–4 Variables Variables are used to store data such as numbers or letters The computer needs to know: the name of the variable the type of data to be stored in the variable Declaring a variable gives this information int numberOfDeposits; char answer; double interestRate; Savitch pp47-55

SE15: Types Expressions and Simple I/O5–55–5 Rules for naming Identifiers Names in a program are called identifiers Java is case sensitive!! accountNumber, AccountNumber and accountnumber are all different Naming Conventions Use only letters and digits Punctuate multiword names using upper case letters numberOfDeposits How about the following? players-score, Faces.com, 4cast Names of classes start with an uppercase letter Names of variables, objects and methods start with a lowercase letter

SE15: Types Expressions and Simple I/O5–65–6 Variable names The names should suggest the variables use or indicate the kind of data they will hold distance - distance between two objects count - for counting something You should avoid using single letters x = y + z ???? Some words are reserved for keywords void for while

SE15: Types Expressions and Simple I/O5–75–7 Primitive Types Type nameKind of valueMemory usedRange byte integer1 byte-128 to 127 short integer2 bytes to int integer4 bytes long integer8 bytes float floating-point number 4 bytes double floating-point number 8 bytes char single character2 bytes boolean true or false1 bit

SE15: Types Expressions and Simple I/O5–85–8 Assignment Statements Examples: amount = 3.99; firstInitial = B; score = numberOfQueens*11+numberOfKings*12; count = count + 10; amount += 4; You cant put a square peg into a round hole int cost = 14.99; char initial = 2.6; int fraction = 4/6; int cost = cost + 0.1;

SE15: Types Expressions and Simple I/O5–95–9 Arithmetic Operators & Expressions You can form arithmetic expressions involving addition (+), subtraction (-), multiplication (*) and division (/) as you would in ordinary arithmetic or algebra. newBalance = oldBalance + deposit; Parentheses can be used to group items together newBalance = oldBalance – (oldBalance*rate); Using parentheses can help tell which operations will be performed first (cost + tax) * discount; cost + (tax * discount); Savitch pp 66-76

SE15: Types Expressions and Simple I/O5–10 Another Operator % operator When you divide one integer by another you get a result and a remainder, ie. 20 divided by 7 gives 2 with a remainder of 6 The % operator gives you the remainder 20%7 evaluates to 6 How can I test is an integer is even or odd ? How would I print out every 4 th integer in a series?

SE15: Types Expressions and Simple I/O5–11 Precedence Rules How do you evaluate Total = cost + tax * discount; ? Is it Total = (cost + tax) * discount; or Total = cost + (tax * discount); ? Operators are evaluated in order of precedence First: unary operators: +, -, ++, --, and ! Second: binary operators: *, /, and % Third:binary operators: + and -

SE15: Types Expressions and Simple I/O5–12 Arithmetic Expressions Ordinary expression Java expression rate*rate + delta 2*(salary+bonus) (a-7)/(t-9*v)

SE15: Types Expressions and Simple I/O5–13 Quick Quiz Which of the following are legal statements 1) int x = 36.7; 2) Boolean b = x; 3) int k = 15; 4) int t = k; 5) t = t + 5; 6) double e = 2.5 * t; 7) e = e * / 2; 8) double f = e – 5.0; If all the legal statements were part of the same method, what would the value of f be after the last valid statement?

SE15: Types Expressions and Simple I/O5–14 Simple Screen Output You have already used System.out System.out.println(The sum of those two numbers is + sum); System.out is an object println is a method + is a kind of and Savitch pp 56,57

SE15: Types Expressions and Simple I/O5–15 Simple input import java.util.Scanner; Tells the compiler where to find the definition of Scanner Scanner input = new Scanner(System.in); Sets things up so data can be entered from the keyboard int n1 = input.nextInt(); This is an assignment statement which gives a value to the variable n1 input.nextInt reads one int from the keyboard

SE15: Types Expressions and Simple I/O5–16 Constants The values of variables can change (hence the name) Literals like 2 or 3.7 are called constants G is a constant of type char Naming constants can be useful for improving clarity circumference = 2.0 * * radius; Can be more clearly written as circumference = 2.0 * PI * radius; How do we name as PI? public static final double PI = ; Savitch pp 58-59

SE15: Types Expressions and Simple I/O5–17 Type Casting Involves changing the type of a value to another type Changing 2.0 from a double to an int. double speed; speed = 10.0; int revolutionsPerSecond; revolutionsPerSecond = speed; revolutionsPerSecond = (int)speed; Savitch pp 61-63

SE15: Types Expressions and Simple I/O5–18 Summary Looked at: what variables & Identifiers are and naming conventions used in Java Primitive types & writing arithmetic expressions in Java Simple input and output

SE15: Types Expressions and Simple I/O5–19 Follow-up Work Read Chapter 2 of Savitch Complete the ranges for each of the primitive types Find out why numbers such as 3.7 are called floating point numbers. Write out the following as a Java expression