1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.

Slides:



Advertisements
Similar presentations
1 Note: Original slides provided by and modified for this specific classwww.apComputerScience.com.
Advertisements

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Reusable Classes.  Motivation: Write less code!
Chapter 3 Extending the Robot Programming Language.
1 Inheritance in Java Behind the scenes: new Objects from old.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
CPSC150 Abstract Classes and Interfaces Chapter 10.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
LECTURE 07 Programming using C# Inheritance
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Multiple Choice Solutions True/False a c b e d   T F.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Chapter 5 Conditionally Executing Instructions
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Programming in Java CSCI-2220 Object Oriented Programming.
1 Note: Original slides provided by and modified for Mr. Heath’s AP Computer Science A classwww.apComputerScience.com.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 Ch Object References a.k.a. variables Teams of Robots (e.g.) –Could have 1 robot harvest 6 rows (we’ve seen that) –Could have 3 robots each.
1 Note: Original slides provided by and modified for Mr. Smith ’ s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
OOP Basics Classes & Methods (c) IDMS/SQL News
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Chapter 3 Extending the Robot Programming Language.
Programming in Java: lecture 7
Sections Inheritance and Abstract Classes
Karel J Robot.
karel_part4_functions_2
Polymorphism and Observers
Karel – Primitive Instructions
Karel J Robot Chapter 4 B.
Section 11.1 Class Variables and Methods
Polymorphism Simple but profound!.
Karel J Robot OOP approach to learning computer science
METHOD OVERRIDING in JAVA
Karel J Robot OOP approach to learning computer science
Unit 1 Lab16.
Object References a.k.a. variables
Inheritance and Polymorphism
Presentation transcript:

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4

2 Sometimes we want to do several tasks, but the tasks are very similar. How can we build the classes to take advantage of the common parts of the task and yet distinguish the specific differences? Another way to say that is, how can we design the inheritance tree so that we don’t duplicate common code used among sub-classes, yet allow sub-classes to have some specific differences? The answer is: use an abstract class…

3 Here is a task for a team of robots. We want to lay down beepers in a 5-by-4 field. The odd-numbered rows have 2 beepers per street corner, the even have 3.

4 Here is how we’d organize that with what we currently know: UrRobot TwoRowLayerThreeRowLayer layBeepers() putBeepers() discuss problems with design

5 On the previous slide, we saw that layBeepers() would have the exact same implementation for both types of beeper layers - namely: public void layBeepers() { move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); } discuss why code duplication (a.k.a., copy/paste) and lack of localization are poor/costly design patterns

6 At the same time, we saw that putBeepers() would have a different implementation in each of the subclasses (one puts 2, the other puts 3). So here is the new design pattern: We’ll extract out an abstract concept of what a general beeper layer would look like and put that into a class (in this case, an abstract class). Methods in the abstract class that have the exact same implementation regardless of the subclass will be implemented in the abstract class. Methods that would have different implementations in the subclasses will not be implemented in the abstract class, forcing each subclass to give its own unique implementation…

7 TwoRowLayer public void putBeepers() { … } UrRobot BeeperLayer public void layBeepers() { … } public abstract void putBeepers(); ThreeRowLayer public void putBeepers() { … }

8 public abstract class BeeperLayer extends UrRobot { public BeeperLayer(int street, int avenue, Direction direction, int beepers) { super(street, avenue, direction, beepers); } // The following abstract method will be used by TwoRowLayer and ThreeRowLayer public abstract void putBeepers(); public void layBeepers() { move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); } public class TwoRowLayer extends BeeperLayer { public TwoRowLayer( int st, int av, Direction dir, int beeps) { super(st, av, dir, beeps); } public void putBeepers() { putBeeper(); }

9 public class BeeperLayerTester implements Directions { public static void main(String[] args) { BeeperLayer lisa; lisa = new TwoRowLayer(1, 3,East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(2, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(3, 3, East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(4, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(5, 3, East, infinity); lisa.layBeepers(); } } abstraction, abstract class, abstract method, polymorphism making references to the code, the inheritance tree, or whatever else we just discussed in the BeeperLayer problem, pick one of these terms and demonstrate that you know what it means

10 Abstraction - Mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time (i.e. abstracting common code and putting it into a method) Abstract class – Special class used to provide common code used among sub-classes, yet allow sub-classes to have some specific differences. It is not possible to create an instance of an abstract class (it must be extended and then that class can be instantiated). An abstract class that only contains abstract methods is an interface (we’ll learn about that later). Abstract method – Method in an abstract class that must be redefined in a class that extends the abstract class. Polymorphism – Objects in different classes can behave differently when sent the same message. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.

11 Initialize – Set the value of a variable to a specific value For example: karel = new UrRobot(1, 2, North, 0); Assignment – Used to change the object to which an reference points. Use the equal sign (=) to denote assignment. Reference (or Variable) – name used to identify a field (i.e. the robot name “karel” is a reference or variable). Instance Variable (or Field) – The things that an object remembers. These are the variables defined in the object class. null – Special value to indicate that the reference to the variable does not refer to anything at all (i.e. UrRobot karel = null). Parameter – Information sent to a method. When we instantiate a new robot object, we are passing 4 different parameters which are used by the constructor method. Parameters can be of any type.