More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Similar documents
Inheritance and objects

Java Object Oriented Design. CSC207 Fall 2014

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Inheritance and objects

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

INHERITANCE AND OBJECTS. Fundamentals of Computer Science I

CS-202 Introduction to Object Oriented Programming

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Arrays Classes & Methods, Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Object Oriented Programming. Java-Lecture 11 Polymorphism

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

Inheritance and Polymorphism

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Chapter 14 Abstract Classes and Interfaces

Inheritance (Part 2) Notes Chapter 6

Polymorphism CSCI 201 Principles of Software Development

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

JAVA MOCK TEST JAVA MOCK TEST II

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Topic 7: Algebraic Data Types

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CS 251 Intermediate Programming Inheritance

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

CH. 2 OBJECT-ORIENTED PROGRAMMING

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Chapter 10 Classes Continued. Fundamentals of Java

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

OOP Subtyping. Lecture 20

Design Patterns: State, Bridge, Visitor

Data Structures and Other Objects Using C++

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CST242 Object-Oriented Programming Page 1

Making New instances of Classes

CMSC 132: Object-Oriented Programming II

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance, cont. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance, and Polymorphism.

Inheritance -- Introduction

CSE 143 Lecture 20. Circle

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance and Interfaces

Introduction to Programming Using Java (98-388)

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Inheritance and Interfaces

ITI Introduction to Computing II

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Introduction to Inheritance

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance, Polymorphism, and Interfaces

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CS 177 Recitation. Week 8 Methods

Chapter 6 Introduction to Defining Classes

Inheritance. OOP: Inheritance 1

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

ITI Introduction to Computing II

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Inheritance and Polymorphism

Inheritance (Part 5) Odds and ends

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

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

9/19/2018 Programming Data Structures. Polymorphism And Abstract

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Computer Science II (20073) Week 1: Review and Inheritance

Input and output thus far. Graphics and sound Input. New input/output capabilities. Output. StdDraw overview. StdDraw. StdAudio

Polymorphism and Inheritance

Classes and objects. Chapter 2: Head First Java: 2 nd Edi4on, K. Sierra, B. Bates

INHERITANCE. Spring 2019

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

1.00 Lecture 14. Exercise: Plants

CS 520 Theory and Practice of Software Engineering Fall 2018

Object-oriented Programming. Object-oriented Programming

Reviewing OO Concepts

Fundamentals of Object Oriented Programming

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Method Overriding in Java

Reusing Classes. Hendrik Speleers

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Java Fundamentals (II)

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Transcription:

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Object hierarchies Overview Several classes inheriting from same base class Concrete versus abstract classes Programming activity Finish class hierarchy for drawing shapes Filled circles Filled rectangles... 2

A tile game Goal: Design classes for use in a tile game Played on a square N x N grid Each grid location can have one thing: Square letter tile Circular number tile Some other rules TBD 3

Designing the Tile game Use a 2D array for the N x N grid Array element null if no tile there Otherwise reference to letter/number tile object Start by randomly placing non-overlapping tiles final int GRID = 8; Tile [][] tiles = new Tile[GRID][GRID]; null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null 4

Single class design? Tile private double size private char ch Tile(double size, char ch) char getcharacter() double getsize() void draw(double x, double y) How big to draw ourselves (a Tile object doesn't know the number of grid cells or screen canvas size). The character appearing on this Tile. Draw ourselves, somebody tells us our center (x,y) location. Problem: draw() has to check character to know how to draw itself Any method whose behavior depends on tile type needs similar conditional logic 5

Tile class hierarchy Tile private double size private char ch Tile(double size, char ch) char getcharacter() double getsize() void draw(double x, double y) LetterTile extends Tile LetterTile(double size) void draw(double x, double y) NumberTile extends Tile NumberTile(double size) void draw(double x, double y) Construct using a random letter A-Z Draw a square tile Construct using a random number 0-9 Draw a circle tile 6

Terminology: Concrete vs. Abstract class Concrete class Some classes make sense to create e.g. LetterTile, NumberTile Abstract class Some classes don't make sense to create Exist so children can inherit things Exist so related objects can live in same array e.g. Tile 7

Prevents anyone from creating a Tile object Child classes will get these methods for free Abstract Tile class public abstract class Tile private double size = 0.0; private char ch = '\0'; public Tile(double size, char ch) this.size = size; this.ch = ch; public char getcharacter() return ch; public double getsize() return size; public abstract void draw(double x, double y); All child classes must implement a method called draw with exactly this signature. This is what makes the polymorphism work (i.e. we can put any child of Tile into the same array and call draw() on any element). 8

Concrete LetterTile class Randomly assigns a letter between A and Z public class LetterTile extends Tile public LetterTile(double size) super(size, (char) StdRandom.uniform((int) 'A', (int) 'Z' + 1)); Since we extend Tile, we must implement all abstract methods declared in Tile public void draw(double x, double y) StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledRectangle(x, y, getsize() / 2.0, getsize() / 2.0); StdDraw.setPenColor(StdDraw.WHITE); StdDraw.text(x, y, "" + getcharacter()); 9

Concrete NumberTile class public class NumberTile extends Tile public NumberTile(double size) super(size, (char) StdRandom.uniform((int) '0', (int) '9' + 1)); public void draw(double x, double y) StdDraw.setPenColor(StdDraw.RED); StdDraw.filledCircle(x, y, getsize() / 2.0); Randomly assign a letter between 0 and 9 Since we extend Tile, we must implement all abstract methods declared in Tile StdDraw.setPenColor(StdDraw.WHITE); StdDraw.text(x, y, "" + getcharacter()); 10

TileBoard class Manage the N x N grid inside another class Create for a given number of tiles, grid size, and canvas size Draw itself TileBoard private Tile [][] tiles // 2D array storing LetterTile and NumberTile objs private double size // Size of tiles in StdDraw coordinates TileBoard(int numtiles, int gridsize, double canvassize) void draw() 11

TileBoard constructor public TileBoard(int numtiles, int gridsize, double canvassize) tiles = new Tile[numTiles][numTiles]; size = canvassize / gridsize; int added = 0; // Keep adding tiles until we reach the target number or board limit while ((added < numtiles) && (added < gridsize * gridsize)) // Choose a random (x, y) grid location for the next tile int x = (int) (Math.random() * gridsize); int y = (int) (Math.random() * gridsize); if (tiles[x][y] == null) // Randomly choose a letter or number tile if (Math.random() < 0.5) tiles[x][y] = new LetterTile(size); else tiles[x][y] = new NumberTile(size); added++; 12

TileBoard drawing public void draw() StdDraw.setFont(new Font("SansSerif", Font.BOLD, 18)); // Loop over all the x-locations in the grid for (int x = 0; x < tiles.length; x++) // Loop over all the y-locations in this x row for (int y = 0; y < tiles[x].length; y++) // Only draw if the grid location contains a tile if (tiles[x][y]!= null) tiles[x][y].draw(size * (x + 0.5), size * (y + 0.5)); 13

TileGame main program public class TileGame public static void main(string [] args) TileBoard board = new TileBoard(Integer.parseInt(args[0]), Integer.parseInt(args[1]), 1.0); board.draw(); % java TileGame 10 4 % java TileGame 30 8 % java TileGame 200 16 14

Goal: Class hierarchy representing shapes Programming activity Shapes can calculate area Shapes can draw themselves Circle @ (0.50, 0.50), radius = 0.10, area 0.0314, distance from origin 0.7071 Circle @ (0.30, 0.30), radius = 0.20, area 0.1257, distance from origin 0.4243 Circle @ (0.70, 0.80), radius = 0.05, area 0.0079, distance from origin 1.0630 Rectangle @ (0.65, 0.15) [0.30 x 0.20], area 0.0600, distance from origin 0.6671 distance from Circle @ (0.50, 0.50), radius = 0.10 to Circle @ (0.30, 0.30), radius = 0.20, 0.2828 distance from Circle @ (0.50, 0.50), radius = 0.10 to Circle @ (0.70, 0.80), radius = 0.05, 0.3606 distance from Circle @ (0.50, 0.50), radius = 0.10 to Rectangle @ (0.65, 0.15) [0.30 x 0.20], 0.3808 distance from Circle @ (0.30, 0.30), radius = 0.20 to Circle @ (0.70, 0.80), radius = 0.05, 0.6403 distance from Circle @ (0.30, 0.30), radius = 0.20 to Rectangle @ (0.65, 0.15) [0.30 x 0.20], 0.3808 distance from Circle @ (0.70, 0.80), radius = 0.05 to Rectangle @ (0.65, 0.15) [0.30 x 0.20], 0.6519 15

Summary Designed classes for a tile game Abstract base class Concrete classes for letter and number tiles Object to manage the entire game board Programming activity Implemented subclasses of an abstract base class Overloaded methods: distance() Overridden methods: tostring() 16