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

Slides:



Advertisements
Similar presentations
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Advertisements

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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
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.
Color Correct and Remove Keystoning A minimalist approach to photographing your art By Paul Marley.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
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.
CS107 References and Arrays By Chris Pable Spring 2009.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Week 15 – Friday.  Student Questions  Review up to Exam 2  Loops  Arrays  StdDraw  StdAudio  Static methods.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
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.
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.
Advanced Programming in Java
Lecture 12 Inheritance.
Inheritance and Polymorphism
Week 14 - Monday CS 121.
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 Carrano Chapter 10 Small Java
PowerPoint Challenge 4 /20
Presentation transcript:

Week 13 - Wednesday

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

 Because JPEGs and PNGs are complex file types using compression, the authors of the old textbook provided us with the Picture class  It’s an easy interface for doing routine things with an image  Loading/saving an image  Getting the height and width of an image  Changing the pixels of an image  Showing the image

 Everything you’d want a picture to do: MethodUse Picture(String file) Creates a Picture from a file Picture(int w, int h) Create a blank Picture with width w and height h int width() Return the width of the image int height() Return the height of the image Color get(int x, int y) Return the Color of the pixel at ( x, y ) void set(int x, int y, Color c) Set the Color of the pixel at ( x, y ) to c void show() Display the image void save(String file) Save the Picture to a file

 Write code that will read user input for width and height  Make an image that is completely filled with the color blue with the specified width and height  Read in the name of an image file from the user  Darken the colors in it by 50%

 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