1 CSE1020: Introduction to Computer Science I. 2 Today’s Lecture Course administrative details Material from Chapter 1 “Introduction to Programming” 19:00-22:00.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
©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.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
String Escape Sequences
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 2. Program Construction in Java. 01 Java basics.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Java Variables and Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

1 CSE1020: Introduction to Computer Science I

2 Today’s Lecture Course administrative details Material from Chapter 1 “Introduction to Programming” 19:00-22:00 = 180 minutes Tentative schedule Class = minutes + break minutes

3 About this course… About your instructor Course web site: –Review of course components

4 About the text book… Must use 2006 edition, NOT 2004 edition

5 How much time per week? HoursActivity 3Prepare for lecture (read textbook) 3Attend lecture 1Post-lecture review (review notes) 2Prepare for lab (do self-paced labs) 1Attend lab 1-4Do eCheck(s) 1-3Do chapter exercises Total: hours per week (not including test preparation)

6 Chapter 1 Introduction to Programming

7 1.1 Anatomy of a Program 1.1.1A Quick Tour 1.1.2Language Elements 1.1.3Program Execution 1.2 The Declaration Statement 1.2.1Variable Names 1.2.2The Integer Types 1.2.3Declaration and Memory 1.2.4Other Data Types 1.2.5Primitive and Non-Primitive Types 1.3 The Assignment Statement 1.3.1The int Arithmetic Operators 1.3.2Other Arithmetic Operators 1.3.3Mixed Types and Casting Outline

8 Let us take a look at a Java program. It does not matter now how the program was written; just become familiar with the terminology for describing program structures. Note, in particular, the following four terms: A Quick Tour Imports, Class, Method, Style

9 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); }

10 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Imports

11 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Imported Class = Delegation

12 import java.lang.System; Subpackage ClassPackage java lang util...

13 import java.lang.System; import java.lang.*; versus The package naming convention calls for lowercase letters. - Note the difference: - And as a matter of style:

14 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Class Header Class Body, a Block

15 import java.lang.*; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Method Header Method Body, a Block

16 Class naming convention Use title case unless an acronym, e.g. Math, UTL, StringTokenizer. Style Block layout Braces must align vertically and the all statements must be left justified and indented by one tab position. Method naming convention Use lowercase letters but for multi-word names, capitalize the first letter of each subsequent word, e.g. main, equals, toString, isLeapYear

17 Still without worrying about semantics, let us identify the elements of a program: Language Elements Keywords Identifiers Literals Operators Separators

18

19 Keywords

20 Identify the language elements in the following program... Keywords, Identifiers, Literals, Operators, Separators Example

21 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Keywords, Identifiers, Literals, Operators, Separators

22 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Keywords, Identifiers, Literals, Operators, Separators

23 import java.lang.System; public class Area { public static void main( String[] args ) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } Keywords, Identifiers, Literals, Operators, Separators

Program Execution

The Declaration Statement type name; The name of a primitive or non- primitive type, e.g. int, double… An identifier to be associated with a memory block A separator The scope of the variable = the enclosing block of the declaration. The variable is not known outside its scope.

26 Rules and guidelines for the name: Must be an identifier Must not be in the scope of another variable with the same name A good name is indicative of the content that will be stored in the variable As a matter of style, use lowercase letters, but for multi-word names, capitalize the first letter of each subsequent word Variable Names

27 A type is a range of values and a set of operations on these values. The range of the int type consists of all whole numbers between -2 and +2 billions (approx). int supports the four arithmetic operations plus the remainder. The long type is very similar to int except its range is much bigger, +/ An integer literal has an int type unless suffixed by L (l), in which case it is long The Integer Types

28 Logical versus Physical Address versus Content Bytes, KB, MB, GB, TB, … The Memory Block byte block at address 24 1-byte block at address 25 2-byte block at address 26 4-byte block at address Declaration and Memory

What happens when we write: int width; width Note that no initialization is involved; only an association of a name with an address. 1.A block big enough to hold an int is allocated, e.g. a 4B block at 24 2.Its address is associated with the variable name, e.g. 24 with width

30 Real Integer Use for integer data, e.g. count. 100% exact Use for real data, e.g. amount. Inherently inaccurate Other Data Types

31 Numeric Types Real Integer float double 4 8 ±10 38 SD=7 ± SD=15 int long 4 8 ±2G exact ±2E exact Integer literals are int by default unless suffixed with L Real literals are recognized thru a decimal point or an exponent. They are double by default unless suffixed with F

32 Stores the result on a condition Has only two possible values true and false are reserved words Boolean variables are not integers Note: Boolean literals are the easiest to recognize! The Type boolean

33 The Character Type A letter, digit, or symbol Digits versus Numbers Store the code, not the typeface The case of English: ASCII char is thus an (unsigned) integer type Unicode has 64K codes char Character literals are recognized by single quotes surrounding one character, e.g. 'A'

34 More on Characters

35 Non-Primitive Primitive number character boolean class interface array Primitive & Non-Primitive

36 Java’s Primitive Type N/A

37 name = value; - Pre-declared and in-scope - Its type can hold RHS - Its content will be overwritten - a Literal - a Name, or - an Expression A separator An operator 1.3 The Assignment Statement

The int Arithmetic Operators

39 Examples double price; price = 17.25; int quantity = 25; boolean isValid = false; double cost; cost = price; double extended; extended = quantity * price; RHS is a variable Can combine declaration with assignment. RHS is an expression

40 Example 5 + (4 - 3) / * 3 % 4

41 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4

42 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4

43 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4

44 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4

45 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4

46 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4

47 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4 =

48 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4 =

49 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4 = = 5 - 2

50 Example 5 + (4 - 3) / * 3 % 4 = / * 3 % 4 = * 3 % 4 = % 4 = = = 3

51 The int operators satisfy closure thru circular wrapping The / int operator always rounds toward 0 and leads to an exception if the divisor is zero The sign of % is the same as that of the dividend The real operators satisfy closure by adding Infinity and NaN. Hence, dividing by zero does not lead to exceptions (a * b) / c is not the same as a * (b / c) for any type (a + b) – c is not the same as a + (b – c) for real types Other Arithmetic Operators Each of long, float, and double come with 11 operators with the same symbols as int; i.e. the symbols are overloaded. Note:

52 Promotion (aka widening conversion) is done automatically when needed May lead to loss of precision but the order of magnitude is preserved Demotion is not done automatically. Can be done manually thru a cast Casting is risky…avoid it Mixed Types and Casting

53

54 Note: The cast operator has a precedence that is higher than * but less than ++ The = operator has the lowest precedence of all operators There are shorthand operators to combine assignment with an operator: x op= y is shorthand for x = x op y Ex: x +=1 is like x = x + 1 or x++

55 Example int iVar = 15; long lVar = 2; float fVar = 7.6f - iVar / lVar; double dVar = 1L / lVar + fVar / lVar; int result = 100 * dVar; Fix, if need be, and output result The answer may surprise you!