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

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

Embed Size (px)

Citation preview

  • Slide 1
  • Chapter 1:7 Fundamentals of Programming 1 Object Oriented programming Instructor: Dr. Essam H. Houssein
  • Slide 2
  • 2 A.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 39-40 Servlets & JSP Week 13 Lec (14): Ch 19 Binary I-O Week 14
  • Slide 3
  • 3 A.List of References: Essential Book: Introduction to Java Programming Comprehensive Version, 8 th Edition, Y. Daniel Liang, Prentice Hall, 2011. Recommended Book: Java How to Program, 10th Edition, P. Deitel, H. Deitel, Prentice Hall, 2012. Java How to Program Course Coordinator: Dr. Essam H. Houssein Signature :()
  • Slide 4
  • 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 %
  • Slide 5
  • 5
  • Slide 6
  • 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.
  • Slide 7
  • 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)
  • Slide 8
  • 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);
  • Slide 9
  • 9
  • Slide 10
  • 10 2.4 Identifiers 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 0.305 are literals in the following statements: int numberOfYears = 34;
  • Slide 11
  • 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';
  • Slide 12
  • 12 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";
  • Slide 13
  • 13 2.16.3 Proper Indentation and Spacing Indentation is used to illustrate the structural relationships between a programs 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:
  • Slide 14
  • 14 2.16.4 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.
  • Slide 15
  • 15 2.17 Programming Errors 2.17.1 Syntax Errors Errors that occur during compilation are called syntax errors or compile errors. 2.17.2 Runtime Errors Runtime errors are errors that cause a program to terminate abnormally. 2.17.3 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.
  • Slide 16
  • 16 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); }
  • Slide 17
  • 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; }
  • Slide 18
  • 18
  • Slide 19
  • 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
  • Slide 20
  • 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; break; case 2: compute taxes for married filing separately; break; case 3: compute taxes for head of household; break; default: System.out.println("Errors: invalid status"); System.exit(0); }
  • Slide 21
  • 21 4.2 The while Loop The syntax for the while loop is as follows: while (loop-continuation-condition) { // Loop body Statement(s); }
  • Slide 22
  • 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);
  • Slide 23
  • 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); }
  • Slide 24
  • 24 5.2 Defining a Method The syntax for defining a method is as follows: modifier returnValueType methodName(list of parameters) { // Method body; }
  • Slide 25
  • 25
  • Slide 26
  • 26
  • Slide 27
  • 27 5.3.1 Call Stacks
  • Slide 28
  • 28 5.3.1 Call Stacks
  • Slide 29
  • 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.
  • Slide 30
  • 30
  • Slide 31
  • 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.
  • Slide 32
  • 32 double[] myList = new double[4]; // Creating Arrays double[] myList; // Declaring Array Variables 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.
  • Slide 33
  • 33 6.5 Copying Arrays 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);
  • Slide 34
  • 34
  • Slide 35
  • 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] }
  • Slide 36
  • 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
  • Slide 37
  • 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'));
  • Slide 38
  • 38 int[][] matrix; matrix = new int[5][5]; matrix[2][1] = 7;
  • Slide 39
  • 39 Assignment (1): Write a program that: 1- Displaying Prime Numbers Page 137 2- Determining Leap YearPage 90 3- Displaying CalendarsPage 151 4- Displaying Twin PrimesPage 195 5- Counting the Occurrence of each Letterpage 212 6- Counting of Occurrence of NumbersPage 227
  • Slide 40
  • Thanks for Attention