1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Fields, Constructors, Methods
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Further abstraction techniques Abstract classes and interfaces 5.0.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Further abstraction techniques Abstract classes and interfaces 1.0.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions Looking inside classes 3.0.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Further abstraction techniques Abstract classes and interfaces 3.0.
Abstract Classes and Interfaces
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)
OOP (Java): Abstract/ OOP Objectives – –use a foxes-and-rabbits simulation to introduce abstract classes, interfaces, and multiple.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
1 COS 260 DAY 1 Tony Gauvin. 2 Agenda Class roll call Instructor Introduction Instructor’s Educational Philosophy Contract on Classroom Behavior Syllabus.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Further abstraction techniques
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Further abstraction techniques Abstract classes and interfaces 6.0.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
11 Further abstraction techniques
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 13 Tony Gauvin.
Understanding class definitions
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Further abstraction techniques
Improving structure with inheritance
Presentation transcript:

1 COS 260 DAY 21 Tony Gauvin

2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due Discussion on Further Abstraction techniques

3 Final Countdown Today –9 th Mini Quiz –Assignment 5 due –Begin Chap 10 Nov 23 –Finish Chapter 10 Nov 30 –10 th Mini quiz –Begin Chap 11 –Capstone Progress Report Dec 3 –Finish Chapter 11 –Assignment 6 due Dec 7 –11 th mini quiz –Begin Chapter 12 Dec 10 –Finish Chapter 12 –Assignment 7 Due Dec 10 AM –12 th mini quiz –Capstone Presentations Copyright © 2014 Pearson Education, Inc. Slide 1-3

4 Change to Grade Calculations 12 instead of 11 mini quizzes –Still best 3% each 7 instead of 8 assignments –AVG(7) = 40% of final grade –Was 5% each Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Further abstraction techniques Abstract classes and interfaces 5.0

6 Main concepts to be covered Abstract classes Interfaces Multiple inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 Simulations Programs regularly used to simulate real-world activities. –city traffic –the weather –nuclear processes –stock market fluctuations –environmental changes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

8 Simulations They are often only partial simulations. They often involve simplifications. –Greater detail has the potential to provide greater accuracy. –Greater detail typically requires more resource. Processing power. Simulation time. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Benefits of simulations Support useful prediction. –The weather. Allow experimentation. –Safer, cheaper, quicker. Example: –‘How will the wildlife be affected if we cut a highway through the middle of this national park?’ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

10 Predator-prey simulations There is often a delicate balance between species. –A lot of prey means a lot of food. –A lot of food encourages higher predator numbers. –More predators eat more prey. –Less prey means less food. –Less food means... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 The foxes-and-rabbits project Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 Main classes of interest Fox –Simple model of a type of predator. Rabbit –Simple model of a type of prey. Simulator –Manages the overall simulation task. –Holds a collection of foxes and rabbits. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

13 The remaining classes Field –Represents a 2D field. Location –Represents a 2D position. SimulatorView, FieldStats, Counter –Maintain statistics and present a view of the field. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 Example of the visualization Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15 Time for Play Lets do Ex 10.1 to 10.9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

16 A Rabbit’s state Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Rabbit { Static fields omitted. // Individual characteristics (instance fields). // The rabbit's age. private int age; // Whether the rabbit is alive or not. private boolean alive; // The rabbit's position private Location location; // The field occupied private Field field; Methods omitted. }

17 A Rabbit’s behavior Managed from the run method. Age incremented at each simulation ‘step’. –A rabbit could die at this point. Rabbits that are old enough might breed at each step. –New rabbits could be born at this point. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 Rabbit simplifications Rabbits do not have different genders. –In effect, all are female. The same rabbit could breed at every step. All rabbits die at the same age. Others? Ex – 12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19 A Fox’s state Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Fox { Static fields omitted // The fox's age. private int age; // Whether the fox is alive or not. private boolean alive; // The fox's position private Location location; // The field occupied private Field field; // The fox's food level, which is increased // by eating rabbits. private int foodLevel; Methods omitted. }

20 A Fox’s behavior Managed from the hunt method. Foxes also age and breed. They become hungry. They hunt for food in adjacent locations. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 Configuration of foxes Similar simplifications to rabbits. Hunting and eating could be modeled in many different ways. –Should food level be additive? –Is a hungry fox more or less likely to hunt? Are simplifications ever acceptable? Ex to Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 The Simulator class Three key components: –Setup in the constructor. –The populate method. Each animal is given a random starting age. –The simulateOneStep method. Iterates over separate populations of foxes and rabbits. Two Field objects are used: field and updatedField. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23 The update step Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for(Iterator it = rabbits.iterator(); it.hasNext(); ) { Rabbit rabbit = it.next(); rabbit.run(newRabbits); if(! rabbit.isAlive()) { it.remove(); } … for(Iterator it = foxes.iterator(); it.hasNext(); ) { Fox fox = it.next(); fox.hunt(newFoxes); if(! fox.isAlive()) { it.remove(); }

24 Room for improvement Fox and Rabbit have strong similarities but do not have a common superclass. The update step involves similar- looking code. The Simulator is tightly coupled to specific classes. –It ‘knows’ a lot about the behavior of foxes and rabbits. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

25 The Animal superclass Place common fields in Animal : –age, alive, location Method renaming to support information hiding: –run and hunt become act. Simulator can now be significantly decoupled. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 Revised (decoupled) iteration Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling for(Iterator it = animals.iterator(); it.hasNext(); ) { Animal animal = iter.next(); animal.act(newAnimals); // Remove dead animals from simulation if(! animal.isAlive()) { it.remove(); }

27 The act method of Animal Static type checking requires an act method in Animal. There is no obvious shared implementation. Define act as abstract: abstract public void act(List newAnimals); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

28 Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. Abstract methods make the class abstract. Abstract classes cannot be instantiated. Concrete subclasses complete the implementation. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

29 The Animal class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(List newAnimals); other methods omitted }

30 Further abstraction Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

31 Selective drawing (multiple inheritance) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

32 Multiple inheritance Having a class inherit directly from multiple ancestors. Each language has its own rules. –How to resolve competing definitions? Java forbids it for classes. Java permits it for interfaces. –No competing implementation. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

33 An Actor interface Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public interface Actor { /** * Perform the actor's regular behavior. newActors A list for storing newly created * actors. */ void act(List newActors); /** * Is the actor still active? true if still active, false if not. */ boolean isActive(); }

34 Classes implement an interface Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Fox extends Animal implements Drawable {... } public class Hunter implements Actor, Drawable {... }

35 Interfaces as types Implementing classes do not inherit code, but implementing classes are subtypes of the interface type. So, polymorphism is available with interfaces as well as classes. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

36 Features of interfaces All methods are abstract. There are no constructors. All methods are public. All fields are public, static and final. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

37 Interfaces as specifications Strong separation of functionality from implementation. –Though parameter and return types are mandated. Clients interact independently of the implementation. –But clients can choose from alternative implementations. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

38 Alternative implementations Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

39 The Class class A Class object is returned by getClass() in Object. The.class suffix provides a Class object: Fox.class Used in SimulatorView : Map colors; String getName() for the class name. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

40 Review Inheritance can provide shared implementation. –Concrete and abstract classes. Inheritance provides shared type information. –Classes and interfaces. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

41 Review Abstract methods allow static type checking without requiring implementation. Abstract classes function as incomplete superclasses. –No instances. Abstract classes support polymorphism. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

42 Review Interfaces provide specification without implementation. –Interfaces are fully abstract. Interfaces support polymorphism. Java interfaces support multiple inheritance. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling