COMP 114 Kimberly Noonan Chris VanderKnyff William Luebke.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Whats your favorite color? a pear a green pear.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Java Review Interface, Casting, Generics, Iterator.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
COMP 114 Chris VanderKnyff Kimberly Noonan William Luebke.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
COMP 114 Kimberly Noonan (040) Chris Vanderknyff (040) William Luebke (039)
COMP 114 Recitation Kimberly Noonan Chris VanderKnyff William Luebke.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Control Structures if else do while continue break switch case return for.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
COMP 114 Chris VanderKnyff Kimberly Noonan William Luebke.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
CompSci Classwork today  Back to bouncing balls  Create a new BouncingSmiley using inheritence  Create an Arraylist of bouncing balls and smileys.
Interfaces and Polymorphism CS 162 (Summer 2009).
A: A: double “4” A: “34” 4.
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Inheritance, Polymorphism and Abstract Classes. Student Management System All students are CUNY Students CUNY Students are Queens College students, or.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
using System; namespace Demo01 { class Program
Interfaces.
Chapter 11 Inheritance and Polymorphism
Lecture 2: Data Types, Variables, Operators, and Expressions
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Lecture 17: Polymorphism (Part II)
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Subtyping Rules David Evans cs205: engineering software BlackBear
Review Operation Bingo
More inheritance, Abstract Classes and Interfaces
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Interfaces and Constructors
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
Inheritance.
Announcements & Review
JavaScript Reserved Words
class PrintOnetoTen { public static void main(String args[]) {
Module 2 - Part 1 Variables, Assignment, and Data Types
Lecture 18: Polymorphism (Part II)
Chapter 11 Inheritance and Polymorphism Part 2
Object Oriented Programming.
Software Design Lecture : 39.
Agenda Types and identifiers Practice Assignment Keywords in Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

COMP 114 Kimberly Noonan Chris VanderKnyff William Luebke

What we’re doing today… ► Questions from Lecture  Abstract classes/methods  Inheritance  Polymorphism / Overloading / Dynamic Dispatch ► Assignment 3 Rubric ► Avoiding using the “instanceof” keyword

Assignment 3 Rubric

Comparing Apples and Oranges… abstract class Fruit { public Fruit() {} public abstract String getColor(); public abstract String getName(); }

Apples: class Apple extends Fruit { public Apple() {} public String getColor() { return "Red"; } public String getName() { return "Apple"; }};

Oranges: class Orange extends Fruit { public Orange() {} public String getColor() { return "Orange"; } public String getName() { return "Orange"; }};

Pears: class Pear extends Fruit { public Pear() {} public String getColor() { return "Green"; } public String getName() { return "Pear"; }};

Shopping for Fruit… public static void main(String args[]) { Random random=new Random(); ArrayList al=new ArrayList(); final int MAX_FRUIT=20; for (int i=0;i<MAX_FRUIT;i++) { int r=random.nextInt(3); switch (r) { case 0: { al.add(new Apple()); } break; case 1: { al.add(new Orange()); } break; case 2: { al.add(new Pear()); } break; }} // More on next slide...

We don’t like Oranges… for (int i=0;i<al.size();i++) { Fruit f=(Fruit)al.get(i); // We want to do something different for oranges... if (f instanceof Orange) // Why is this bad? { Orange orange=(Orange)f; System.out.println("What a terrible “ +orange.getName()+"! “+ “I'm done looking at fruit “+ “I'm done looking at fruit “+ “for today."); break;}else{ System.out.println("What a delicious “+ f.getColor()+" "+f.getName()+"..."); }}}}

ScreenShot

Revised Fruit… abstract class Fruit { public Fruit() {} public abstract String getColor(); public abstract String getName(); public boolean isTolerable() // (?) { return true; }}

Revised Orange: class Orange extends Fruit { public Orange() {} public String getColor() { return "Orange"; } public String getName() { return "Orange"; } public boolean isTolerable() { return false; }};

Revised FruitExample: for (int i=0;i<al.size();i++) { Fruit f=(Fruit)al.get(i); // What we’re really doing is doing something different // for intolerable fruit, not just oranges... if (f.isTolerable()) { System.out.println("What a terrible "+ f.getName()+ "! I'm done looking at fruit for today."); break;}else{ System.out.println("What a delicious "+ f.getColor()+" "+f.getName()+"..."); }}}}