Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.

Slides:



Advertisements
Similar presentations
Inheritance Writing and using Classes effectively.
Advertisements

Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Week 11: Class Design 1.  Most classes are meant to be used more than once  This means that you have to think about what will be helpful for future.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS503: Fourth Lecture, Fall 2008 Advanced OOP and Lab. Michael Barnathan.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Computer Science I Inheritance Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
CSC 142 Computer Science II Zhen Jiang West Chester University
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
Peyman Dodangeh Sharif University of Technology Fall 2014.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Week 14 - Monday.  What did we talk about last time?  Heaps  Priority queues  Heapsort.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CRE Programming Club Class 2 (Import JJZ543 and Practice Your Typing!)
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Advanced Programming in Java
Lecture 12 Inheritance.
Week 14 - Monday CS 121.
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

Week 13 - Wednesday

 What did we talk about last time?  Color representation  Color class  Picture class

 Straightforward idea  Flip an image around the y-axis  Maybe you want to decipher some of Leonardo’s writings  No, the other one

 Given an image with width w and height h:  Moving from left to right in the original image, copy each column, storing each column from right to left in the new image

012 0 A 1 B 2 C 3 D A 1 B 2 C 3 D OriginalMirrored

 What would the code for mirroring look like? Picture picture = new Picture( file ); //the picture to be mirrored Picture mirrored = new Picture( picture.width(), picture.height() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) mirrored.set( picture.width() - i - 1, j, picture.get( i, j ) ); Picture picture = new Picture( file ); //the picture to be mirrored Picture mirrored = new Picture( picture.width(), picture.height() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) mirrored.set( picture.width() - i - 1, j, picture.get( i, j ) );

 Pretty much the same thing  Instead of copying each column in reverse order, copy each row in reverse order

 Another straightforward idea  Necessary if you are writing software for a digital camera (the user might turn the camera for portrait instead of landscape)

 Given an image with width w and height h:  Create a new image with width h and height w  Copy each column in the original image into a row in the new image, remembering to move back along the row

012 0 A 1 B 2 C 3 D DCBA 1 2 Original Rotated

 What would the code for a rotation look like? Picture picture = new Picture( file ); //the picture to be rotated Picture rotated = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) rotated.set( picture.height() - j - 1, i, picture.get( i, j ) ); Picture picture = new Picture( file ); //the picture to be rotated Picture rotated = new Picture( picture.height(), picture.width() ); for( int i = 0; i < picture.width(); i++ ) for( int j = 0; j < picture.height(); j++ ) rotated.set( picture.height() - j - 1, i, picture.get( i, j ) );

 Rotating 180°, 270°, -90°, -180°, or -270° can be done using similar techniques or simply performing right rotations multiple times  Rotations that are not multiples of 90° are much trickier, result in non-rectangular final images, and need some trigonometry  Don’t worry about rotations that are not multiples of 90°

 You’ve seen this many times before  Sometimes an image won’t fit nicely in on a PowerPoint slide

 Let's just focus on growing, because that's what you need to do in your project  If we are just doubling the image, we create a new image whose width and height are twice the original  Then, we go through the new image, copying the pixels from the original using pixels whose column and row are half as big as the ones we are putting into the new image

 Doubling an image  Each old pixels maps to four new ones AABB 1 AABB 2 CCDD 3 CCDD 01 0 AB 1 CD

 This kind of resize works, but is crude  To shrinking an image, a clever resize might average together pixels  When growing an image, different techniques can be done to fill in “guesses” for pixels that sit between pixels from the original image, instead of just duplicating them  People who program Photoshop have thought long and hard about how to do these tasks better

 The idea of inheritance is to take one class and generate a child class  This child class has everything that the parent class has (members and methods)  But, you can also add more functionality to the child  The child can be considered to be a specialized version of the parent

 The key idea behind inheritance is safe code reuse  You can use old code that was designed to, say, sort lists of Vehicle s, and apply that code to lists of Car s  All that you have to do is make sure that Car is a subclass (or child class) of Vehicle

 Java respects the subclass relationship  If you have a Vehicle reference, you can store a Car object in that reference  A subclass (in this case a Car ) is a more specific version of the superclass ( Vehicle )  For this reason, you can use a Car anywhere you can use a Vehicle  You cannot use a Vehicle anywhere you would use a Car

 As long as Car is a subclass of Vehicle, we can store a Car in a Vehicle reference  Even in an array is fine  Storing a Vehicle into a Car doesn’t work Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Car c = new Vehicle(); // gives error

 All this is well and good, but how do you actually create a subclass?  Let’s start by writing the Vehicle class public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); } public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); }

 We use the extends keyword to create a subclass from a superclass  A Car can do everything that a Vehicle can, plus more public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); } public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); }

 There is a part of the Car class that knows all the Vehicle members and methods Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City" Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City"

 Each Car object actually has a Vehicle object buried inside of it  If code tries to call a method that isn’t found in the Car class, it will look deeper and see if it is in the Vehicle class  The outermost method will always be called Car model getModel() startEngine() Car model getModel() startEngine() Vehicle travel() Vehicle travel()

 Sometimes you want to do more than add  You want to change a method to do something different  You can write a method in a child class that has the same name as a method in a parent class  The child version of the method will always get called  This is called overriding a method

 We can define the Mammal class as follows: public class Mammal { public void makeNoise() { System.out.println("Grunt!"); } public class Mammal { public void makeNoise() { System.out.println("Grunt!"); }

 From there, we can define the Dog, Cat, and Human subclasses, overriding the makeNoise() method appropriately public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } }

 Inheritance examples  Lab 13

 Start working on Project 5