Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 1: Course overview.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
Inheritance Inheritance Reserved word protected Reserved word super
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 27 - Java Object-Oriented Programming Outline 27.1Introduction 27.2Superclasses and Subclasses.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Object-Oriented Programming Outline Introduction Superclasses and Subclasses protected Members Relationship between Superclass Objects and Subclass Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 8 – Sections covered
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
 2002 Prentice Hall. All rights reserved. Chapter 9 - Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 8 Inheritance and Polymorphism 一切皆有缘起.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
 2002 Prentice Hall. All rights reserved. Modifed by Haytham Allos, NYU CSD V Week 2 - Object-Oriented Programming Outline 2.1 Introduction 2.2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
April 27, 1998CS102-02Lecture 5-1 Object-Oriented Programming Revisited CS Lecture 5-1 The Wonderful World of Objects.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Computer Programming 2 Lecture 8: Object Oriented Programming Creating Classes & Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Introduction To Programming Information Technology , 1’st Semester
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Introduction to Data Structure
Presentation transcript:

Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 1: Course overview

2 Outlines  What is array and why ?  Questions about array  Declaring and Allocating Arrays  Examples  Passing Arrays to Methods  Sorting element of arrays  Searching Arrays: Linear Search and Binary Search  Overview: Object- Oriented Programming  Overview: Inheritance

3 They said قال رسول الله صلى الله عليه وسلم : ( من دعا إلى هُدى كان له من الأجر مثل أجور من تَبِعَه لا ينقص ذلك من أجورهم شيئا) رواه مسلم

4 What is array and why ?  Problem You need to keep track of a fixed amount of information and retrieve it (usually) sequentially.  Solution Use an array.

5 What is array and why ?  Array is a group of contiguous memory locations that all have the same name and th same type.  Example: 1.Array of students 2.Array of fruits 3.Array of integers 4.Array of characters

6 Index of array

7 Questions about array  Is it a data structure ?  Is it a dynamic or static ? Dynamic data structure : 1.Linked List 2.Stack (LIFO) 3.Queue (FIFO)

8 Declaring and Allocating Arrays  Arrays occupy space in memory.  The programmer specifies the type of the elements and uses operator new to allocate dynamically the number of elements required by each array.  Arrays are allocated with new because arrays are objects and all objects must be created with new.

9 Declaring and Allocating Arrays type array_name [] = new type[ x ]; Type of elements which will be allocated in the array Number of elements which will be allocated in the array type array_name []; // declares the array array_name = new type[ x ]; // allocates the reference to the array type array_name [,] = new type[ x, y ];

10 Allocating an Array and Initializing Its Elements (Example 1)  Creates integer arrays of x elements and displays it // JoptionPane.showMssageDialog(null, output); X [ ]

11 Allocating an Array and Initializing Its Elements (Example 2) Try it ….

12 Passing Arrays to Methods  To pass an array argument to a method, specify the name of the array without using brackets.  For example, if array students declared as int students[] = new int[ 24 ]; the method call ModifyArray( students );  For a method to receive an array through a method call, the method’s parameter list must specify that an array will be received. For example, the method header for method ModifyArray might be written as: void ModifyArray( int s[] )

13 Sorting elements of array  Sorting : putting a collection of data in some order.  Bubble sort:  compares pairs of elements during a pass  an element can only move up one spot each pass  finished if nothing was swapped or at most n-1 passes  How to program it using Java ?  Two or loops  Who’s larger ?  Swap

14 Sorting elements of array  Try it …

15 Searching Arrays: Linear Search and Binary Search Search Linear SearchBinary Search

16 Searching Arrays: Linear Search and Binary Search Try it …

17 Object- Oriented Programming  What is OOP ?  Objects in the problem domain are mapped to objects in software

18 OOP : main concepts  The main concepts:  Class:  Object  Data Encapsulation  Data and operations are grouped together Account Withdraw Deposit Transfer Interface: Set of available operations

19 OOP : main concepts  The main concepts:  Abstract: Hierarchical classification School DoorChairstudents RoomsPersons LengthTypeColor

20 OOP : main concepts  The main concepts:  Inheritance  A class which is a subtype of a more general class is said to be inherited from it.  The sub-class inherits the base class’ data members and member functions

21 OOP : main concepts  The main concepts:  Polymorphism:  One interface  Multiple implementations  Inheritance  Method overloading

22 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference to a superclass or subclass type –private members Accessible only in methods of the superclass –protected members Intermediate protection between private and public Only accessible by methods of superclass, of subclass, or classes in the same package

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Point definition 1.1 Data members 1.2 Constructors 1.3 Methods 1// Fig. 27.3: Point.java 2// Definition of class Point 3 4public class Point { 5 protected int x, y; // coordinates of the Point 6 7 // No-argument constructor 8 public Point() 9 { 10 // implicit call to superclass constructor occurs here 11 setPoint( 0, 0 ); 12 } // Constructor 15 public Point( int a, int b ) 16 { 17 // implicit call to superclass constructor occurs here 18 setPoint( a, b ); 19 } // Set x and y coordinates of Point 22 public void setPoint( int a, int b ) 23 { 24 x = a; 25 y = b; 26 } // get x coordinate 29 public int getX() { return x; } 30

 2000 Prentice Hall, Inc. All rights reserved. Outline 1.2 Methods Circle Definition 1.1 extends Point 1.2 Multiple constructors 38// Fig. 27.3: Circle.java 39// Definition of class Circle 40 41public class Circle extends Point { // inherits from Point 42 protected double radius; // No-argument constructor 45 public Circle() 46 { 47 // implicit call to superclass constructor occurs here 48 setRadius( 0 ); 49 } // Constructor 52 public Circle( double r, int a, int b ) 53 { 54 super( a, b ); // call to superclass constructor 55 setRadius( r ); 56 } // Set radius of Circle 59 public void setRadius( double r ) 60 { radius = ( r >= 0.0 ? r : 0.0 ); } 31 // get y coordinate 32 public int getY() { return y; } // convert the point into a String representation 35 public String toString() 36 { return "[" + x + ", " + y + "]"; } 37}

 2000 Prentice Hall, Inc. All rights reserved. Outline 1.3 Overridden toString method // Get radius of Circle 63 public double getRadius() { return radius; } // Calculate area of Circle 66 public double area() { return Math.PI * radius * radius; } // convert the Circle to a String 69 public String toString() 70 { 71 return "Center = " + "[" + x + ", " + y + "]" + 72 "; Radius = " + radius; 73 } 74}

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize objects 2. Refer to a subclass object with a superclass reference 2.1 toString 2.2 Downcast 2.3 toString 75// Fig. 27.3: InheritanceTest.java 76// Demonstrating the "is a" relationship 77import java.text.DecimalFormat; 78import javax.swing.JOptionPane; 79 80public class InheritanceTest { 81 public static void main( String args[] ) 82 { 83 Point pointRef, p; 84 Circle circleRef, c; 85 String output; p = new Point( 30, 50 ); 88 c = new Circle( 2.7, 120, 89 ); output = "Point p: " + p.toString() + 91 "\nCircle c: " + c.toString(); // use the "is a" relationship to refer to a Circle 94 // with a Point reference 95 pointRef = c; // assign Circle to pointRef output += "\n\nCircle c (via pointRef): " + 98 pointRef.toString(); // Use downcasting (casting a superclass reference to a 101 // subclass data type) to assign pointRef to circleRef 102 circleRef = (Circle) pointRef; output += "\n\nCircle c (via circleRef): " circleRef.toString();

 2000 Prentice Hall, Inc. All rights reserved. Outline 2.4 Print area 3. if statement DecimalFormat precision2 = new DecimalFormat( "0.00" ); 108 output += "\nArea of c (via circleRef): " precision2.format( circleRef.area() ); // Attempt to refer to Point object 112 // with Circle reference 113 if ( p instanceof Circle ) { 114 circleRef = (Circle) p; // line 40 in Test.java 115 output += "\n\ncast successful"; 116 } 117 else 118 output += "\n\np does not refer to a Circle"; JOptionPane.showMessageDialog( null, output, 121 "Demonstrating the \"is a\" relationship", 122 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 125 } 126}

 2000 Prentice Hall, Inc. All rights reserved. Outline Program Output

29 Next Lecture isa Interfaces & Inner Classes