400 likes | 445 Views
Object Oriented programming. Instructor: Dr. Essam H. Houssein Dr Mona A. S. Ali. Content. List of References: Essential Book: Introduction to Java™ Programming Comprehensive Version, 9 th Edition, Y. Daniel Liang, Prentice Hall, 2013.
E N D
Object Oriented programming Instructor: Dr. Essam H. Houssein Dr Mona A. S. Ali
List of References: Essential Book:Introduction to Java™ Programming Comprehensive Version, 9th Edition, Y. Daniel Liang, Prentice Hall, 2013. Recommended Book:Java™ How to Program, 10th Edition , P. Deitel, H. Deitel , Prentice Hall, 2012. Course Coordinator:Dr. Essam H. Houssein Dr. Mona A.S. Ali
Weighting of Assessments: Final-Term Examination (90 Degree) Mid-Term Examination(10 Degree) • Practical and Oral Examination (Project) (20 Degree) • Other types of assessment (Assignments) (5 Degree) • 125
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.
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)
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);
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: intnumberOfYears = 34;
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';
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";
2.16.3 Proper Indentation and Spacing Indentationis 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:
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.
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.
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); • }
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; • }
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
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); }
4.2 The while Loop • The syntax for the while loop is as follows: • while (loop-continuation-condition) { • // Loop body • Statement(s); • }
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);
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); • }
5.2 Defining a Method • The syntax for defining a method is as follows: • modifier returnValueTypemethodName(list of parameters) { • // Method body; • }
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.
5.12 Method Abstraction 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.
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.
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 methodin 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);
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] } }
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 java.util.Arrays.binarySearch(list, 11));
int[][] matrix; matrix = new int[5][5]; matrix[2][1] = 7;
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