CS112 Lecture: Extending Classes and Defining Methods

Similar documents
CS112 Lecture: Introduction to Karel J. Robot

We know have to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view.

We know how to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view.

CS112 Lecture: Defining Instantiable Classes

Numeric answers: select letters to total to your answer. For example 3 = d and e, 11 = b, d and e & 17 = a and e. a) 16 b) 8 c) 4 d) 2 e) 1

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Laboratory 1: Eclipse and Karel the Robot

CS211 Lecture: Relationships Between Classes: Dependency, Inheritance, and Realization

1. BlueJ bank example with subclasses of BankAccount 2. Transparency of UML diagram for BankAccount class hierarchy

CS112 Lecture: Working with Numbers

Mehran Sahami Handout #7 CS 106A September 24, 2014

CS 106A, Lecture 3 Problem-solving with Karel

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Using Karel with Eclipse

CS 106A, Lecture 3 Problem-solving with Karel

CS112 Lecture: Repetition Statements

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CSE 143 Lecture 12 Inheritance

CS112 Lecture: Loops

Using Eclipse and Karel

CPS122 Lecture: Defining a Class

CPS122 Lecture: Detailed Design and Implementation

CPS122 Lecture: Detailed Design and Implementation

IS12 - Introduction to Programming

Inheritance (Outsource: )

Slide 1 CS 170 Java Programming 1 Testing Karel

PreTest.doc. That was so much fun, lets do it again :^) Name Period. 1 Beeper. 25. How many Beepers are in Karel BeeperBag?

CS112 Lecture: Making Choices

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CS111: PROGRAMMING LANGUAGE II

Inheritance and Polymorphism

BIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett

Introduction to Java

Asking For Help. BIT 115: Introduction To Programming 1

Inheritance, Polymorphism, and Interfaces

Developing and Reusing Methods

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object-Oriented Programming

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Java Object Oriented Design. CSC207 Fall 2014

CS-202 Introduction to Object Oriented Programming

CS112 Lecture: Inheritance and Polymorphism

Programming in Karel. Eric Roberts and Jerry Cain CS 106J April 5, 2017

Before we start: Any questions on Karel?

Software Development 2

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

5.6.1 The Special Variable this

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

C++ Important Questions with Answers

Polymorphism and Inheritance

Practice Midterm Examination #1

CS111: PROGRAMMING LANGUAGE II

16 Multiple Inheritance and Extending ADTs

CS211 Lecture: Design Quality; Cohesion and Coupling; Packages

Self-review Questions

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

More on Objects in JAVA TM

CS112 Lecture: Primitive Types, Operators, Strings

CMSC 132: Object-Oriented Programming II

Programming II (CS300)

Arrays Classes & Methods, Inheritance

CS111: PROGRAMMING LANGUAGE II

Debugging with Karel

Lecture 18 CSE11 Fall 2013 Inheritance

IS12 - Introduction to Programming. Lecture 6: Advanced Programming with Karel. Peter Brusilovsky

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

printf( Please enter another number: ); scanf( %d, &num2);

CS121/IS223. Object Reference Variables. Dr Olly Gotel

AP CS A Karel J Robot Review Sheet Chapters 5-6

COMP 110/L Lecture 19. Kyle Dewey

CSE 341, Autumn 2015, Ruby Introduction Summary

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

(Refer Slide Time: 1:27)

INHERITANCE AND EXTENDING CLASSES

OBJECT ORİENTATİON ENCAPSULATİON

Specification Load Mountain.java and study its method explore(). Complete the Exercises on the next page.

(Refer Slide Time: 00:23)

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

Introduction to Computer Programming for Non-Majors

COMP 250 Fall inheritance Nov. 17, 2017

Introduction to Computer Programming for Non-Majors

THE ADHERENCE OF OPEN SOURCE JAVA PROGRAMMERS TO STANDARD CODING PRACTICES

Justification for Names and Optional Parameters

Material from Recitation 1

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

COMP19612 exam performance feedback 2014

17 Multiple Inheritance and ADT Extensions

C++ Programming Style Guide

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

PROGRAMMING LANGUAGE 2

CS 1567 Intermediate Programming and System Design Using a Mobile Robot Aibo Lab3 Localization and Path Planning

Transcription:

Objectives: CS112 Lecture: Extending Classes and Defining Methods Last revised 1/9/04 1. To introduce the idea of extending existing classes to add new methods 2. To introduce overriding of inherited methods 3. To introduce stepwise refinement Materials: 1. Transparency of Problem 2.5 plus Problem2_5.world, Problem2_5_Revised.java 2. PlantDiagonal.world, PlantDiagonal1.java, PlantDiagonal2.java 3. Chapter 3 of Karel book to display 4. Transparency of Problem 3.5 from earlier Karel ++ book plus Problem3_5.world,.java I. Defining new Instructions for Robots A. In our first look at our robots, we saw that they are capable of executing just 5 primitive methods: move(), turnleft(), pickbeeper(), putbeeper(), and turnoff(). All robot programs ultimately are built up out of combinations of these methods. B. At the same time, though, we saw that working with just these primitives can be a bit awkward. For example, to turn a robot to the right, we had to code a sequence of three turnleft()s. This is undesirable for two reasons: 1. It involves extra work for the programmer. 2. It makes the program obscure to the human reader. A human would find it easier to understand the program if we could use a turnright() operation instead of three turnleft()s. C. Fortunately, there is a built-in mechanism for expanding the vocabulary of our robots. 1. What we must do is to define a new class of Robot as an extension of an existing class. 2. This new class inherits all the methods of the class it is derived from. 1

3. We then define additional methods for this new class of robot, perhaps utilizing the methods it inherited from the class it is derived from 4. Finally, when we create a new robot using the new operation, we create a robot of our newly-defined class, rather than the base class Robot as we have been doing. EXAMPLE: The newspaper retrieval program, redone using a new class of robot called RightTurnerRobot that has a turnright() method. a) Put up TRANSPARENCY of original problem b) Show code for original problem - note several lines that have three turnleft() instructions in a row. (In Solutions folder for Intro Lecture) c) Show revised code using turnright() instruction (Problem2_5_Revised.java) NOTE: (1) Definition for class RightTurnerRobot (a) extends Robot (b) new turnright() method definition - NOTE: within the method definition we don t explicitly name the robot to whom the turnleft() messages are begin sent - they are being sent to the same robot to whom the turnright() method was sent. (We could make this explicit by using the Java reserved word this) (c) RightTurnerRobot constructor that calls constructor for superclass. (To construct a RightTurnerRobot, in effect we first construct an ordinary Robot and then add the new method to it. Note how starting position etc. is passed on to the super constructor.) (2) We now construct a RightTurnerRobot using new (and declare the variable to be of this type) (3) Use of turnright() method where before we had three turnleft()s d) DEMO compilation - note how two class files are created. e) DEMO run of revised program 2

D. Some Naming Conventions 1. The names of newly defined classes follow the class naming convention - the name begins with an uppercase letter, and each new word in the name is capitalized. 2. Frequently, the name of an extended class includes the name of the original class - hence RightTurnerRobot. (Of course, this has to be done with caution or we end up with an impossibly-long name!) 3. New methods follow the convention of the name beginning with a lowercase letter, and each new word in the name is capitalized - hence turnright(). 4. The name of a new method should clearly indicate to the human reader what the method does. The robot, of course, does not care what name we use - e.g. we could have called our turnright() method foo() or even turnaround() as far as the robot is concerned (but if the definition called for three turnleft()s, the robot would still turn right!) E. An additional note: we placed the definition of RightTurnerRobot in the same file as the main program. More typically, this would have been placed in a separate file to facilitate reuse in other programs. However this would have made the compilation process a bit more complicated, which I wanted to avoid at this point in time. F. Practice: define a new instruction turnaround() that turns the robot to face in the opposite direction from where it is currently facing. II. Using New Methods to Define Additional Method Once a new method has been defined, it in effect becomes a new primitive that can then be used in defining other methods. A. EXAMPLE: Consider the task of having Karel plant a diagonal line of beepers on the corner of 1st St & 1st Ave, 2nd St & 2nd Ave, 3rd St & 3rd Ave.. 10th St & 10th Ave. The program would begin something like this (assuming Karel starts out at 1st & 1st with a bag of 10 beepers, facing east): karel.move(); karel. karel.move(); karel.turnright(); 3

karel.move(); karel. karel.move(); karel.turnright();... B. Life would be simpler if we defined a new method movediagonally() as follows: void movediagonally() { move(); move(); turnright(); } C. Then our main program becomes: karel.movediagonally(); karel.movediagonally(); karel.movediagonally(); karel.movediagonally();... D. There are actually two different ways of doing this job: 1. We could create a new subclass of RightTurnerRobot, perhaps called DiagonalMoverRobot SHOW, DEMO PlantDiagonal1.java with PlantDiagonal.world 2. We could put both turnright() and movediagonally() in the same class (which we would be wiser to call DiagonalMoverRobot() rather than RightTurnerRobot()) SHOW, DEMO PlantDiagonal2.java with PlantDiagonal.world Note that we have used turnright() as part of the definition of movediagonally() in the same class. 4

3. Which approach is better? a) Probably more opportunities for reuse by making RightTurnerRobot its own class - it can be reused in many situations. b) However, making each extension its own class can lead to a proliferation of classes, so we don t want to carry this too far, creating an inheritance hierarchy that is excessively deep. E. Note, again, as mentioned above that we would more typically place each robot class in its own file, which further facilitates reuse. III. Inheriting Methods; Over-Riding Inherited Methods A. Note that, when we extend a class to define new methods, we ordinarily inherit all the methods of the base class we are extending. 1. Example: RightTurnerRobot inherits the methods move(), turnleft(), etc. from Robot. 2. Example: In our first solution to the plant diagonal problem, DiagonalMoverRobot() inherits turnright() from RightTurnerRobot plus all the methods that RightTurnerRobot inherited from Robot. B. On occasion, instead of simply inheriting a method, a subclass may need to redefine the method to either add additional behavior or perhaps change its behavior totally. We call this overriding the method. 1. Really good examples of this are a bit hard to develop in the robot world. The book mentioned one: ASK CLASS Creating a robot class called Mile_Mover that moves a mile (8 blocks) every time it is told to do a move. a) This is done by overriding move to call the original, inherited move 8 times: DISPLAY ch 3 of book and find Mile_Mover section b) Actually, this is a bit questionable, since a method whose effect is wellknown now has surprising behavior. It would be easy to inadvertently write a program that sends a robot into a wall by forgetting that move() now means go 8 blocks. 5

2. A better example of overriding comes from the world of bank accounts we used in our Intro to OO lecture ASK An overdraft protected account might override the behavior of the withdraw method, as follows: a) If the inherited withdraw method allows the withdrawl, fine b) Otherwise, make an overdraft loan. IV. Stepwise Refinement A. We introduced the idea of defining new instructions as a mechanism for simplifying the task of writing robot programs, and for making the resultant programs more readable. It also turns out that this idea is related to a powerful concept for designing complicated programs called STEPWISE REFINEMENT. B. Suppose we are given a fairly large and complex task for Karel to accomplish. It may not be immediately obvious how we are to go about writing a robot program to accomplish the task. However, we often can get a start on the job by breaking it up into smaller parts. For example, consider problem 3.5 in the earlier Karel ++ Book: TRANSPARENCY - Problem 3.5 1. Since the assignment allows us to choose Karel s starting position, let's choose to put him at the bottom left corner of the "H", facing north. Suppose we had available to us procedures for drawing each of the letters of the alphabet, which start with the robot at the lower left corner of the letter and leave it in position to start the next letter, again facing north. Then, our basic task would be fairly simple: karel.drawh(); karel.drawe(); karel.drawl(); karel.drawl(); karel.drawo(); turnoff(); 6

2. Now, what we have done is to convert our original problem into four new problems - defining instructions for drawing each of the letters. Let's consider drawh() first: a) The H obviously consists of three parts: two vertical bars of 5 beepers each, plus a horizontal bar of 2 beepers. b) If we had available to us methods for drawing these bars, then we could define drawh() fairly easily. We will call these bar5() and bar2(). It will be convenient to assume that each method can expect the robot to be on the right square facing the right way at the outset, and will leave it at the other end of the bar facing in the same direction upon completion. c) It will also be helpful to define a method move2() that moves the robot two squares (same as bar2() but doesn t plant beepers). d) Finally, we will make use of turnright() and turnaround() defined previously. e) We can now define drawh as follows: void drawh() { bar5(); turnaround(); move2(); move(); bar2(); move(); move2(); turnaround(); bar5(); move2(); } f) Defining the three methods bar2(), bar5() and move2() is fairly simple: ASK CLASS 7

3. The remaining methods can be defined similarly: ASK CLASS 4. DEMO: Complete program: Problem3_5.world,.java C. The idea behind stepwise refinement is this: 1. Break the original problem into a logical series of steps, assuming, for the moment, that you have code available to perform each of them. 2. Define each of these steps as a new method, and refine each into still smaller steps. 3. Continue the process until all steps are primitives. Where possible, try to develop general-purpose methods that can be used multiple times (e.g. turnright(), bar5(), etc.) D. Note that often the difference between a good solution to a problem and a bad one (in terms of programming effort, elegance) is a good initial refinement. Be willing to spend some time on this. 8