elementary programming

Slides:



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

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Basic Elements of C++ Chapter 2.
Chapter 2 Programming Building Blocks — Java Basics.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2 Elementary Programming
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
Copyright Curt Hill Variables What are they? Why do we need them?
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
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.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Identifiers - symbolic names
Unit 2 Elementary Programming
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 3: Operators, Expressions and Type Conversion
Basic Elements of C++.
Multiple variables can be created in one declaration
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
IDENTIFIERS CSC 111.
Starting JavaProgramming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Introduction to C Programming
Chapter 2 Variables.
Expressions and Assignment
Anatomy of a Java Program
Programming Building Blocks Java Basics
Chapter 2: Introduction to C++.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Introduction to C Programming
Presentation transcript:

elementary programming lab2 PROGRAMMING 1 elementary programming

BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

content Anatomy of a Java Program. Special Symbols . Java Basic Syntax Java Data Types Java Identifiers Operator In Java To cast the value of one type to another type. Output with Character Escape . Input. Ins.Ebtesam AL-etowi

Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comments Methods Modifiers Blocks Class Name & Main method In order to run a class, the class must contain a method named main. The program is executed from the main method. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. Ins.Ebtesam AL-etowi

Statement & Statement Terminator Anatomy of a Java Program Statement & Statement Terminator A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program is a statement to display the greeting "Welcome to Java!“. Every statement in Java ends with a semicolon (;). Reserved words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Ins.Ebtesam AL-etowi

Anatomy of a Java Program Comments In Java, the comments used to describe the Java code. They are preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */. Modifiers & Blocks Modifiers specify which parts of the program may see and use any particular class/method/field. A modifier is a Java reserved word that specifies particular characteristics of a programming construct. Java has three visibility modifiers: public, private, and protected. A pair of braces in a program forms a block that groups components of a program. Ins.Ebtesam AL-etowi

Special Symbols Ins.Ebtesam AL-etowi

Java Basic Syntax *Case Sensitivity - Java is case sensitive which means identifier Hello and hello would have different meaning in Java. *Class Names - should be in Upper Case. *Method Names - should start with a Lower Case letter. *Program File Name - should exactly match the class name. *public static void main(String args[ ]) - java program processing starts from the main() method which is a mandatory part of every java program. Ins.Ebtesam AL-etowi

Java Primitives Data Types Ins.Ebtesam AL-etowi

Character and String String: Character : String is used to represent more than a single character. There are 3 ways to define string: a) By Creating a String Object String s=new String("abcdef"); b) By just creating object and then referring to string String a=new String(); a="abcdef"; c) By simply creating a reference variable String a="abcdef“; Character : Character is used to represent a single character. Char c='A‘; Char C=97; Ins.Ebtesam AL-etowi

Java Identifiers(variable) What is a variable? variables are used to store values to be used later in a program. Why ? They are called variables because their values can be changed. Java provides the programmer with different types of data but basic data types are most required: integer (int), real (float) ,double and character (char). Ins.Ebtesam AL-etowi

Declaring a variable(identifier) The syntax for declaring a variable is: Datatype variable name; An identifier is a sequence of characters that consists of letters, digits, underscores(_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit An identifier cannot be a reserved word. Ex: true, false, or null. An identifier can be of any length. Here are some examples of variable declarations: Int count; float radius; double Rate; Constant: the syntax for declaring a constant: final datatype CONSTANTNAME = VALUE; Example: final double PI = 3.14159; Ins.Ebtesam AL-etowi

Example of identifiers(variables) Valid identifier Invalid Identifier Name Error Letter1 1Letter start with a digite integer int A key word Mark94 Mark 94 Contain a space maining main Ins.Ebtesam AL-etowi

Assigning Values to Your Variables In Java, the equal sign (=) is used as the assignment operator. The syntax for assignment statements is as follows: 1- variable = value; 2- variable = expression; Example: int Width; Width = 5; You can combine these steps and initialize Width when you define it by writing: int Width = 5; If the right part is an expression int I=9; int J=13; int z=I+J; Note: In an assignment statement, the data type of the variable on the left must be compatible with the data type of the value on the right. For example, int x = 1.0 would be illegal, because the data type of x is int. Ins.Ebtesam AL-etowi

Kinds of Operator Arithmetic Op. : + - * / % Relational Op. : > >= < <= == != Logical Op. : && || ! Inc/Dec Op. : ++ -- Bit Op. : & | ^ ~ << >> >>> Operators of Java Conditional Op. : ?: Assign Op. : = += -= *= /= %= &= ^= |= >>= <<= >>>= Casting Op. : (Data Type) Array Op. : [] Method Op. : () . instanceof Op. : instanceof Ins.Ebtesam AL-etowi

Casting Operator Widening a type can be performed automatically without explicit casting Narrowing a type must be performed explicitly. Example int j=1; double b =j; (automatically) j=(int)b; (explicit ) Assignment Operator Syntax: variable or(constant) = expression; or value In variable declare. Ins.Ebtesam AL-etowi

Arithmetic Operators Result Example Meaning Name 35 34+1 Addition + 33 34-1 Subtraction - 9000 30*300 Multiplication * 0.5 1/2 Division / 2 20%3 Remainder % Ins.Ebtesam AL-etowi

Shortcut Operators Operator Example Equivalent += a += 3; a = a + 3; -= a -= 10; a = a - 10; *= a *= 4; a = a * 4; /= a /= 7; a = a / 7; %= a %= 10; a = a % 10; Ins.Ebtesam AL-etowi

Increment and Decrement Operators ++ increments the value of its operand by 1. -- decrements the value of its operand by 1. Syntax: Pre-increment: ++variable Post-increment: variable++ Pre-decrement: --variable Post-decrement: variable-- OUTPUT Ins.Ebtesam AL-etowi

Operations Priority 2 3 1 5 4 Operator Order of evaluation Operation ( ) left - right parenthesis for explicit grouping ++ -- right - left preincrement, predecrement postincrement, postdecrement * / % multiplication, division, modulus + - addition or String concatenation, subtraction = += -= *= /= %= assignment For the following expression, in what order are the operators evaluated in Java? ++a / (b + c) - d % e 2 3 1 5 4 Ins.Ebtesam AL-etowi

Output: Writing To Console Character Escape Sequence Name \b Backspace \t Tab \n Linefeed \f Formfeed \r Carriage Return \\ Backslash \' Single Quote \" Double Quot Output: Writing To Console Ins.Ebtesam AL-etowi

Input: Reading from Console Example: Ins.Ebtesam AL-etowi

Exercise 1 Write a program that converts a Fahrenheit degree to Celsius using the formula?? In Java Celsius = (5.0/9.0) * (Fahrenheit – 32); 1 2 3 OUTPUT Ins.Ebtesam AL-etowi

Exercise 2 Write a program that converts a Celsius degree to Fahrenheit using appropriate input function to read the degree from the user.? Ins.Ebtesam AL-etowi

Home Work Write a program that computes the average of 3 students grades which are entered by the user?. Ins.Ebtesam AL-etowi

Thank You !