1 chapter 1. introduction -overview of course/housekeeping etc. -review of key object-oriented...

24
1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture 1

Upload: autumn-blevins

Post on 28-Mar-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

1

Chapter 1. Introduction

-Overview of course/housekeeping etc.-Review of key object-oriented concepts-Review of arrays and array lists

ADS2 Lecture 1

Page 2: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

2

Algorithms and Data structures 2 (ADS2)

• Lecturer Alice Miller: Room S133, [email protected]

• Course structure• 22 lectures @ 2 per week (Monday @ 11.00, Weds @ 1pm, both in Maths 516)• 5 X 2-hour labs @ 1 every other week (various times)-tutor start week 3 (week 19), i.e. wk beginning 25th Jan.

• Assessment• Degree exam (1.5 hours) in May - counts for 80%• 2 x Assessed coursework - counts for 20%

• To be awarded credit in ADS2 need *at least*•Attend 3 labs•Submit 1 piece of assessed coursework•Attend degree exam

ADS2 Lecture 1

Page 3: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

3

•Materials• lecture slides (outlines only, some bits missing). Available on moodle before lecture, may be modified after lecture • Lab sheets + examples class sheets via moodle• Course text (essential purchase)

• Goodrich and Tamassia, Data Structures and Algorithms in Java, 4th edn, Wiley, 2007, £35 approximatelyhttp://ww0.java4.datastructures.net/Other references:

•any other ADS/DSA in Java book (from library, see http://www.lib.gla.ac.uk/)•Savitch from last semester,•Another Java reference that has a nice view of things is “Thinking in Java” by Bruce Eckel. (See library)•Moodle page (see later..)

ADS2 Lecture 1

Page 4: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

4

Also• Plenty of opportunity in this course to give you little examples to do, to help solidify ideas (and hence less of me talking).

Why don’t I just go away and read the book myself?

Why come to lectures? Helps to• keep in step with the material, • keep in touch with the assignments etc. •… make friends?

Cartoons: I try to stick one or two in a lecture. Send me any you’d like me to include (must be clean and PC!)

ADS2 Lecture 1

“With this new blue screen graphics package, I can sit like this and superimpose a girlfriend…”

Page 5: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

5

ADS2 - The Context

• first course in Algorithms and Data structures for students with experience with programming in Java

• Introduces new concepts such as DS (data structures), ADTs (abstract data types) Algorithms to manipulate them (As), complexity analysis (to see how efficient the As are) recursion (a nice trick for writing short methods)

• Will assume you know the basics of Java, but will revise concepts before use

ADS2 Lecture 1

Page 6: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

6

• Where does ADS2 fit in?• OOSE2 will use some of the DS and As we introduce (hopefully not before we have done them!)

• project work in Levels 3 and/or 4 – use DS and As to get your programmes to run efficiently

•Alg3 - extends the As side, assumes knowledge of the DS and complexity analysis learned here.

In fact, basic As and DSs important in whole range of courses!

ADS2 Lecture 1

•It's a demanding and crucial course - be prepared to work hard at it

•You can revise your Java as you go along.

Page 7: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

JP2 Lecture 1 7

ADS2 - Modus operandi

• Lectures Mon 11-12, Weds 1-2 maths 516

•Examples class Friday weeks 19, 22, 25 (we are now in week 17) Maths 516

• 2 hour lab session weeks 19,21,23,25,27

• Lab sheets distributed in lectures and on moodle (actually lab sheet 1 should be there by the end of this week…). There are 5 labs, and 5 sheets (one per lab). Unlike OOSE

• Assessed work submission (via AMS) on date stipulated on assignment sheet (only 2/5 are assessed).

. Model answers posted on moodle once submission deadlines past

. Timetable/deadlines/lecture notes etc. posted on moodle: http://moodle2.gla.ac.uk/fims/moodle/

Log in to Algorithms and Data StructuresADS2 Lecture 1

Page 8: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

Assessed Coursework Dates

ADS2 Lecture 1 8

5 separate exercises, 2 of which are assessed.

Two assessed exercises Ads2; critical facts are:

•Two assignments• Each worth 10% of your final grade• Each will require that you implement a program using data structures and algorithms studied in the lectures• Electronic submission of your solution is due by 10:00 on the day following your lab during the week that the assignment is due

• Assignment 1 – available on 26th Jan, due the week beginning 8 February• Assignment 2 – available on 23rd Feb, due the week beginning 8  March

Note, OOSE is different, several labs devoted to each exercise. In ADS2, every lab has a different (smaller) exercise, only 2 are assessed, but need skills obtained by doing the others.

Page 9: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

9

Mon lecture lab ? Weds lecture Friday examples class

Week 17 Mon Jan 11th Weds Jan 13th

Week 18 Mon Jan 18th Weds Jan 20th

Week 19 Mon Jan 25th lab Weds Jan 27th Friday Jan 29th

Week 20 Mon Feb 1st Weds Feb 3rd

Week 21 Mon Feb 8th lab Weds Feb 9th

Week 22 Mon Feb 15th Weds Feb 17th Friday Feb 19th

Week 23 Mon Feb 22nd lab Weds Feb 24th

Week 24 Mon Mar 1st Weds Mar 3rd

Week 25 Mon Mar 8th lab Weds Mar 10th Friday Mar 12th

Week 26 Mon Mar 15th Weds Mar 17th

Week 27 Mon Mar 22nd lab Weds Mar 24th

ADS2 Lecture 1

Page 10: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

10

ADS2 - Rough Course Outline (may change!)

• Follows the course text (Goodrich + Tammassa) to some extent covers most of Chapters 1-7, 9-11)

1 Introduction. Review of OO concepts, arrays, The ArrayList class

2

3 Linked lists

4

5

6Recursion

7

8 Analysis tools

9

10 ADTs, stacks and queues

11

12Lists and iterators, the Java collections framework13

14

Trees + search trees, B Trees

15

16

17

18Fast Sorting Algorithms

19

20 Maps and Hash tables

21

22 RevisionADS2 Lecture 1

Page 11: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

11

Revision of OO concepts – See Goodrich chap 2

Classes:• Everything is an object (apart from base/primitive types)• Every object belongs to a class• Class consists of data members, and methods•Data members: the fields of the class•Methods:

•constructors, accessors, mutators, other methods•Methods that perform an action•Methods that return a value

•New object created from a defined class using new operator + constructor

ADS2 Lecture 1

Page 12: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

12

Creating a new objectmyVariable = new MyClass(param1,param2,...)

Causes the following:

• New object of type MyClass dynamically created in memory, all instance variables initialised to default value (null for object variables, 0 for base types except false for boolean null character for type char)

•Constructor for new MyClass object called with parameters specified, filling in meaningful values for instance variables and performing additional operations if necessary

• new operator returns a reference (memory address) to newly created object. Object variable myVariable refers to this newly created object

ADS2 Lecture 1

Page 13: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

Object references

13

• Address of object space assigned to reference variable. • Reference variable can be viewed as a pointer to some object.

Never attempt to access the object directly. Only by using the methods from the class associated with the object

Goodrich p. 9

the object

the reference

the reference variable

ADS2 Lecture 1

Page 14: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

Example (from JP2, lectures 6-7)

14

2 classes, Date and Student.

public class Date { private int day; private String month; private int year; /** constructor to assign a given date value */ public Date(int d, String m, int y){ day = d; month = m; year = y;}

/** no argument constructor */ public Date(){ day = 1; month = "January" ; year = 2000; }

// copy constructor that takes an existing Date as parameter public Date(Date d){ this.day = d.day; this.month = d.month; this.year = d.year; }/** Accessor methods */public int getDay(){return day;}

fragment from Date class:

ETC.

ADS2 Lecture 1

Page 15: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

15

fragment from Student class:

public class Student { private String familyName, givenName; private String studentNumber; private Date birthDate; private boolean male;/*constructor */ public Student(String f, String g, String s, Date b, boolean m){ familyName = f; givenName = g; studentNumber = s; birthDate = new Date(b); male = m;}

ETC.Includes methods to determine current age, and email address resp:

public int age(){/* return current age*/..}

public String emailAddress(){/* return email address*/..}

ADS2 Lecture 1

Page 16: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

16

public class TestStudent {public static void main(String[] args) { Student stud1, stud2; Date d1 = new Date(12, "October", 1989); Date d2 = new Date(13, "October", 1987);

stud1 = new Student("Smith", "John", "0791234", d1, true); stud2 = new Student("Brown", "Jane", "0792345", d2, false);

System.out.println(stud1 + " is aged " + stud1.age() + ", email " + stud1.emailAddress()); System.out.println(stud2 + " is aged " + stud2.age() + ", email " + stud2.emailAddress()); }

}

Main program:

ADS2 Lecture 1

Page 17: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

17

Information Hiding and Encapsulation

Savitch p. 197

“Encapsulation means that the data and actions are combined into a single item (a class object) and the details of the implementation hidden. Information hiding and encapsulation deal with the same general principle: If a class is well designed, a programmer who uses a class need not know all the details of the implementation of the class but need only know a much simpler description of how to use the class”

ADS2 Lecture 1

Page 18: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

18

APIs and ADTs

API= application programming interface. API for a class is a description of how to use the class. If class is well designed (using encapsulation) a programmer need only read the API and not look at the details of code for the class definition.

ADT = abstract data type. An ADT is a data type written using information-hiding techniques. We will build lots of ADTs.

You will have seen APIs at:http://java.sun.com/javase/6/docs/api/

Main structural element in Java that enforces an API is interface = collection of method declarations with no data and no bodies. Classes implement interfaces.Next example illustrates the use of interfaces (will come onto ADTs in later lectures). In JP2 you saw interfaces mainly in GUI context.

ADS2 Lecture 1

Page 19: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

19

Example (Goodrich p.81)

Create inventory of antiques in shop, catagorised as objects of various types with various properties.

Sellable and/or transportable.

Have interfaces Sellable and Transportable.

Photograph is a class that implements Sellable

BoxedItem is a class that implements both Sellable and Transportable

Could extend to other classes, and other interfaces as required.

ADS2 Lecture 1

Page 20: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

20

Example : Antiques

Sellable.java

/**Interface for objects that can be sold *///Goodrich p. 81package antiques;

public interface Sellable {/** description of the object. */public String description();

/**list price in cents */public int listPrice();

/**lowest price in cents we will accept */public int lowestPrice();

}

ADS2 Lecture 1

Page 21: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

21

/**Class for photographs that can be sold*///Goodrich p. 81package antiques;

public class Photograph implements Sellable { private String descript; //description of this photo private int price; //the price we are selling private boolean color; //true if photo is in colour

public Photograph(String desc, int p, boolean c){ //constructor descript = desc; price = p; color=c; }

public String description(){return descript;} public int listPrice(){return price;} public int lowestPrice(){return price/2;} public boolean isColor(){return color;}}

ADS2 Lecture 1

Page 22: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

22

/**Interface for objects that can be transported*/ //Goodrich p. 81package antiques;

public interface Transportable {

/**weight in grams */public int weight();

/**whether the object is hazardous*/public boolean isHazardous();

}

ADS2 Lecture 1

Page 23: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

23

/** Class for objects that can be sold, packed and shipped *///from Goodrich p. 82package antiques;public class BoxedItem implements Sellable, Transportable{

private String descript; //description of this itemprivate int price; //list price in centsprivate int weight; //weight in gramsprivate boolean haz; //true if object is hazardousprivate int height=0; //box height in centimetersprivate int width=0; //box width in centimetersprivate int depth=0; //box depth in centimeters public BoxedItem(String desc, int p, int w, boolean h){ descript = desc; price = p; weight =w; haz = h;} public String description(){return descript;} public int listPrice(){return price;} public int lowestPrice(){return price/2;} public int weight(){return weight;} public boolean isHazardous(){return haz;} public int insuredValue(){return price*2;} public void setBox(int h,int w,int d){ height=h; width=w; depth=d;}

}

ADS2 Lecture 1

Page 24: 1 Chapter 1. Introduction -Overview of course/housekeeping etc. -Review of key object-oriented concepts -Review of arrays and array lists ADS2 Lecture

24

Note:• Class can implement multiple interfaces (BoxedItem implements both Sellable and Transportable)

• This is “multiple inheritance” (according to GoTa p.83. Some people would not use this term for “implements”)

• In Java multiple inheritance is allowed for interfaces but not classes. Why?See JP2 lecture L2 slide 8.

• Could also create a new interface that combines the methods of both interfaces and adds more. See [GoTa] p. 83

• Note: abstract class is like an interface but may have some method implementations and/or data.

ADS2 Lecture 1