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 !