***** SWTJC STEM ***** Chapter 2-2 cg 21-22 Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
©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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
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;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
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)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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 Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 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.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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,
A Sample Program #include using namespace std; int main(void) { cout
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 2 Basic Computation
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2 Data Types Richard Gesick.
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must start with letter, underscore (_) or dollar sign ($) Can be of any length Cannot contain “+” or “-” Cannot be a keyword used by Java (see Appendix A of text) Should have meaning (“radius” preferred to “r”) Identifier naming guidelines must be followed... Package Names - A collection of classes First word lower case Additional words lower case, separated by “.” Example geometry.areas

***** SWTJC STEM ***** Chapter 2-2 cg 22 Identifiers - Naming cont. Identifier naming guidelines cont. Class Names - A collection of data and methods First word upper case Additional words upper case Example ShellSort Method Names - A collection of statements Are verbs that represent actions First word lower case Additional words upper case Example linearSearch

***** SWTJC STEM ***** Chapter 2-2 cg 22 Identifiers - Naming cont. Identifier naming guidelines cont. Variables - Used to store data Are nouns that represent items or things First word lower case Additional words upper case, Example circleArea Constants - Used to store data that does not change All characters upper case Additional words separated by underscore _ Example PI, FLOW_FACTOR

***** SWTJC STEM ***** Chapter 2-2 cg 23 Programs with Simple Algorithms Our first programs will implement simple algorithms. Example: Find the area of a circle. Given radius = 2 and PI = Find circleArea = PI * radius * radius We will need to learn how Java handles... Inputting data, variables, and constants Calculations with expressions Outputting to PC Monitor - System.out object

***** SWTJC STEM ***** Chapter 2-2 cg 23 How to Input Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Input Data and Variables

***** SWTJC STEM ***** Chapter 2-2 cg 23 Handling Data Java provides for three primitive data types... 1.Numeric data - Numbers 2.Character data - Text 3.Boolean data - Logical states, true or false Data typing provides increased... Efficiency of memory use Ease of programming Speed of program execution Java handles data as primitive data types.

***** SWTJC STEM ***** Chapter 2-2 cg 23 Primitive Data Types KeywordDescriptionSize/Format (integers) byteByte-length integer8-bit [-128 to 127] shortShort integer16-bit [ to 32767] intInteger 32-bit [approx.  2*10 9 ] longLong integer 64-bit [approx.  9*10 18 ] (real numbers) float Single-precision floating point 32-bit [ approx.  3.4*10 38 ] double Double-precision floating point 64-bit [ approx.  1.7* ] (other types) charA single character16-bit Unicode character boolean A Boolean value (true or false) true or false

***** SWTJC STEM ***** Chapter 2-2 cg 24 Data Types Literals A literal is a primitive data type appearing directly in a program , ‘b’, “Hello World”, false are literals. The type of a literal is as follows: The type of an integer literal that ends with L or l is long; the type of any other integer literal is int; e.g. 10. The type of a floating-point literal that ends with F or f is float; the type of any other floating-point literal is double. (Floating-point literals contain a decimal point; e.g., 2.0) The type of a character literal is char; e.g., ‘p’. The type of a Boolean literal is Boolean; e.g., true.

***** SWTJC STEM ***** Chapter 2-2 cg 24 Data Types Literals Examples LiteralData Type 178int 8864Llong double ffloat Ffloat 26.77e3double ' c 'char trueboolean falseboolean Examples:

***** SWTJC STEM ***** Chapter 2-2 cg 24 Unicode Characters Character data holds a single character value like ‘H’ or ‘5’. Character data is stored as unicode, a 16 bit encoding scheme. Includes ASCII character set plus many other alphabets and symbols - See appendix B of text. “\u0041” represents the letter “A” and is stored in memory as two hexadecimal bytes, “00” & “41”. Examples: \u0041 is equivalent to the character ‘A’. \u0042 is equivalent to the character ‘B’. \u is equivalent to the character ‘B’.

***** SWTJC STEM ***** Chapter 2-2 cg 24 Escapes Sequences \u0022\”Double quote \u0027\’Single quote \u005C\\Backslash \u000D\rCarriage return \u000A\nNew line \u0009\tTab \u0008\bBackspace UnicodeCharacter SequenceDescription Escape sequence is available for convenience. Example: ‘\’’ is the literal for a single quote. You cannot use ‘’’ ! ‘\u0022’ will also work.

***** SWTJC STEM ***** Chapter 2-2 cg 24 Boolean Data and Literals Boolean data holds only one of two values, true or false. Boolean representations are useful to compare two values. Is iValue less than jValue? The answer is either true or false. And, is representable as Boolean. Boolean is also used to represent logical states. Is the intrusion detector switch open? It is either, true - open or false - closed.

***** SWTJC STEM ***** Chapter 2-2 cg 25 Handling Variables Variables are used to store data. Java provides for three basic variable types... 1.Numeric variable - Holds a numeric value. 2.Character variable - Holds a single character value. 3.Boolean variable - Holds a logical value, true or false. Creating a Java variable involves two steps... 1.Declaring the name and data type of the variable 2.Assigning the variable a value Note: Variables must be declared before used.

***** SWTJC STEM ***** Chapter 2-2 cg 25 Creating a Variable - Declaring Step 1 - To declare a variable... Use the declaration statement... datatype varName1, varName2,..., varNamen; The declaration statement consists of two parts... The datatype (byte, int, float, char, etc.). Name(s) for variable(s) separated by commas. Meaningful noun First word lower case Additional words upper case, Note that all Java statements end with a semicolon ;.

***** SWTJC STEM ***** Chapter 2-2 cg 25 Declaring Variables - Examples Examples of declaring numeric variables... int count; where data type is integer and name is count. float bulletSpeed, time, distance; where data type is floating point and multiple variables names are bulletSpeed, time, and distance. char startingLetter; where data type is character and name is startingLetter boolean doorAjar, trunkOpen; where data type is Boolean and names are doorAjar and trunkOpen.

***** SWTJC STEM ***** Chapter 2-2 cg 26 Creating a Variable - Assigning Step 2 - To assign a value to a variable, Use the assignment statement. variable = expression; The assignment statement consists of three parts... The variable whose value is to be assigned. The assignment symbol, =. An expression to be evaluated or a literal value to be assigned to the variable.

***** SWTJC STEM ***** Chapter 2-2 cg 26 Creating a Variable - Examples Examples of assigning values to variables... time = 4.0; assigns the value 4.0 to numeric variable time. distance = time * ; evaluates the expression to and assigns the value to numeric variable distance. firstLetter = ‘F’; assigns the character F to the character variable firstLetter. gateOpen = true; assigns the logical condition true to the Boolean variable gateOpen.

***** SWTJC STEM ***** Chapter 2-2 cg 26 Note About the “=“ Sign Note: The = sign means assign a value, not equals. Variable (on the left) is replaced by evaluated result of the expression (on the right). Consider the code below. count = 4; count = count + 1; After executing, count is 5. It may look like an equation, but it is not!

***** SWTJC STEM ***** Chapter 2-2 cg 26 Multiple Assignments To assign a value to a several variables, use... variable1 = variable2 =... = variablen = expression; Example: iCount = jCount = kCount = 4; is equivalent to... iCount = 4; jCount = 4; kCount = 4;

***** SWTJC STEM ***** Chapter 2-2 cg 26 Combining Declare and Assign To declare and assign a value to a variable at the same time, combine the declaration and assignments statements. datatype variable = expression; variable is... Declared as datatype Assigned the value of expression Examples: int count = 4; integer count is assigned value of 4. double rate = 0.15, maxRate = 2.0 * rate; rate and maxRate are declared as double and assigned values 0.15 and 0.30 (calculated) respectively.

***** SWTJC STEM ***** Chapter 2-2 cg 27 Declaring a Constant To declare and assign a value to a constant, use... final datatype CONSTANT_NAME = VALUE; final means exactly that. Once the constant is assigned a value, it cannot change! Examples: final double PI = ; assigns double constant PI the value final int I_COUNT_MAX = 1000; assigns constant I_COUNT_MAX the value Remember that constant names are all caps with multiple names separated by underscores!

***** SWTJC STEM ***** Chapter 2-2 cg How to Calculate Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data and Variables Calculate Assignment Statement

***** SWTJC STEM ***** Chapter 2-2 cg How to Output Something? Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data & Variables Expressions Output System.out Object

***** SWTJC STEM ***** Chapter 2-2 cg 28 print and println Methods To output something with Java: 1.Invoke the out object of the System class (program). 2.Use println method to print and move to the next line 3.System.out.println(“Hello World”); prints Hello World. By default out outputs to the PC monitor. The println method is the action (or service) the object out performs for us. Method print is similar to println but without next line. The actual Java statement looks like this... System.out.println(parameter); Parameter is the piece of data we want to output. Parameter can be a variable or literal.

***** SWTJC STEM ***** Chapter 2-2 cg 28 print and println Examples Examples: 1.System.out.println(“Hello World”); Outputs the string literal Hello World to the PC monitor. 2.Assume iCount is 10, System.out.println(“iCount = ” + iCount); Outputs the string literal iCount = 10 to the PC monitor. Note: The plus + concatenates (ties together) the character literal and the numeric variable value. 3.System.out.print(“Hello ”); System.out.println(“World”); Outputs identically to example 1.

***** SWTJC STEM ***** Chapter 2-2 cg Putting It All Together Calculate Area of Circle A = PI * r 2 Radius rCircle Area Data & Variables Expressions System.out Object

***** SWTJC STEM ***** Chapter 2-2 cg Example Procedural Program public class CircleArea { public static void main(String[] args) { // Declare variables double radius, circleArea; final double PI = ; // Input data radius = 2.0; // Process data circleArea = PI * radius * radius;

***** SWTJC STEM ***** Chapter 2-2 cg Example Procedural Program // Output result System.out.println("Let’s begin."); System.out.print("A circle with radius = " + radius); System.out.println(" has an area = " + circleArea + "."); }