Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
University of Petra Faculty of Information Technology Data Structures in Java: An Introduction Our world if full of DATA These data need to be processed Dr techn Dr. techn. Dipl Dipl. Inform Inform. Bassam Haddad Associate Professor of Computer Science Faculty of Information Technology Petra University Office: 7312 Ext. 340 @ op.e .jo [email protected] Introduction PART I How to store data in a Computer Program? How to organize the involved Data? How data are arranged in relation to each other? Which Which operations and parameters? Which data are kept in the memory? Which are calculated when needed? Which are kept in files, and how the files are arranged? Which method is more efficient? Course No.: 601221 Prerequisite:601212 PL-II 2010-1 Which Programming Language? Why JAVA? Is our program correct (Testing & Verification) What about the maintenance of a Program Based on: Data Structures and Abstractions with JAVA Frank M. Carrano & W. Savitech, Pearson Education, 2Ed, 2007 (Changing the Environment, Environment New Demands on the Program) ..... We need to organize our Data used in a program In Data Structures We need to specify such Data Structures 2 © Bassam Haddad 2010 © Bassam Haddad 2010 2 1 Data Structures in Java: An Introduction Specification of Data Structures Abstract Data Types ADT Formal Model ( Specification) that describes: The Types of Data Operations on these Data Involved parameters Implementation Realization in a PL C++, Java, C, Prolog, … Data Structures in Java: An Introduction Which DS we will learn in this course? Lists Stack Regular Queue Deque Priority Queue Implementations p Trees Graph Dictionary Recursion Searching and Sorting Algorithm Analysis Data Structures in JAVA We call these (Abstract) Structures Data Structures in the Implementation phase © Bassam Haddad 2010 3 © Bassam Haddad 2010 4 2 Objects & Classes An object is a program construct that Contains data (Instance Variables, data fields, data members, properties, ...) Performs certain actions • The actions are called methods (functions, behavior, operations, member functions, …) • The actions interact to form the solution to a given problem Objects & Classes Example of a class: A class is a type yp or kind of object j Objects of the same class have The same kinds of data The same methods • A class definition is a general description of What Wh t the th object bj t is i What it can do From DS and Abstr. Carrano & Savitch 2003 © Bassam Haddad 2010 5 © Bassam Haddad 2010 6 3 Objects & Classes Objects & Classes: An Example Class Modifier: public, final, abstract Instantiation of the Class Automobile public class Name NajeebCar slemanCar bassamCar { private String first; private String last; // first name // last name Variable Modifier: protected, public, private Scope: final, static < Definitions of methods are here. > Jordan Mercedes } // end Name Jeep Method Modifier: public, protected, private Scope: final, static public bli String St i getFirst() tFi t() { return first; } // end getFirst body of the method public: no restriction on where to use the class, any other Java class can use it final: cannot be overridden in a derived class abstract: has no definition and must be overridden in a derived class. Avoiding the creation of objects of such class. Abstract methods: derived class should implement them static method: can be shared by all objects of the class From DS and Abstr. Carrano & Savitch 2003 © Bassam Haddad 2010 7 © Bassam Haddad 2010 8 4 Objects & Classes Example: Java Implementation Strategy Implementation Strategy: Interface The Class The Client public class Name {p private String g first;; private String last; public Name () // Defualt Constuctor { } public Name(String fName, String lName) { first=fName; last=lName; } The Interface 1) public String getFirst() { return first; } public String getLast() { return last; } public interface NameInterface { …… } The class public bli String St i toString() t St i () { return first + " "+ last; } /*t /*toString St i M Method*/ th d*/ 2) public static void main(String[ ] args) { Name a = new Name(); Name b= new Name("Bashar", "ahmad"); System.out.println(a.getLast()); // Null The Client 3) System.out.println(a.getFirst()); //Null System.out.println(b.getLast()); System.out.println(b.getFirst()); public class Name implementsNameInterface { …… } public class NameClient { …… } a=b; /* reference to a, alias to a*/ System.out.println(a); //*because because of toString Menthod*/ Menthod / System.out.println(b); } } © Bassam Haddad 2010 9 5 Example: Java Implementation Strategy NameInterface Example: Java Implementation Strategy Class: Name implementation public interface NameInterface { public String getFirst(); /**Task:getFirst getting the first Name *@param: no *@return: String*/ public class Name implements NameInterface { private String first; private String last; public Name() // Default Constructor { } public Name(String fName, String lName) { first=fName; last=lName; } public String getLast(); /**Task: getLast *@param: no *@return: String */ public String toString(); /**Task:toString printing an object *@param: no *@return: String*/ public String getFirst() { return first; } public String getLast() { return last; } } public String toString() { return first + " "+ last; } //*toString toString Method Method*// } © Bassam Haddad 2010 11 © Bassam Haddad 2010 12 6 Example: Java Implementation Strategy Composition: A class uses composition when it has a data filed that is an instance of another class client class with main public class ClientName{ public static void main(String[ ] args) { NameInterface a = new Name(); NameInterface b= new Name(“Najeeb", “momo"); a=b; system.out.println(a.getLast()); system.out.println(a.getFirst()); } } © Bassam Haddad 2010 Composition 13 Example : an object of type Student. private Name fullName; private String id; © Bassam Haddad 2010 14 7 Assignment 1 Use the composition concept to write a Java program based on the introduced implementation Strategy: 1) A StudentInterface with at least the following methods public Name g p getName() () public String getId () public String toString() and the constructors 2) Student class implementing the StudentInterface 3 A client class defining at least three student objects j with their fullName and and id’s 4) Test you class in the LAB © Bassam Haddad 2010 15 8