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.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Chapter 2: Java Fundamentals Input and Output statements.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
String Escape Sequences
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 part #4 Operator
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Elementary Programming
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
CS 201 Lecture 2: Elementary Programming Tarik Booker CS 201 California State University, Los Angeles.
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.
Fundamentals 2.
CompSci 230 S Programming Techniques
Chapter # 2 Part 2 Programs And data
Elementary Programming
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
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Presentation transcript:

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 the computer's memory: space memory. A space memory has three characteristics Identifier Data Type State 2

Identifier is a sequence of characters that denotes the name of the space memory to be used. This name is unique within a program. Identifier Rules It cannot begin with a digit (0 – 9). It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed. Identifiers in Java are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler. 3

State My be changed  variable All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word. Cannot be changed  constant All uppercase, separating words within a multiword identifier with the underscore symbol, _. 4

Data Type The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program. 5

Java built-in Data Types 6

Primitive Data Types 7 Type Size (bits) RangeDescription booleantrue, falseStores a value that is either true or false. char160 to 65535Stores a single 16-bit Unicode character. byte8-128 to +127Stores an integer. short to Stores an integer. int32 bits-2,147,483,648 to +2,147,483,647 Stores an integer. long64 bits-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Stores an integer. float32 bitsaccurate to 8 significant digitsStores a single-precision floating point number. double64 bitsaccurate to 16 significant digitsStores a double-precision floating point number.

Variable/Constant Declaration When the declaration is made, memory space is allocated to store the values of the declared variable or constant. The declaration of a variable means allocating a space memory which state (value) may change. The declaration of a constant means allocating a space memory which state (value) cannot change. 8

Constant Declaration final dataType constIdentifier = literal | expression; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; 9 These are called literals. This is called expression.

Variable Declaration A variable may be declared: – With initial value. – Without initial value. Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3; Variable declaration without initial value; dataType variableIdentifier; double avg; int i; 10

More declaration examples String declaration – with initial value: String word="Apple"; – without initial value: String word; char declaration – with initial value: char symbol ='*'; – without initial value: char symbol; 11 Boolean declaration: – with initial value: boolean flag=false; boolean valid=true; – without initial value: boolean flag;

Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable of type string with initial value equals to the value of constant in B. D-Is the following names are valid, why? – Student name – 1course – course*name 12

Operators Operators are special symbols used for: – mathematical functions – assignment statements – logical comparisons Examples of operators: – // uses + operator – – 4 * (5 – 3) // uses +, -, * operators Expressions: can be combinations of variables and operators that result in a value Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 13

Groups of Operators There are 5 different groups of operators: – Arithmetic Operators – Assignment Operator – Increment / Decrement Operators – Relational Operators – Logical Operators Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 14

Java Arithmetic Operators Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 15 Addition+ Subtraction– Multiplication  Division/ Remainder (modulus )% Assignment Operator =

Arithmetic Operators The following table summarizes the arithmetic operators available in Java. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 16 This is an integer division where the fractional part is truncated.

Example Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 17 Example of division issues : 10 / 3 gives / 3 gives As we can see, if we divide two integers we get an integer result. if one or both operands is a floating-point value we get a floating-point result.

Modulus Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 18  Generates the remainder when you divide two integer values. 5%3 gives 25%4 gives 1 5%5 gives 05%10 gives 5  Modulus operator is most commonly used with integer operands. If we attempt to use the modulus operator on floating-point values we will garbage!

Example: Sum of two integer Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 19 public class Sum { // main method public static void main( String args[] ){ int a, b, sum; a = 20; b = 10; sum = a + b; System.out.println(a + ” + ” + b + “ = “ + sum); } // end main } // end class Sum

Arithmetic/Assignment Operators Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 20 Java allows combining arithmetic and assignment operators into a single operator: Addition/assignment+= Subtraction/assignment  = Multiplication/assignment  = Division/assignment/= Remainder/assignment %=

Increment/Decrement Operators Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 21 Only use ++ or   when a variable is being incremented/decremented as a statement by itself. x++; is equivalent to x = x+1; x--; is equivalent to x = x-1;

Relational Operators Relational operators compare two values They Produce a boolean value (true or false) depending on the relationship OperationIs true when a >ba is greater than b a >=ba is greater than or equal to b a ==ba is equal to b a !=ba is not equal to b a <=ba is less than or equal to b a <ba is less than b Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 22

Example int x = 3; int y = 5; boolean result; result = (x > y); now result is assigned the value false because 3 is not greater than 5 Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 23

Logical Operators Symbol Name && AND || OR ! NOT || TF TTT FTF && TF TTF FFF Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 24

Example boolean x = true; boolean y = false; boolean result; result = (x && y); result is assigned the value false result = ((x || y) && x); (x || y) evaluates to true (true && x) evaluates to true result is then assigned the value true Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 25

Operators Precedence Parentheses(), inside-out Increment/decrement++, --, from left to right Multiplicative*, /, %, from left to right Additive+, -, from left to right Relational, =, from left to right Equality==, !=, from left to right Logical AND&& Logical OR|| Assignment=, +=, -=, *=, /=, %= Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 26

Standard Output Window Using System.out, we can output multiple lines of text to the standard output window. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 27 The exact style of standard output window depends on the Java tool you use.

The println Method We use println instead of print to skip a line. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 28 int x = 123, y = x + x; System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); x = 123 x + x = 246 THE END

Standard Input To input primitive data values, we use the Scanner class. 4 steps are needed to be able to use input primitive: – Step 1: import the Scanner class: import Java.util.Scanner; – Step 2 : declaring a reference variable of a Scanner Scanner read ; //we named the object read – Step 3: creating an instance of the Scanner read = new Scanner (System.in); – Step 4: use specific methods to enter data int x = read.nextInt(); Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 29

Example Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 30 import java.util.Scanner; public class TestInput { public static void main(String[] args) { Scanner input ; int area,length, width; input = new Scanner (System.in); // creating an instance System.out.println("enter the length "); length = input.nextInt(); //reading the length from the keyboard System.out.println("Enter the Width "); width = input.nextInt(); //reading the width from the keyboard area = length * width ; System.out.println("the length is "+ length); System.out.println("the width is "+ width); System.out.println("the area is "+ area); }

Output enter the length 2 Enter the Width 3 the length is 2 the width is 3 the area is 6 Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 31

Common Scanner Methods MethodExample Scanner input = new Scanner (System.in); nextByte( )byte b = input.nextByte( ); nextDouble( )double d = input.nextDouble( ); nextFloat( )float f = input.nextFloat( ); nextInt( )int i = input.nextInt( ); nextLong( )long l = input.nextLong( ); nextShort( )short s = input.nextShort( ); next() String str = input.next(); Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 32