Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009

2 Lecture Overview Review ◦ Structures in MATLAB Advanced MATLAB ◦ Classes  isa function  hierarchy ◦ Cell Arrays

3 Structures in MATLAB

4 A Database Application Given= Name= Chris Credits= 27 Graduation= 12/15/2011 Name= Sola Credits= 18 Graduation= 05/17/2011 Name= Roger Credits= 55 Graduation= 06/10/2009 Name= Tom Credits= 15 Graduation= 05/22/2012

5 Given= We can implement it with arrays like this= Name Credits Grad. 1 2 3 4 Name= Chris Credits= 27 Graduation= 12/15/2011 Name= Sola Credits= 18 Graduation= 05/17/2011 Name= Roger Credits= 55 Graduation= 06/10/2009 Name= Tom Credits= 15 Graduation= 05/22/2012 27Chris12/15/2011 18Sola05/17/2011 55Roger06/10/2009 15Tom05/22/2012 A Database Application

6 Given= OR we can do it like this an array with structs=.d Name= Chris Credits= 27 Graduation= 12/15/2011 Name= Sola Credits= 18 Graduation= 05/17/2011 Name= Roger Credits= 55 Graduation= 06/10/2009 Name= Tom Credits= 15 Graduation= 05/22/2012 Students (1). Name= ‘Chris’ Students (1).Credits= 27 Students (1). Graduation= ‘12/15/2011’ Students (2).Name= ‘Sola’ Students (2).Credits= 18 Students (2).Graduation= ‘05/17/2011’ Students (3). Name= ‘Roger’ Students (3). Credits= 55 Students (3). Graduation= ‘06/10/2009’ Students (4). Name= ‘Tom’ Students (4). Credits= 15 Students (4). Graduation= ‘05/22/2012’ A Database Application

7 record1.name = 'Me'; record2.name = 'Not Me'; record1.credits = 27; record2.credits = 30; record1.age = 10; record2.age = 14; function [] = displayRecordName(record) disp(record.name); displayRecordName(record1); displayRecordName(record2); Initializing a structure

8 Classes Object-oriented Programming Classes represent types of Objects Similar to structs

9 Class definition classdef Student properties name credits age end

10 Class definition (cont’) classdef Student properties name credits age end methods function obj=Student(name, credits, age) obj.name = name; obj.credits = credits; obj.age = age; end

11 Class definition classdef Teacher properties name yearsTaught age end methods function obj= Teacher(name, yearsTaught, age) obj.name = name; obj.yearsTaught = yearsTaught; obj.age = age; end

12 Common properties? Student name credits age Teacher name yearsTaught age

13 Common properties (cont’)? Student credits Teacher yearsTaught Person name age An example of the “is a” relationship. A Student “is a” Person. A Teacher “is a” Person. The Person class contains shared properties.

14 Class definition (Person) classdef Person properties name age end methods function obj = Person(name, age) obj.name = name; obj.age = age; end

15 Class definition (Student) classdef Student < Person properties credits end methods function obj = Student(name, age, credits) obj@Person(name,age); obj.credits = credits; end

16 Class definition (Teacher) classdef Teacher < Person properties yearsTaught end methods function obj = Teacher(name, age, yearsTaught) obj@Person(name,age); obj.yearsTaught = yearsTaught; end

17 The isa function isa(object, ‘classname’) Examples= student1 = Student(‘Frank’,25,20); teacher1 = Teacher(‘James’,0,30); isa(student1, ‘Student’) isa(teacher1, ‘Teacher’) isa(student1, ‘Teacher’) isa(student1, ‘Person’)

18 Arrays of classes? student1 = Student(‘Frank’,25,20); student2 = Student(‘Sally’,50,21); teacher1 = Teacher(‘James’,0,30); [student1 student2] [student1 student2 teacher1] ?

19 Cell Arrays Normal array zeros(1,5) is 5 doubles [1 2 3 4] is 4 doubles Cell array {1 2 3 4} is 4 cells, each with a 1x1 array of double values inside

20 What is a cell? A cell is like a window into a room, and inside the room can be data of any size A cell array is like a hallway with many doors 15437 52 Student age=19 name=‘Joe’ credits=78 ‘hello’ 0 1.1 normal arraycell array

21 Cell Arrays (cont) c = {1 2 3 4} c{1} = ‘hello’ Cell arrays store cells ◦ Can be another array ◦ Or a string ◦... any struct/object/cell array


Download ppt "General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/13/2009."

Similar presentations


Ads by Google