Object Oriented programming

Slides:



Advertisements
Similar presentations
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Control Structures Corresponds with Chapters 3 and 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Java Programming, 4E
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 7: User-Defined Methods
Guide To UNIX Using Linux Third Edition
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Course Outline Kumar Harshit, USW. Course Description Teaches students to program using the Java programming language with the help of the Netbeans.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
 Review  Quick reference Announcement. Chapter 1: Basics.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Introduction to Java Programming with Forte Y. Daniel Liang.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Introduction to Java Java Translation Program Structure
1 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
By Mr. Muhammad Pervez Akhtar
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Terms: Software  Set of instructions  aka programs  Operating System:  Special software whose job it is to translateinstructions into hardware instructions.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Java Programming, 4E Y. Daniel Liang.
Introduction to Control Statements IT108 George Mason University.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Information and Computer Sciences University of Hawaii, Manoa
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Selections Java.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 6 Arrays.
Chapter 5 Control Statements
Chapter 1: Computer Systems
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Single-Dimensional Arrays chapter6
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

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

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

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 :( )

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 %

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 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 0.305 are literals in the following statements: int numberOfYears = 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';

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";

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:

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.

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); }

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; 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); }

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); }

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 returnValueType methodName(list of parameters) { // Method body; }

5.3.1 Call Stacks

5.3.1 Call Stacks

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 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.

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 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);

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

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'));

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

Thanks for Attention