Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn how to design and make use of data structures that are fundamental to all areas of computing. We will focus on abstract data types (ADTs) using the object-oriented paradigm. An ADT is abstractions of data and basic operations on the abstractions. We will use a Java class to implement an ADT. 3 1
Object-oriented paradigm 4 Object-oriented paradigm In general, programs are instructions to do something on given data. Programs two main components are: Data (or variables to store data). Statements that define computation (or something to do). 5 Object-oriented paradigm In conventional programming, data and computation are defined separately. In object-oriented programming, data and computation must be defined together. 6 2
What is an object? An object is data equipped with relevant computation. Formally, an object is defined by: A set of attributes that define data. A set of operations (or behaviors) that define actions on data. 7 What is an object? Example. Lucy, a dog, is an object defined by the following: Attributes: female, Golden Retriever, 22 in., 60 lbs., 5 years old. Operations (or behaviors): bark, walk, run, sit, jump, swim,... 8 Objects Many objects share common attribute types and operations. For example, all dogs share: Attributes: gender, breed, height, weight, age. Operations (or behaviors): bark, walk, run, sit, jump, swim,... In fact, all dog objects can be defined by these attributes and operations. 9 3
What is a class? A class is an abstract definition of objects that share common attribute types and operations. Formally, a class is defined by: A set of attributes without specific data. A set of operations (or behaviors) that define actions on data. 10 Classes and objects A class is an abstract description of objects that share common attribute types and operations. A class may be regarded as simply collection of objects that share common attribute types and operations. An object is an instance of a class. For example, the object Lucy is an instance of the class Dog. 11 Classes and objects In Java, an object must be defined as an instance of a class. Every Java class has a constructor that instantiates an object. In Java, attributes are called fields, and operations (or behaviors) are called methods. 12 4
Java class public class Dog { Fields (attributes) Constructor Methods (operations or behaviors) 13 Accessibility Classes, fields and methods must be declared with access modifiers. Classes and methods are usually declared as public accessible by anyone from anywhere. Fields are usually declared as private accessible only within the object. 14 Primitive data types Fields are often defined using variables. In Java, variables must be declared with types. There are four primitive data types: int integers:..., -1, 0, 1, 2,... double reals:..., 3.14159,... char letters: a, b, c,... boolean boolean values: true, false 15 5
public class Dog { Defining fields private String breed; private char gender; private double height; private double weight; private int age; Constructor Methods (operations or behaviors) 16 Methods Methods define objects operations, similar to the way functions define basic computational tasks. Many (but not all) methods return values, just like many functions return values. All methods must be declared with return types (which specify the types of values being returned). 17 public class Dog { Defining methods private String breed; private char gender; private double height; private double weight; private int age; Constructor public int getage() { return age; 18 6
public class Dog { Defining methods private String breed; private char gender; private double height; private double weight; private int age; Constructor public void getolder() { age = age + 1; 19 Constructor A constructor is a special method that instantiates an object. Objects are created from a class by invoking a constructor. Constructors usually have parameters, and constructors create objects according to the values of parameters. 20 Parameters Many (but not all) constructors and methods have parameters, just like many functions have parameters. In Java, parameters must be declared with types, just like variables must be declared with types. 21 7
public class Dog { Defining a constructor private String breed; private char gender; private double height; private double weight; private int age; public Dog(String b, char g, double h, double w, int a) { breed = b; gender = g; height = h; weight = w; age = a; Methods (operations or behaviors) 22 Instantiating an object Dog lucy = new Dog( Golden Retriever, f, 22, 60, 5); lucy.getolder(); System.out.print( Lucy is now + lucy.getage() + years old. ); 23 Accessors A method that simply returns the value of a field is called an accessor (or get method). An accessor for a field f is usually named getf. An accessor has no parameter. The return type of an accessor for a field f is the type of f. 24 8
Accessors public class Coo { private type f;... public type getf() { return f; 25 Mutators A method that simply changes the value of a field by a parameter value is called a mutator (or modifier or set method). A mutator for a field f is usually named setf. A mutator returns nothing. A mutator for a field f has a parameter whose type is the same as that of f. 26 public class Coo { private type f;... public type getf() { return f; Mutators public void setf(type f0) { f = f0; 27 9
Method calls Every method belongs to some object. In general, when we call a method, we must specify both the object and method: object.method(<parameters>) 28 Internal method calls To invoke a method within the same object, we can just call by the method name. public class Coo { public void moo() { noo(5); public void noo(int x) {... 29 Internal method calls Or, to be more explicit, you may also put this with the method name. public class Coo { public void moo() { this.noo(5); public void noo(int x) {... 30 10
External method calls To invoke a method of another object, you must always explicitly specify the object. In general, from a class Doo, to call a Coo object c s method noo(), put: public class Doo { public void poo() { Coo c = new Coo(); c.noo(7); 31 The main method Some class has a special method, called main, that defines a stand-alone program. A class that has the main method is called a Java application. public class Coo { public static void main(string[] args) {... 32 The main method To execute such a stand-alone program defined in the main method, just invoke java with the name of its class: $ java Coo 33 11
static method A method that does not belong to any object is called a static method. In general, when we call such a method, we must specify the class and method: Class.method(<parameters>) 34 static method public class Coo { public static void print() {... To call Coo s static method print() from outside, put: Coo.print(); 35 12