Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in java

Similar presentations


Presentation on theme: "Object Oriented Programming in java"— Presentation transcript:

1 Object Oriented Programming in java
Dana F. Doghramachi Chapter -1- Introduction to OOP 2015

2 What is java ? Designed by Sun Microsystems in 1991. Based on C/C++.
Java used for embedding programs into Web-based applications Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

3 Java Features -1- Object Oriented Robust
Object-oriented languages divide programs into separate modules, called objects, that encapsulate the program's data and operations. Robust meaning that errors in Java programs don't cause system crashes as often as errors in other programming languages. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

4 Java Features -2- Platform Independent Distributed Language Secure
Platform is a particular kind of computer system, such as a Macintosh or Windows system. Java program can be run without changes on different kinds of computer system. Distributed Language which means that its programs can be designed to run on computer networks. Secure Java designed to be used on networks, Java contains features that protect against entrusted code. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

5 Software Development Process -1-
source code is written in TextPad files with the .java extension. source files are then compiled into .class files by the java compiler. A .class contains bytecodes: the machine language of the Java Virtual Machine (Java VM). The java runs the application with an instance of the Java VM Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next. software development process

6 Software Development Process -2-
Java VM is available on many different operating systems. The same .class files are capable of running on. Microsoft Windows Solaris OS Linux Mac OS Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

7 Writing Java programs We need Creating an Application in Windows
The Java SE Development Kit 8 (JDK 8). Textpad Or Eclipse. Textpad.com. Creating an Application in Windows Create a source file in .java extension Compile the source file into a .class java compiler Run the program Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

8 "Hello World!" Application
Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

9 Comments words in blue color are comments
The Java supports two kinds of comments: /* text */ or /** documentation */ The compiler ignores everything from /* to */. // text The compiler ignores everything from // to the end of the line. Blank lines, spaces, and tabs are white-space characters Ignored by compiler. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

10 The Class Definition -1-
Begins class declaration for class HelloWorldApp Every Java program has at least one user-defined class. Keyword: words reserved for use by Java. class keyword followed by class name Naming classes: capitalize every word. //Naming Convention HelloWorldApp Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

11 The Class Definition -2-
Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces //Naming Rules Examples: Welcome1, $value, _value, button7 7button is invalid Java is case sensitive (capitalization matters) a1 and A1 are different use public keyword // explainlater Certain details not important now Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

12 The Class Definition -3-
Saving files File name must be class name with .java extension HelloWorldApp.java Left brace { Begins body of every class (line 6) Right brace ends declarations (line 11) Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

13 The main Method Part of every Java application
Applications begin executing at main Parenthesis indicate main is a method (exp. later). Java applications contain one or more methods. Exactly one method must be called main Methods can perform tasks and return info. void means main returns no information Left brace begins body of method declaration Ended by right brace } (line 10) Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

14 Printing a Line of Text Instructs computer to perform an action
Prints string of characters String - series characters inside double quotes White-spaces in strings are not ignored by compiler Method System.out.println Print to command window (i.e., MS-DOS prompt) Displays line of text Argument inside parenthesis This line known as a statement Statements must end with semicolon ; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

15 Printing Multi Line of Text
System.out.println( "Welcome\nto\nJava\nProgramming!" ); Notice how a new line is output for each \n escape sequence. Welcome to Java Programming! Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

16 Escape characters Backslash ( \ )
Indicates special characters be output Escape sequence Description \ n Newline. Position the screen cursor at the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. \\ Backslash. Used to print a backslash character. " Double quote. Used to print a double - quote character. For Example, System.out.println( "in quotes "" ); displays "in quotes"

17 Displaying Text with printf
The System.out.printf method (f means "formatted") displays formatted data. System.out.printf("%s%n%s%n", "Welcome to", "Java Programm"); Format specifies begin with a percent sign (%) followed by a character that represents the data type. For example, the format specifies %s is a placeholder for a string. The format string specifies that printf should output two strings, each followed by a newline character.

18 Java keywords Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

19 Primitive Data Types -1-
The eight primitive data types supported by the Java programming language are: byte: 8-bit signed two's complement integer minimum value -128 maximum value of 127 short: 16-bit signed two's complement integer minimum value of -32,768 maximum value of 32,767 Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

20 Primitive Data Types -2-
int: 32-bit signed two's complement integer minimum value of -2,147,483,648 maximum value of 2,147,483,647 4. long: 64-bit signed two's complement integer minimum value of -9,223,372,036,854,775,808 maximum value of 9,223,372,036,854,775,807 byte, short, int, and long are considered as Integral Types Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

21 Primitive Data Types -3-
float: single-precision 32-bit floating point double: double-precision 64-bit floating point boolean: has only two possible values: true and false char: single 16-bit Unicode character minimum value of '\u0000' (or 0) maximum value of '\uffff' (or 65,535) float and double are considered as Floating Point Types Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

22 String class the Java provides special support for character strings via the String class Enclosing character string within double quotes will automatically create a new String object for example, String s = "this is a string"; The String class is not a primitive data type Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

23 Variables -1- When the declaration is made, memory space is allocated to store the values of x and y. x and y are called variables. A variable has three properties: A memory location to store the value, The type of data stored in the memory location, and The name used to refer to the memory location. Sample variable declarations: int x; int v, w, y; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

24 Variables -2- float numberOne, numberTwo; long bigInteger;
Here is an example of declaring variables of different data types: int i, j, k; float numberOne, numberTwo; long bigInteger; double bigNumber; At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as in int count = 10, height = 34; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

25 Variable Types -1- The Java programming language defines the following kinds of variables: Instance Variables (Non-Static Fields) objects store their variables in "non-static fields", that is, fields declared without the static keyword. int cadence = 0; Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence static double pi = 3.14; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

26 Variable Types -2- Local Variables a method will often store its temporary state in local variables void show(){ int i = 0; ….. } Parameters the signature for the method is void changeGear(int newGear){ gear = newGear; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

27 Default Values -1- Fields that are declared but not initialized will be set to a reasonable default by the compiler. default will be zero or null, depending on the data type. Depending on such default values is considered bad programming style. the compiler never assigns a default value to an uninitialized local variable. Accessing an uninitialized local variable will result in a compile-time error. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

28 Default Values -2- Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

29 Naming Rules and Convention
We mean by naming rules the conditions that should be considered when naming user defined identifiers User defined identifiers such as variables, methods, classes If one of these rules is missed when naming an identifier the complier generates an error message Naming Conventions Conventions are the rules that is better to be considered when naming an identifier Missing these convention not cause any errors Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

30 Naming Rules (Variables)
Variable names are case-sensitive. an unlimited-length sequence of Unicode letters and digits. beginning with a letter, the dollar sign "$", or the underscore character "_“. White space is not permitted. Subsequent characters may be letters, digits, dollar signs, or underscore characters. the name must not be a keyword or reserved word. Examples for valid variable names: cadence, x1, _value, $i, bike1 … Example for invalid variable names: 1y, first Value, one*, void …. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

31 Naming Conventions (Variables)
Use names (not verbs) for variable names. When choosing a name for your variables, use full words instead of abbreviations. If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word, for example gearRatio and currentGear. If your variable stores a constant value capitalizing every letter and separating subsequent words with the underscore character. static final int NUM_GEARS = 6; Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

32 Naming (Methods and Classes)
Methods Naming The rules used in naming variables are also applied to method names. The conventions for methods is the same as for variables except using verbs as methods names. Classes Naming The rules used in naming variables are also applied to class names. The conventions for Classes is the same as for variables except capitalizing the first letter of the class name. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

33 Reading data from keyboard
There are many ways to read data from the keyboard. For example: InputStreamReader Console Scanner DataInputStream etc. Give a brief overview of the presentation. Describe the major focus of the presentation and why it is important. Introduce each of the major topics. To provide a road map for the audience, you can repeat this Overview slide throughout the presentation, highlighting the particular topic you will discuss next.

34 Scanner Class -1- Using a Scanner object is a simple way to input data from the standard input System.in, which accepts input from the keyboard. First we need to associate a Scanner object to System.in as follows: import java.util.Scanner; Scanner input= new Scanner(System.in);

35 Scanner Class -2- public String nextLine(): it moves the scanner position to the next line and returns the value as a string. public int nextInt(): it scans the next token as an int value. public double nextDouble(): it scans the next token as a double value.

36 Reading from Standard Input
After the Scanner object is set up, we can read data. Scanner input= new Scanner(System.in); System.out.print (“Enter a number: ”); Int firstNum = input.nextInt(); System.out.println(“The value is :” + firstNum); 1. Prompt is displayed Enter a number: 25 ENTER The value is : 25 2. Data is entered 3. Result is printed

37 Application of Adding Integers


Download ppt "Object Oriented Programming in java"

Similar presentations


Ads by Google