1 / 26

lab2 PROGRAMMING 1

lab2 PROGRAMMING 1. elementary programming . BEFORE WE START. 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.

lavonn
Télécharger la présentation

lab2 PROGRAMMING 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. lab2PROGRAMMING 1 elementary programming

  2. BEFORE WE START Ins.Ebtesam AL-Etowi

  3. 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

  4. 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

  5. Statement & Statement Terminator Anatomy of a Java Program 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

  6. Modifiers & Blocks 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 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

  7. Special Symbols Ins.Ebtesam AL-etowi

  8. 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

  9. Java Primitives Data Types Ins.Ebtesam AL-etowi

  10. Character and String String: 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 stringString a=new String(); a="abcdef";c) By simply creating a reference variableString a="abcdef“; Character : Character is used to represent a single character. Char c='A‘; Char C=97; Ins.Ebtesam AL-etowi

  11. What is a variable? Java Identifiers(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

  12. Declaring a variable(identifier) The syntax for declaring a variable is: Datatypevariable 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: finaldatatypeCONSTANTNAME = VALUE; Example: final double PI = 3.14159; Ins.Ebtesam AL-etowi

  13. Example of identifiers(variables) Ins.Ebtesam AL-etowi

  14. 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

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

  16. 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

  17. Arithmetic Operators Ins.Ebtesam AL-etowi

  18. Shortcut Operators Ins.Ebtesam AL-etowi

  19. 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

  20. Operations Priority 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

  21. 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

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

  23. 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

  24. 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

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

  26. Thank You !

More Related