Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1.

Similar presentations


Presentation on theme: "Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1."— Presentation transcript:

1 Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1

2 Objectives: Part (1): Programs. The relationship between Java and the World Wide. To know Java’s advantages. To write a simple Java program. To create, compile, and run Java programs. To understand the Java runtime environment. To know the basic syntax of a Java program. To display output on the console and on the dialog box. 10/8/2015Lecture 1 2

3 :Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. 10/8/2015Lecture 1 3

4 Programming Languages: Machine Language Assembly Language High-Level Language 10/8/2015Lecture 1 4 Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010

5 Programming Languages: Machine Language Assembly Language High-Level Language 10/8/2015Lecture 1 5 The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415;

6 Programming Languages: Machine Language Assembly Language High-Level Language 10/8/2015Lecture 1 6 Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3

7 Popular High-Level Languages: COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) C++ (an object-oriented language, based on C) Java (We use it in this Semester ) 10/8/2015Lecture 1 7

8 Compiling Source Code: A program written in a high-level language is called a source program. Since a computer cannot understand a source program. Program called a compiler is used to translate the source program into a machine language program called an object program. The object program is often then linked with other supporting library code before the object can be executed on the machine. 10/8/2015Lecture 1 8

9 Example for compiling source code in C++: 10/8/2015Lecture 1 9

10 Compiling Java Source Code: You can port a source program to any machine with appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown in Figure 1.5. Java Virtual Machine is a software that interprets Java bytecode. 10/8/2015Lecture 1 10

11 10/8/2015Lecture 1 11 Example for compiling source code in Java:

12 10/8/2015Lecture 1 12 Example for compiling source code in Java:

13 10/8/2015Lecture 1 13 Object Oriented Programming(OOP ): Is a new method used in the programming as it works to analyze and design applications in the form of objects.. And contain data controlled by a set of processes. Example/ Java language. Structured programming : Is a technique that based on building the structure of the data without paying attention to the processes applied to the data. Example/ C++ language.

14 :Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Java Is Inheritances Java Is Encapsulation 10/8/2015Lecture 1 14

15 :Java IDE Tools Borland JBuilder NetBeans Open Source by Sun Sun ONE Studio by Sun MicroSystems Eclipse Open Source by IBM 10/8/2015Lecture 1 15

16 :A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } 10/8/2015Lecture 1 16

17 Creating, Compiling, and Running Programs: 10/8/2015Lecture 1 17

18 Trace a Program Execution: 10/8/2015Lecture 1 18 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Enter main method

19 :Trace a Program Execution 10/8/2015Lecture 1 19 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Execute statement

20 Trace a Program Execution: 10/8/2015Lecture 1 20 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } print a message to the console

21 : content of a Java Program Comments Statements Blocks Classes Methods The main method 10/8/2015Lecture 1 21

22 :Comments In Java, comments 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 */. 10/8/2015Lecture 1 22 A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). Statements:

23 Blocks: 10/8/2015Lecture 1 23 A pair of braces in a program forms a block that groups components of a program.

24 Classes: Is the most important term in the object-oriented programming (OOP).. It can be defined as a template or outline, which made ​​ him the objects.. The classification determines what data and methods that will be included in the object.. And classification is the basic encapsulation unit in the Java language. we can using one or more classes. 10/8/2015Lecture 1 24

25 10/8/2015Lecture 1 25 Object Is the case for the class.. Because it represents the real situation with the specifications contained in class.. This means that if the class of several objects is equal for each object, including its data separate. Object:

26 Methods: Contains the code Executive for the class.. It is defined as Determines the behavior of objects.. The methods can be defined as the code that controls the data. It is a term of actual work in Java and corresponding to a work function in C++. 10/8/2015Lecture 1 26

27 main Method: The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; } 10/8/2015Lecture 1 27

28 10/8/2015Lecture 1 28

29 10/8/2015lecture 2 29 Objectives: To understand objects and classes and use classes to model objects Part (2): To understand Basic structure of the Java program. To use identifiers to name variables, constants, methods, and classes. To use variables to store data. To program with assignment statements and assignment expressions. To use constants to store permanent data. To declare Java primitive data types: byte, short, int, long, float, double, and char. To use Java operators to write expressions. To understand output statement. To write a simple Java program.

30 Fundamentals of programming structures in java: 10/8/2015lecture 2 30 When you write a program in Java it is design or create a group of Classes, then objects are created, which takes the form of Classes.. The Java language includes a set of ready-made Classes, called ( Class Library ), and by which Classes can be created easily. Class Library: It is a set of ready-made Classes and can be used in various programs in Java.

31 Basic structure of the Java :program 10/8/2015lecture 2 31 To deal with the classes in the Java language, it must know two important things:  Declaring the Class.  Body of Class. Classes in java:

32 Declaring the Class: 10/8/2015lecture 2 32 Modifiers Class Class name extends name of super class نكتبها في حالة التوريث من class إلى class فقط Structural form of the declaring for Class: 1- Modifiers: they are contain of set of the Different types, It is the most famous:  public: Means that it can create any number of objects belonging to this class under any other class, whether this class in the same package or in any other package. Example: public class AA  Final: Means that the class does not contain subclasses, which can not use any of his properties by another class. Example: final class AA  Abstract: Means that this class is a Super Class to a group of other classes so that they can be used for all variables and functions in the abstract class. Example: abstract class AA

33 2 – Class Name: Taken into account when choosing the name of the class as follows: 10/8/2015lecture 2 33 Declaring the Class: 1- The name of letters and numbers. 2- The name must begin with a letter the rest of the name can be letters or numbers. 3- should not be the name of reserved words in the language. 4- Java language is case sensitive letters where distinguish between uppercase and lowercase letters. 5- Each statement in java must be finished with semicolon (;). 6- The code in java must be written between brackets {}. 3 –Super Class: That is, it can be certain that the Class inherits and uses all the variables and functions of the Class has been built previously, called this characteristic feature of inheritance. Example: public class AA extends BB ال Class AA يرث جميع المتغيرات والوظائف في ال Class BB

34 Body of Class: 10/8/2015lecture 2 34 Modifiers Class Class name extends name of super class The class contain of a set of data (variables and constants ) and a set of methods, and the structural form of the contents of the class as follows: Type _ variable 1; Type _ variable 2; Type _ variable 3; Type _ variable 4; Type method _name 1 (parameter_ list) { Method1_ body; } Type method _name 2(parameter_ list) { Method2_ body; } The class body } { Public static void main (string [ ] args) the java program

35 Body of Class: 1 –Variables: 10/8/2015lecture 2 35 Is a collection of variables that are declared within the brackets of the beginning and the end of the Class, These variables can be used by all the methods within the class. Note that the variables that are declared within the method Be within the knowledge of this method are only, in this case the internal or local variables.  access modifier: Each variable access modifier explains how to use this variable, they are as follows: FinalStaticPrivateProtectedPublic Means that the value of this variable can not be changed after giving it the initial value. Ex: final int aa We can refer to this variable in terms of the class directly without resorting to create the object. Ex: static int aa Variable is visible to all methods in the class basic, but invisible to any other Class or any subclass of the class basic. Ex: Private int aa Variable is visible to all the class and subclasses in the package case only. Ex: Protected int aa Variable visible to all the class and subclasses in the same package and the external package. Ex: Public int aa

36 10/8/2015lecture 2 36 Body of Class:  Primitive Types: Java has eight primitive types, each with its own purpose and use: 1- boolean 5- int 2- byte 6- long 3- short 7- float 4- char 8- double Example: Public int R Final int aa Private float x  Rules for creating Identifiers (variables): There are several rules that must be obeyed when creating identifier: 1- The first character of in identifier must be letter, After that all subsequent characters can be letters or numbers. 2- should not be the variable of reserved words in the language. 3- Java is case sensitive uppercase and lowercase letters are different, so a1 and A1 are different identifiers. 4- we use the underscores ( _ ) in the naming of variables.

37 10/8/2015lecture 2 37 Is the constant value that does not change. final datatype CONSTANTNAME = VALUE; Example: final double PI = 3.14159; final int SIZE = 3; 2 –constant: Body of Class: 3- Operations on constants and variables: 1- Mathematical operations2- Logical operations3- Relational operations

38 10/8/2015lecture 2 38 ExampleDescriptionMathematical operations Assignment operator= i += 8i = i+8 Add and assign operator+= i -= 8 i=i-8 subtract and assign operator-= i *= 8 i = i* 8 Multiply and assign operator*= i /= 8i = i / 8 divide and assign operator/= i %= 8i = i % 8 modulus and assign operator%= ++xIncrease before the operation++ variable X++Increase after the operationVariable++ --xDecreases before the operation--Variable X--Decreases after the operationVariable--  Mathematical operations: A set of mathematical and logical operations that can be applied to variables and constants in Java: Body of Class:

39 10/8/2015lecture 2 39  Logical operations: Body of Class: Descriptionlogical operations Logical AND&& Logical OR|| Logical NOT!

40 10/8/2015lecture 2 40  Relational operations: DescriptionRelational operations greater than> less than< greater than or equal to>= less than or equal to<= equal to= Not equal to!= Body of Class:

41 10/8/2015lecture 2 41 Is the basic syntax of the Java language program is directed directly to the compiler. Public static void main (string [ ] args): Output Statement: Is one of the types of methods in the Java and is considered among the basic printing in Java where you print the output of the program on the screen. System.out.println( ); Example: System.out.println( “Name’’); System.out.println( nam);

42 10/8/2015lecture 2 42 Class Sample_1 { public static void main (String [ ] args) { System.out.println(“Have a nice day”); } Simple program 1: Output :

43 10/8/2015lecture 2 43 Class Sample_2 { public static void main (String [ ] args) { int a1 = 12; int a2; System.out.println(“value of a1=“+ a1); a2= a1/2; System.out.println(“value of a2=“+ a2); } Simple program 1: Output :


Download ppt "Chapter 1 Introduction to Java 10/8/2015 Lecture 1 1."

Similar presentations


Ads by Google