Admin. CS 112 Introduction to Programming. Recap: The Critter Class. Recap: Event-Driven Programming. State Machine Driven Cougar.

Size: px
Start display at page:

Download "Admin. CS 112 Introduction to Programming. Recap: The Critter Class. Recap: Event-Driven Programming. State Machine Driven Cougar."

Transcription

1 Admin CS 112 Introduction to Programming q PS8 and class project questions? q Special session on Google App Engine Monday 8 pm at DL 120 Critters/Event-Driven Programming; Interface Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: yry@cs.yale.edu 2 Recap: The Critter Class Recap: Event-Driven Programming // abstract class means not implement every method public abstract class Critter { public boolean eat() public Attack fight(string opponent) // ROAR, POUNCE, SCRATCH, FORFEIT public Color getcolor() public Direction getmove(string[][] grid) // NORTH, SOUTH, EAST, WEST, CENTER public String tostring() // read the class for other methods available q Key concepts: The simulator is typically implemented using polymorphism The simulator is in control, NOT an animal. An animal must keep state (as fields) so that it can make a single move, and know what moves to make later. We say that EDP focuses on writing the callback functions of objects Next move? % Recap: Cougar State Machine Driven Cougar q Write a critter class Cougar (among the dumbest of all animals): Method constructor public Cougar() eat fight getcolor getmove tostring Always eats. Always roars. Behavior Blue if the Cougar has never fought; red if he has. Walks west until he finds food; then walks east until he finds food; then goes west and repeats. "C" q getmove q Color Move West! Has fought eat() eat() Move East fight() Has fought 6 1

2 Recap: Snake Method Behavior constructor public Snake() eat Never eats fight random pounce or roar getcolor Color(20, 50, 128) getmove 1 E, 1 S; 2 W, 1 S; 3 E, 1 S; 4 W, 1 S; 5 E,... tostring "S" Non-EDP Version A non-event driven version cyclelength = 1; while (true) { steps = 0; while (steps < cyclelength) if cyclelength % 2 == 1 go East else go West steps ++; go South cyclelength ++ Non-EDP-> EDP: Guarding Condition Snake solution A non-event driven version steps < cyclelength import java.awt.*; // for Color public class Snake extends Critter { private int cyclelength; // # steps in curr. Horiz. cycle steps < cyclelength private int steps; // # of cycle's steps already taken cyclelength = 1; while (true) { steps = 0; while (steps < cyclelength) if cyclelength % 2 == 1 go East else go West steps ++; // invariant? go South cyclelength ++ if (cyclelength % 2 == 1) go East else go West steps++; steps == cyclelength go South cyclelength ++ steps=0; 9 public Snake() { cyclelength = 1; steps = 0; public Direction getmove() { if (steps < cyclelength) { steps++; if (cyclelength % 2 == 1) { else { else { steps = 0; return Direction.EAST; return Direction.WEST; cyclelength ++; return Direction.SOUTH; public String tostring() { return "S"; if (cyclelength % 2 == 1) go East else go West steps++; steps == cyclelength Go South cyclelength ++ steps=0; Comment: States Testing critters q Counting is helpful: How many total moves has this animal made? How many times has it eaten? Fought? q Remembering recent actions in fields may be helpful: Which direction did the animal move last? How many times has it moved that way? Did the animal eat the last time it was asked? How many steps has the animal taken since last eating? How many fights has the animal been in since last eating? q Focus on one specific critter of one specific type Only spawn 1 of each animal, for debugging q Make sure your fields update properly Use println statements to see field values q Look at the behavior one step at a time Use "Tick" rather than "Go" 2

3 Designing Bulldog Coordination q Be open minded q Think about strategies, e.g., How much state do your bulldogs keep and probe state? When does your Bulldog eat/mate? Is there an optimal fight strategy for a specific type of opponent? Do your bulldogs play disguise? Does your strategy change with time? Do your bulldogs coordinate their behaviors to form some kind of patterns? 13 Coordination 14 Critter exercise: Hipster q A group of hipster critters want to hangout together q Each hipster can suddenly become inspired and choose a random board location called an edgy bar q A hipster go north until reaches edgy bar s horizontal, then east until reaching the bar 15 Solution 1 (See Hipster.java) Solution 1 (See Hipster.java) public class Hipster extends Critter { private Random rand; private int edgybarx, edgybary; private int nextt, t; private final int FLASH_INTERVAL = 200; public Hipster() { rand = new Random(); public Direction getmove(string[][] grid) { t ++; if (t == nextt) { edgybarx = rand.nextint( getwidth() ); edgybary = rand.nextint( getheight() ); public class Hipster extends Critter { private Random rand; private int edgybarx, edgybary; private int nextt, t; private final int FLASH_INTERVAL = 200; public Hipster() { rand = new Random(); public Direction getmove(string[][] grid) { q Problem: Each t ++; if (t == nextt) { hipster goes edgybarx = rand.nextint( getwidth() ); edgybary = rand.nextint( getheight() ); to a different bar. if (gety()!= edgybary) { return Direction.NORTH; else if (getx()!= edgybarx) { return Direction.EAST; else { return Direction.CENTER; public String tostring() { return "H(" + edgybarx + "," +edgybary + ") ; if (gety()!= edgybary) { return Direction.NORTH; else if (getx()!= edgybarx) { return Direction.EAST; else { return Direction.CENTER; We want all hipsters to share the same bar location. public String tostring() { return "H(" + edgybarx + "," +edgybary + ") ; 3

4 Solution 1 (See Hipster.java) public class Hipster extends Critter { private Random rand; private static int edgybarx, edgybary; private int nextt, t; private final int FLASH_INTERVAL = 200; public Hipster() { rand = new Random(); public Direction getmove(string[][] grid) { t ++; if (t == nextt) { edgybarx = rand.nextint( getwidth() ); edgybary = rand.nextint( getheight() ); if (gety()!= edgybary) { return Direction.NORTH; else if (getx()!= edgybarx) { return Direction.EAST; else { return Direction.CENTER; public String tostring() { return "H(" + edgybarx + "," +edgybary + ") ; Recall: Static members q static: Part of a class, rather than part of an object. Object classes can have static methods and fields. Not copied into each object; shared by all objects of that class. object #1 state: int field1 double field2 behavior: public void method3() public int method4() public void method5() class state: private static int staticfielda private static String staticfieldb behavior: public static void somestaticmethodc() public static void somestaticmethodd() object #2 state: int field1 double field2 behavior: public void method3() public int method4() public void method5() object #3 state: int field1 double field2 behavior: public void method3() public int method4() public void method5() Accessing static fields q From inside the class where the field was declared: fieldname // get the value fieldname = value; // set the value q From another class (if the field is public): ClassName.fieldName // get the value ClassName.fieldName = value; // set the value Example q We want to assign a unique, increasing employee ID for each Employee object (e.g., Secretary, Marketer, Lawyer) that we create generally static fields are not public unless they are final 22 Employee with Static public class Employee { private int empid; private static int nextempid = 1000; empid = nextempid; nextempid++; public static int nextempid() { return nextempid; public class Firm { public static void main(string[] args) { Lawyer larry = new Lawyer( Larry"); Marketer mike = new Marketer("Mike"); Lawyer lynn = new Lawyer( Lynn"); Example: The Employee Objects 1000 Employee.nextEmpID public class Employee { private int empid; private static int nextempid = 1000; emplid = nextempid; nextempid++; 24 4

5 Example: The Employee Objects Example: The Employee Objects larry: Lawyer name = Larry empid = 1000; Employee.nextEmpID larry: Lawyer name = Larry empid = 1000; Employee.nextEmpID public class Employee { private int empid; private static int nextempid = 1000; emplid = nextempid; nextempid++; mike: Marketer name = Mike empid = 1001; public class Employee { private int empid; private static int nextempid = 1000; emplid = nextempid; nextempid++; Lawyer larry = new Lawyer("Larry"); Marketer mike = new Marketer("Mike"); Back to Hipster Exercise public class Hipster extends Critter { Random rand; private static int edgybarx, edgybary; private int nextt, t; public Hipster() { rand = new Random(); t = 0; nextt = rand.nextint(200); public Direction getmove(string[][] grid) { t ++; if (t == nextt) { edgybarx = rand.nextint(60); edgybary = rand.nextint(50); t = 0; nextt = rand.nextint(200); q Design a type of critters that can conduct formation, e.g., move around a circle with equal distance (enough to earn you full 20 points of Part 2/PS8) if (gety()!= edgybary) { return Direction.NORTH; else if (getx()!= edgybarx) { return Direction.EAST; else { return Direction.CENTER; public String tostring() { return "H(" + edgybarx + "," +edgybary + ") ; Comment: PS8 Development Strategy Summary: Polymorphism q Do one species at a time in ABC order from easier to harder debug printlns q polymorphism: Ability for the same code to be used with different types of objects and behave differently with each. q Simulator helps you debug smaller width/height fewer animals "Tick" instead of "Go" "Debug" checkbox drag/drop to move animals System.out.println can print any type of object. Each one displays in its own way on the console. CritterMain can interact with any type of critter. Each one moves, fights, etc. in its own way. 5

6 Polymorphic Array: Generic Programming Critter[] critters = { new Ant(), new Cougar(), new Snake(), new Bulldog() index while (true) { for (i=0; i<critters.length; i++) pos = critters[i].getmove(); disp = critters[i].tostring(); draw disp at pos Not dependent on any specific critters but only the generic Critter concept Single vs. Multiple Inheritance q Some object-oriented languages allow multiple inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents q The price: collisions, such as the same variable name, same method name in two parents, have to be resolved q Java design decision: single inheritance, meaning that a derived class can have only one parent class q But many concepts in real-life do have multiple perspectives 32 Motivating Example: Sorting Motivating Example: Sorting Employees public static void insertionsort(int[] elems) { int key = elems[index]; public static void insertionsort(int[] elems) { int key = elems[index]; && elems[insertpos - 1] > key) { // end of for // end of insertionsort 33 && elems[insertpos - 1] > key) { // end of for // end of insertionsort Goal: design a sorting method to sort Employee objects by name. Q: Which line(s) of sorting depend on int instead of Employee 34 Motivating Example: Sorting Employees Motivating Example: Sorting Persons public static void insertionsort(employee[] elems) { Employee key = elems[index]; && elems[insertpos - 1].compareTo(key) >0 ) { // end of for // end of insertionsort public class Employee { int compareto(employee other) { return name.compareto(other.name); 35 public static void insertionsort(person[] elems) { Person key = elems[index]; && elems[insertpos - 1].compareTo(key) >0 ) { // end of for // end of insertionsort public class Person { private int age; int compareto(person other) { return age other.age; 36 6

7 Applying Sorting to General Class X public static void insertionsort(x[] elems) { X key = elems[index]; && elems[insertpos - 1].compareTo(key) >0 ) { // end of for // end of insertionsort Applies to any class X that has implemented the compareto method Summary q The sort method can be applied to any class X that implements the compareto method that we used when defining the sort method. q Q: Implement using already covered technique Define a base class X public class X { public int compareto(x other); Define that Employee, Person, extends X and overrides compareto Problem: An Employee/Person conceptually really is not an X. X is just an abstract property Interface Outline q An interface provides an abstraction to write reusable, general programs q Instead of writing a program for a single class of objects, we want to write a program to handle all classes with a given set of behaviors/properties An interface is an abstraction for the common behaviors of these behaviors q Often interface represents abstract concepts q Admin and recap q Interface Motivation Syntax Interfaces Inheritance vs Interface q interface: A list of methods that classes can promise to implement. Analogous to non-programming idea of roles or certifications "I'm certified as a CPA accountant. The certification assures you that I know how to do taxes, perform audits, and do management consulting." q Inheritance gives you an is-a relationship and code-sharing. A Lawyer object can be treated as an Employee, and Lawyer inherits Employee's code. q Interface give you an is-a relationship without code sharing

8 Interface Syntax Interface: Example q An interface is a collection of constants and abstract methods abstract method: a method header without a method body; we declare an abstract method using the modifier abstract since all methods in an interface are abstract, the abstract modifier is usually left off 43 interface is a reserved word public interface Movable { public double getspeed(); public void setspeed(double speed); public void setdirection(int direction); public int getdirection(); This interface describes the behaviors common to all movable things. (Every Movable thing should have these methods.) A semicolon follows each method header immediately No method in an interface has a definition (body) 44 Implementing an interface Interface Implementation q general syntax: public class <name> implements <interface names> {... Example: public class Bicycle implements Movable {... (What must be true about the Bicycle class for it to compile?) q If we write a class that claims to be an interface (e.g., Movable), but doesn't implement all of the methods defined in the interface, it will not compile. Example: public class Bicycle implements Movable { The compiler error message: Bicycle.java:1: Bicycle is not abstract and does not override abstract method getspeed() in Movable Example: Shape interface q An interface for shapes: public interface Shape { public double area(); This interface describes the common features that all shapes should have in your design. (Every shape has an area.) Example: Circle class // Represents circles. public class Circle implements Shape { private double radius; // Constructs a new circle with the given radius. public Circle(double radius) { this.radius = radius; // Returns the area of this circle. public double area() { return Math.PI * radius * radius;

9 Example: Rectangle class Outline // Represents person. public class Person implements Shape { private double weight; private double height; public Person(double weight, double height) { this.weight = weight; this.height = height; // Returns the area of a person using Du Bois formula public double area() { return * Math.power(weight, 0.425) * Math.power(height, 0.725); q Admin and recap q Interface Motivation Syntax Polymorphism through interface // other methods Polymorphic Reference through Interface q A variable of interface type T can hold an object of any class implementing T. Movable mobj = new Bicyle(); You can call any methods defined in the Movable interface on mobj. When you invoke a method through the interface variable, the behavior is that of the object type. Interface Polymorphism: Example public static void printshapeinfo(shape s) { System.out.println("area : " + s.area()); System.out.println(); Any object that implements the interface may be passed as the parameter to the above method. Circle circ = new Circle(12.0); Person john = new Person(60, 175); printshapeinfo(circ); printshapeinfo(john); 52 Interface Polymorphism: Example q We can create an array of an interface type, and store any object implementing that interface as an element. Circle circ = new Circle(12.0); Person john = new John(60, 175); YaleStudent nicole = new YaleStudent(); Shape[] shapes = {circ, john, nicole; for (int i = 0; i < shapes.length; i++) { printshapeinfo(shapes[i]); Each element of the array executes the appropriate behavior for its object when it is passed to the printshapeinfo method Highly Reusable Sorting public interface Comparable { public int compareto(comparable comp); public class Sort { public static void insertionsort(comparable[] elems) { Comparable key = elems[index]; && elems[insertpos - 1].compareTo(key) > 0) { // end of for // end of insertionsort

10 Defining a Comparable Interface Defining a Comparable Interface public interface Comparable { public int compareto(comparable comp); public class Employee extends Object implements Comparable { public int compareto(comparable emp) { String oname = ((Employee)emp).name(); return name.compareto( oname ); public public class Staff { private Employee[] stafflist; public Staff() { stafflist = new Employee[4]; stafflist[0] = new Lawyer("Lisa"); stafflist[1] = new Secretary("Sally"); stafflist[2] = new LegalSecretary("Lynne"); stafflist[3] = new Hourly("Holly", 100); public void payday() { Sort.insertionSort(staffList); for (int count = 0; count < stafflist.length; count++) { System.out.printf("%-10s: ", stafflist[count].name()); System.out.printf("$%.2f\n", stafflist[count].pay() ); Exercise q What if you want the ability to sort according to different attribute, e.g., name, pay Highly Reusable Sorting public interface Comparable2 { public int compareto(comparable2 comp, String attr); public class Sort { public static void insertionsort(comparable2[] elems, String attr) { Comparable2 key = elems[index]; && elems[insertpos - 1].compareTo(key, attr) > 0) { 57 // end of for // end of insertionsort 58 Defining a Comparable Interface Summary: Using Interface for General Programming public interface Comparable2 { public int compareto(comparable comp, String attr); public class Employee extends Object implements Comparable2 { public int compareto(comparable emp, String attr) { if (attr.equals( name )) { String oname = ((Employee)emp).name(); return name.compareto( oname ); else if (attr.equals( pay )) { double diff = pay() ((Employee)emp).pay(); if (diff > 0) return 1; else return -1; else return 0; 59 q When defining a class or method (e.g., sorting), think about the essence (most general) properties/behaviors of the objects you require q Define those properties in an interface q Implement the class/method for the interface only so that your design is the most general! 60 10

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Critters/Event-Driven Programming; Interface Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Critters/Event-Driven Programming; Interface Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Critters/Event-Driven Programming Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Class project

More information

Admin. CS 112 Introduction to Programming. Admin. Admin. Recap. Recap: Polymorphism and Arrays. the Google doc to record the teaming

Admin. CS 112 Introduction to Programming. Admin. Admin. Recap. Recap: Polymorphism and Arrays. the Google doc to record the teaming Admin CS 112 Introduction to Programming Critters/Event-Driven Programming Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu q Class project

More information

What is Polymorphism? CS 112 Introduction to Programming

What is Polymorphism? CS 112 Introduction to Programming What is Polymorphism? CS 112 Introduction to Programming (Spring 2012) q polymorphism: Ability for the same code to be used with different types of objects and behave differently with each. Lecture #33:

More information

CS 112 Introduction to Programming. (Spring 2012)

CS 112 Introduction to Programming. (Spring 2012) CS 112 Introduction to Programming (Spring 2012) Lecture #32: Inheritance and Class Hierarchy Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 9 Inheritance, Polymorphism; reading: 9.2 2 The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs.

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 8 Lecture 8-3: Encapsulation; Homework 8 (Critters) reading: 8.3-8.4 Encapsulation reading: 8.4 2 Encapsulation encapsulation: Hiding implementation details from clients.

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Data Critter exercise: Snake Method constructor public Snake() eat Never eats fight always forfeits getcolor black Behavior getmove 1 E,

More information

Assignment 11: Critters

Assignment 11: Critters Assignment 11: Critters HW11 Assignment Specification 1 Critters A simulation world with animal objects with behavior: fight animal fighting getcolor color to display getmove movement tostring letter to

More information

CSc 110, Spring 2017 Lecture 38: Critters. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring 2017 Lecture 38: Critters. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2017 Lecture 38: Critters Adapted from slides by Marty Stepp and Stuart Reges 1 Calling overridden methods Subclasses can call overridden methods with super super(classname, self).method(parameters)

More information

CSc 110, Spring 2017 Lecture 37: Critters. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring 2017 Lecture 37: Critters. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2017 Lecture 37: Critters Adapted from slides by Marty Stepp and Stuart Reges 1 Calling overridden methods Subclasses can call overridden methods with super super(classname, self).method(parameters)

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #31: Software Reuse through Inheritance Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Inheritance Hierarchy; Polymorphism Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Class

More information

Admin. q Admin and recap q Class inheritance

Admin. q Admin and recap q Class inheritance Admin CS 112 Introduction to Programming q Class project Please work on forming teams Inheritance Hierarchy; Polymorphism Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:

More information

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM This assignment focuses on classes and objects. Turn in Ant.java, Bird.java, Hippo.java, Vulture.java,

More information

Homework 9: Critters (cont.)

Homework 9: Critters (cont.) Homework 9: Critters (cont.) Critter exercise: Snake Method Behavior constructor public Snake() eat Never eats fight always forfeits getcolor black getmove 1 E, 1 S; 2 W, 1 S; 3 E, 1 S; 4 W, 1 S; 5 E,

More information

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1 Critters This assignment was co-created by Stuart Reges and Marty Stepp. This program focuses on classes, objects, and inheritance. You will write the following files: Ant.java, Bird.java, Crab.java, FireAnt.java,

More information

Critter #1 Attack.ROAR random winner #2 wins #1 wins Attack.POUNCE #1 wins random winner #2 wins Attack.SCRATCH #2 wins #1 wins random winner

Critter #1 Attack.ROAR random winner #2 wins #1 wins Attack.POUNCE #1 wins random winner #2 wins Attack.SCRATCH #2 wins #1 wins random winner CSE 142, Winter 2016 Programming Assignment #8: Critters (40 points) Due: Tuesday, March 8, 2016, 11:30 PM (Husky must be submitted on time to be in tournament in class on Friday, March 11) This assignment

More information

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4

Interfaces. Relatedness of types. Interface as a contract. The area and perimeter of shapes 7/15/15. Savitch ch. 8.4 Relatedness of types Interfaces Savitch ch. 8.4 Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. There are certain attributes or operations that are

More information

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2 CSE 143 Lecture 13: Interfaces, Comparable reading: 9.5-9.6, 16.4, 10.2 Related classes Consider classes for shapes with common features: Circle (defined by radius r ): area = r 2, perimeter = 2 r Rectangle

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static Testing Critters Focus on one specific Critter of one specific type Only spawn 1 of each Critter type Make sure your fields

More information

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Related classes Consider classes for

More information

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4 Building Java Programs Interfaces and Comparable reading: 9.5-9.6, 10.2, 16.4 2 Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations

More information

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance Admin CS 112 Introduction to Programming q Class project Inheritance Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu 2 Recap: OOP Analysis

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key CIS 110 Introduction To Computer Programming December 19th, 2011 Final Answer key CIS 110 final instructions You have 120 minutes to finish this exam. Time will begin when called by a proctor and end precisely

More information

CSE 143X: Accelerated Computer Programming I/II HW5: Critters (due Friday, October 30, :30pm)

CSE 143X: Accelerated Computer Programming I/II HW5: Critters (due Friday, October 30, :30pm) CSE 143X: Accelerated Computer Programming I/II HW5: Critters (due Friday, October 30, 2015 11:30pm) This assignment focuses on Objects and Classes. Turn in the following files using the link on the course

More information

CSE 142 SPRING 2008 FINAL EXAM

CSE 142 SPRING 2008 FINAL EXAM CSE 142 SPRING 2008 FINAL EXAM 1. Expressions (10 points) For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g.,

More information

CS 312 Final Fall 2013

CS 312 Final Fall 2013 CS 312 Final Fall 2013 Your Name Your UTEID Problem Number Topic Points Possible 1 short answer 1 12 2 program logic 16 3 short answer 2 14 4 return methods 10 5 arrays 1 20 6 critters 20 7 arrays 2 20

More information

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 11: Inheritance and Polymorphism Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad

More information

Chapter 6: Inheritance

Chapter 6: Inheritance Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _

CS 312 Final Fall Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION. Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _ CS 312 Final Fall 2013 Your Name SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION Your UTEID SOLUTION SOLUTION SOLUTION SOLUTION SOLUTION _ Problem Number Topic Points Possible 1 short answer 1 12 2 program

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science Polymorphism: Interfaces and Iteration Fundamentals of Computer Science Outline A shape object hierarchy Classes that extend Versus classes that implements Java interfaces How Java handles multiple-inheritance

More information

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting Overview ITI 1121. Introduction to Computing II Rafael Falcon and Marcel Turcotte (with contributions from R. Holte) Electrical Engineering and Computer Science University of Ottawa Interface Abstract

More information

CS 312 Final Fall 2016

CS 312 Final Fall 2016 CS 312 Final Fall 2016 Your Name Your UTEID Problem Number Topic Points Possible 1 expressions 10 2 program logic 20 3 code tracing 15 4 critters 15 5 arrays 15 6 strings 15 7 2d arrays 15 8 ArrayList

More information

The Essence of OOP using Java, Nested Top-Level Classes. Preface

The Essence of OOP using Java, Nested Top-Level Classes. Preface The Essence of OOP using Java, Nested Top-Level Classes Baldwin explains nested top-level classes, and illustrates a very useful polymorphic structure where nested classes extend the enclosing class and

More information

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts. Inheritance The software crisis software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues:

More information

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

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

More information

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Big software. code reuse: The practice of writing program code once and using it in many contexts. Inheritance Big software software engineering: The practice of conceptualizing, designing, developing, documenting, and testing largescale computer programs. Large-scale projects face many issues: getting

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

CS 106B Lecture 27: Inheritance and Polymorphism in C++

CS 106B Lecture 27: Inheritance and Polymorphism in C++ CS 106B Lecture 27: Inheritance and Polymorphism in C++ Monday, June 4, 2018 Programming Abstractions Spring 2018 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming

More information

CSE 143 Lecture 20. Circle

CSE 143 Lecture 20. Circle CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public

More information

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code COMPUTER SCIENCE DEPARTMENT PICNIC Welcome to the 2016-2017 Academic year! Meet your faculty, department staff, and fellow students in a social setting. Food and drink will be provided. When: Saturday,

More information

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor CSE 143 Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog Dog

More information

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes COMP200 - Object Oriented Programming: Test One Duration - 60 minutes Study the following class and answer the questions that follow: package shapes3d; public class Circular3DShape { private double radius;

More information

Reusing Classes. Hendrik Speleers

Reusing Classes. Hendrik Speleers Hendrik Speleers Overview Composition Inheritance Polymorphism Method overloading vs. overriding Visibility of variables and methods Specification of a contract Abstract classes, interfaces Software development

More information

ASSIGNMENT 4 Classes and Objects

ASSIGNMENT 4 Classes and Objects ASSIGNMENT 4 Classes and Objects COMP-202A, Fall 2010, All Sections Due: Friday, November 19, 2010 (23:55) You MUST do this assignment individually and, unless otherwise specified, you MUST follow all

More information

Polymorphism. Polymorphism is an object-oriented concept that allows us to create versatile software designs

Polymorphism. Polymorphism is an object-oriented concept that allows us to create versatile software designs Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs Binding Consider the following method invocation: obj.doit(); At some point, this invocation

More information

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

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan COSC 121: Computer Programming II Dr. Bowen Hui University of Bri?sh Columbia Okanagan 1 Quick Review Inheritance models IS- A rela?onship Different from impor?ng classes Inherited classes can be organized

More information

Class 9: Static Methods and Data Members

Class 9: Static Methods and Data Members Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward Goals This the session in which we explain what static means. You

More information

Lab 10: Inheritance (I)

Lab 10: Inheritance (I) CS2370.03 Java Programming Spring 2005 Dr. Zhizhang Shen Background Lab 10: Inheritance (I) In this lab, we will try to understand the concept of inheritance, and its relation to polymorphism, better;

More information

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: has a CSE143 Sp Student. CSE 143 Java Object & Class Relationships Inheritance Reading: Ch. 9, 14 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Issues on PS3 NumberCoolness:

More information

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1 Merchandise Inheritance Systems Electronics Clothing Television Camcorder Shirt Shoe Dress Digital Analog 9.1.1 Another AcademicDisciplines Hierarchy Mathematics Engineering Algebra Probability Geometry

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q Issues on PS3 NumberCoolness:

More information

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: has a CSC Employee. Supervisor CSC 143 Object & Class Relationships Inheritance Reading: Ch. 10, 11 Relationships Between Real Things Man walks dog Dog strains at leash Dog wears collar Man wears hat Girl feeds dog Girl watches dog

More information

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver { Chapter 21- Using Generics Case Study: Geometric Bunch In this example a class called GeometricBunch is made to wrap around a list of GeometricObjects. Circle and Rectangle are subclasses of GeometricObject.

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Inheritance Sort in ascending order. Reusability 5 Sort Take The 4 Order 12,10,5,4. Class. Use this class to define a new class

Inheritance Sort in ascending order. Reusability 5 Sort Take The 4 Order 12,10,5,4. Class. Use this class to define a new class CS 112 / Section 02 Ece Akhan, Serkan Fidancı, Birol Cabukusta Notes of March 18, 2008 and March 20, 2008: Inheritance Sort in ascending order Reusability 5 Sort 12 5 4 12 İn Take 10 12 Sort 5 10 Desc.

More information

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism IST311 Advanced Issues in OOP: Inheritance and Polymorphism IST311/602 Cleveland State University Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition

More information

The Comparable Interface. reading: 10.2

The Comparable Interface. reading: 10.2 The Comparable Interface reading: 10.2 2 Wednesday Notecards What s your credit card number? In contains, how would we get it to return false if either the left or the right returned false? Use &&, you

More information

Data Structures and Other Objects Using C++

Data Structures and Other Objects Using C++ Inheritance Chapter 14 discuss Derived classes, Inheritance, and Polymorphism Inheritance Basics Inheritance Details Data Structures and Other Objects Using C++ Polymorphism Virtual Functions Inheritance

More information

C18a: Abstract Class and Method

C18a: Abstract Class and Method CISC 3115 TY3 C18a: Abstract Class and Method Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/31/2018 CUNY Brooklyn College 1 Outline Recap Inheritance and polymorphism Abstract

More information

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes CS/ENGRD 2110 SPRING 2019 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due Thursday night (14 February) Go back to Lecture 6 & discuss method

More information

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Interfaces vs. Inheritance Abstract Classes Inner Classes Readings This Week: No new readings. Consolidate! (Reminder: Readings

More information

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

Java Comparable interface

Java Comparable interface Java Comparable interface Recall that to define a binary search tree, the elements that we are considering must be comparable to each other, meaning that there must be a well-defined ordering. For strings

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

The Nervous Shapes Example

The Nervous Shapes Example The Nervous Shapes Example This Example is taken from Dr. King s Java book 1 11.6 Abstract Classes Some classes are purely artificial, created solely so that subclasses can take advantage of inheritance.

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

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

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3: PROGRAMMING ASSIGNMENT 3: Read Savitch: Chapter 7 Programming: You will have 5 files all should be located in a dir. named PA3: ShapeP3.java PointP3.java CircleP3.java RectangleP3.java TriangleP3.java

More information

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key for review problems

CIS 110 Introduction To Computer Programming. December 19th, 2011 Final. Answer key for review problems CIS 110 Introduction To Computer Programming December 19th, 2011 Final Answer key for review problems CIS 110 final instructions You have 120 minutes to finish this exam. Time will begin when called by

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Last class. -More on polymorphism -super -Introduction to interfaces

Last class. -More on polymorphism -super -Introduction to interfaces Last class -More on polymorphism -super -Introduction to interfaces Interfaces Sometimes in Java, we will have 2 classes that both share a similar structure, but neither of them is clearly the parent or

More information

Use the scantron sheet to enter the answer to questions (pages 1-6)

Use the scantron sheet to enter the answer to questions (pages 1-6) Use the scantron sheet to enter the answer to questions 1-100 (pages 1-6) Part I. Mark A for True, B for false. (1 point each) 1. Abstraction allow us to specify an object regardless of how the object

More information

Building Java Programs

Building Java Programs Building Java Programs Comparable, Search reading: 10.2, 13.1 13.3 2 Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Lecture 2: Java & Javadoc

Lecture 2: Java & Javadoc Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block

More information

CSE 142 Sample Final Exam #1 (based on Spring 2005's final; thanks to Stuart Reges)

CSE 142 Sample Final Exam #1 (based on Spring 2005's final; thanks to Stuart Reges) CSE 142 Sample Final Exam #1 (based on Spring 2005's final; thanks to Stuart Reges) 1. Expressions For each expression at left, indicate its value in the right column. List a value of appropriate type

More information

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

Interfaces and itera-on. CSCI 136: Fundamentals of Computer Science II Keith Vertanen Interfaces and itera-on CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview A shape object hierarchy Classes that extend Versus classes that implements Java interfaces How Java handles

More information

Binghamton University. CS-140 Fall Dynamic Types

Binghamton University. CS-140 Fall Dynamic Types Dynamic Types 1 Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected

More information

What is inheritance. 8: Inheritance. Recall the objectives of OOP. Defining a Subclass. A cylinder is like a circle, but with an extra length

What is inheritance. 8: Inheritance. Recall the objectives of OOP. Defining a Subclass. A cylinder is like a circle, but with an extra length School of Computer Science, University of Birmingham Java Lecture notes. M. D. Ryan. October 2001. 8: Inheritance What is inheritance A way of defining a new in terms of an old one. Idea: B is like the

More information

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1

More information

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes

Department of Civil and Environmental Engineering, Spring Semester, ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes Department of Civil and Environmental Engineering, Spring Semester, 2013 ENCE 688R: Midterm Exam: 1 1/2 Hours, Open Book and Open Notes Name : Question Points Score 1 30 2 30 3 40 Total 100 1 Question

More information

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer public class Base { protected int theint = 100; System.out.println( theint ); public class Doubler extends Base { System.out.println( theint*2 ); public class Tripler extends Base { System.out.println(

More information

Reviewing OO Concepts

Reviewing OO Concepts Reviewing OO Concepts Users want to draw circles onto the display canvas. public class Circle { // more code here SWEN-261 Introduc2on to So3ware Engineering Department of So3ware Engineering Rochester

More information

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle Admin CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

CSE 142, Winter 2007 Final Exam. Name:

CSE 142, Winter 2007 Final Exam. Name: 1 of 10 CSE 142, Winter 2007 Final Exam Name: Section: Student ID #: TA: You have 110 minutes to complete this exam. You may receive a deduction if you keep working after the instructor calls for papers.

More information