Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented programming

Similar presentations


Presentation on theme: "Object Oriented programming"— Presentation transcript:

1 Object Oriented programming
Chapter 1:7 Fundamentals of Programming Instructor: Dr. Essam H. Houssein

2 Content Lec # Subject Time Lec (1):
Chapter 1:7 Fundamentals of Programming Week 1 Lec (2): Chapter 8 Objects and Classes Week 2 Lec (3): Chapter 9 Strings and Text I/O Week 3 Lec (4): Chapter 10 Thinking in Objects Week 4 Lec (5): Chapter 11 Inheritance and Polymorphism Week 5 Lec (6): Chapter 13 Exception Handling Week 6 Lec (7): Chapter 14 Abstract Classes and Interfaces Week 7 Lec (8): Chapter 19 Binary I/O Week 8 Lec (9): Chapter 12 GUI Basics Week 9 Lec (10): Chapter 15 Graphics Week 10 Lec (11): Chapter 16 Event-Driven Programming Week 11 Lec (12): GUI & Menus & Multithreading Week 12 Lec (13): Ch Servlets & JSP Week 13 Lec (14): Ch 19 Binary I-O Week 14

3 List of References: Essential Book: Introduction to Java™ Programming Comprehensive Version, 8th Edition, Y. Daniel Liang, Prentice Hall, 2011. Recommended Book: Java™ How to Program, 10th Edition , P. Deitel, H. Deitel , Prentice Hall, 2012. Course Coordinator: Dr. Essam H. Houssein Signature :( )

4 Weighting of Assessments:
Final-Term Examination (90 Degree) 65% Mid-Term Examination (10 Degree) 10 % Practical and Oral Examination (Project) (20 Degree) 20% Other types of assessment (Assignments) (5 Degree) 5% 100 %

5

6 1.6 The Java Language Specification, API, JDK, and IDE
The Java language specification is a technical definition of the language that includes the syntax and semantics of the Java programming language. The application program interface (API) contains predefined classes and interfaces for developing Java programs.

7 Java Development Toolkit JDK consists of a set of separate programs, each invoked from a command line, for developing and testing Java programs. Besides JDK, you can use a Java development tool (e.g., Net- Beans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE)

8 2.3 Reading Input from the Console
Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows: Scanner input = new Scanner(System.in);

9

10 2.4 Identifiers 2.8.2 Numeric Literals
As you see in Listing 2.3, ComputeAverage, main, input, number1, number2, number3, and so on are the names of things that appear in the program. Such names are called identifiers. All identifiers must obey the following rules: 2.8.2 Numeric Literals A literal is a constant value that appears directly in a program. For example, 34 and are literals in the following statements: int numberOfYears = 34;

11 System.out.println( (int) 1.7 ) ;
2.13 Character Data Type and Operations The character data type, char, is used to represent a single character. A character literal is enclosed in single quotation marks. Consider the following code: char letter = 'A'; char numChar = '4';

12 String message = "Welcome to Java";
2.15 The String Type The char type represents only one character. To represent a string of characters, use the data type called String. For example, the following code declares the message to be a string with value “Welcome to Java”. String message = "Welcome to Java";

13 2.16.3 Proper Indentation and Spacing
Indentation is used to illustrate the structural relationships between a program’s components or statements. Java can read the program even if all of the statements are in a straight line, but humans find it easier to read and maintain code that is aligned properly. Indent each subcomponent or statement at least two spaces more than the construct within which it is nested. A single space should be added on both sides of a binary operator, as shown in the following statement:

14 Block Styles A block is a group of statements surrounded by braces. There are two popular styles, next-line style and end-of-line style, as shown below.

15 2.17 Programming Errors 2.17.1 Syntax Errors
Errors that occur during compilation are called syntax errors or compile errors. Runtime Errors Runtime errors are errors that cause a program to terminate abnormally. Logic Errors Logic errors occur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons. Logic errors are called bugs. The process of finding and correcting errors is called debugging.

16 if (boolean-expression) { statement(s); }
3.4.1 One-Way if Statements A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown below: if (boolean-expression) { statement(s); }

17 3.6 Two-Way if Statements A one-way if statement takes an action if the specified condition is true. If the condition is false, nothing is done. But what if you want to take alternative actions when the condition is false? You can use a two-way if statement. The actions that a two-way if statement specifies differ based on whether the condition is true or false. Here is the syntax for a two-way if statement: if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case;

18

19 3.15 switch Statements The if statement in Listing 3.6, ComputeTax.java, makes selections based on a single true or false condition. There are four cases for computing taxes, which depend on the value of status. To fully account for all the cases, nested if statements were used. Overuse of nested if statements makes a program difficult to read. Java provides a switch statement to handle multiple conditions efficiently. You could write the following switch statement to replace

20 the nested if statement in Listing 3.6:
switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married filing jointly; case 2: compute taxes for married filing separately; case 3: compute taxes for head of household; default: System.out.println("Errors: invalid status"); System.exit(0); }

21 while (loop-continuation-condition) {
4.2 The while Loop The syntax for the while loop is as follows: while (loop-continuation-condition) { // Loop body Statement(s); }

22 4.3 The do-while Loop The do-while loop is a variation of the while loop. Its syntax is given below: do { // Loop body; Statement(s); } while (loop-continuation-condition);

23 4.4 The for Loop In general, the syntax of a for loop is as shown below: for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Statement(s); }

24 5.2 Defining a Method The syntax for defining a method is as follows:
modifier returnValueType methodName(list of parameters) { // Method body; }

25

26

27 5.3.1 Call Stacks

28 5.3.1 Call Stacks

29 5.8 Overloading Methods that is, two methods have the same name but different parameter lists within one class. 5.9 The Scope of Variables The scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a method is referred to as a local variable.

30

31 5.12 Method Abstraction and Stepwise Refinement
Method abstraction is achieved by separating the use of a method from its implementation. The client can use a method without knowing how it is implemented. The details of the implementation are encapsulated in the method and hidden from the client who invokes the method. This is known as information hiding or encapsulation.

32 double[] myList; // Declaring Array Variables
double[] myList = new double[4]; // Creating Arrays This statement declares an array variable, myList, creates an array of ten elements of double type, and assigns its reference to myList. The size of an array cannot be changed after the array is created.

33 6.5 Copying Arrays list2 = list1;
Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: list2 = list1; Another approach is to use the arraycopy method in the java.lang.System class to copy arrays instead of using a loop. The syntax for arraycopy is shown below: arraycopy (sourceArray, src_pos, targetArray, tar_pos, length);

34

35 What is the difference between pass-by-value and pass-by-sharing?
public class Test { public static void main(String[] args) { int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values m(x, y); // Invoke m with arguments x and y System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); } public static void { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0]

36 6.9 Searching Arrays 6.10 Sorting Arrays 6.11 The Arrays Class
The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types. double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); // Sort the whole array

37 char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'};
System.out.println("(3) Index is " + java.util.Arrays.binarySearch(chars, 'a')); System.out.println("(4) Index is " + java.util.Arrays.binarySearch(chars, 't'));

38 int[][] matrix; matrix = new int[5][5]; matrix[2][1] = 7;

39 Assignment (1): Write a program that:
1- Displaying Prime Numbers Page 137 2- Determining Leap Year Page 90 3- Displaying Calendars Page 151 4- Displaying Twin Primes Page 195 5- Counting the Occurrence of each Letter page 212 6- Counting of Occurrence of Numbers Page 227

40 Thanks for Attention


Download ppt "Object Oriented programming"

Similar presentations


Ads by Google