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

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

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

CS 113 PRACTICE FINAL

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

CSE 143 Lecture 20. Circle

CS1150 Principles of Computer Science Objects and Classes

AShape.java Sun Jan 21 22:32: /** * Abstract strategy to compute the area of a geometrical shape. Dung X. Nguyen * * Copyright

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

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

Data Structures and Other Objects Using C++

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

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Abstract Classes Interfaces

CS-202 Introduction to Object Oriented Programming

ITI Introduction to Computing II

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Letterkenny Institute of Technology

Inheritance and Polymorphism

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

ITI Introduction to Computing II

ITI Introduction to Computing II

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

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

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Programming Languages and Techniques (CIS120)

Simplified Operating Instructions T105-C / T108-C / T106-C / T17B-C DISPLAYS DAY OR DAY BLOCK PROG 00:0000 PROG PROG ALWAYS OFF

//Since the variable names are same (seconds) we use this key. Actually these variables //are not same, just, they have same name.

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

Cloning Enums. Cloning and Enums BIU OOP

CH. 2 OBJECT-ORIENTED PROGRAMMING

EECS 1001 and EECS 1030M, lab 01 conflict

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Agenda: Discussion Week 7. May 11, 2009

CS 251 Intermediate Programming More on classes

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

Inheritance. Inheritance

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

CS Week 13. Jim Williams, PhD

Inheritance, and Polymorphism.

Chapter 11 Inheritance and Polymorphism

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

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

CS122/Sec. 2 Week No. 3 Dilce Gozuyasli Sevi Durdu Due date: March 7, 08. Notes of February 26, 08. Review of class Circle UML diagram

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

Programming 2. Inheritance & Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Programming Languages and Techniques (CIS120)

Reusing Classes. Hendrik Speleers

We are on the GUI fast track path

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Timing for Interfaces and Abstract Classes

Programming in the Large II: Objects and Classes (Part 2)

Objectives. Describe ways to create constants const readonly enum

Programming Languages and Techniques (CIS120)

ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

Part 1. Programming Output [Polymorphism]

CSC 1052 Algorithms & Data Structures II: Interfaces

Inheritance (continued) Inheritance

Chapter 11 Inheritance and Polymorphism

Chapter 8 Objects and Classes Part 1

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

CSC-140 Assignment 6

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Inheritance Abstraction Polymorphism

public static boolean isoutside(int min, int max, int value)

Chapter 14 Abstract Classes and Interfaces

Using Variables to Write Pattern Rules

Object Oriented Relationships

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

2 What are the differences between constructors and methods?

Chapter 9 Abstract Classes and Interfaces

Crystal Reports. Overview. Contents. Cross-Tab Capabilities & Limitations in Crystal Reports (CR) 6.x

CIS265 - Spring Exam 2 First Name Last Name CSU#

Object Oriented Programming. Java-Lecture 11 Polymorphism

Reviewing OO Concepts

Methods Common to all Classes

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

Programming Languages and Techniques (CIS120)

Introduction to Programming Written Examination

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

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

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

IT 313 Advanced Application Development Midterm Exam

Programming Languages and Techniques (CIS120)

OBJECT ORİENTATİON ENCAPSULATİON

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Transcription:

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. The 5 10 Desc 10 4 Order Inverse 4 4 12 12,10,5,4 Used to define new classes (concrete class) Class Use this class to define a new class Abstract Class Change/ extend existing class according to our needs Cannot be used to create objects GeometricObject -color; String -filled : boolean -datacreated:java.util.date +GeometricObject +getcolor(): String +tostring() UML Diagram Instance variables Super Class Interfaces Circle Rectangle Sub Classes 1

public class Geometric Object { private String color= white ; private Boolean filled; private java.util.date date Created; Instance variables private Date datacreated; Color public GeometricObject(){// default constructor datacreated=new java.util.date(); (getcolor,setcolar etc.) myshape Filled public void setfilled (boolean filled){ this.filled=filled; public class Circle extends GeometricObject { private double radius; public Circle(double radius) { this.radius = radius; public string tostring() { return("circle radius is " + r); public Circle(double radius, String newcolor) { this.radius = radius; super.setcolor(newcolor); public class Rectangle extends GeometricObject { private double length, width; public Rectangle() { length = 1.0; width = 1.0; public Rectangle(double length, double width) { this.length = length; this.length = width; public String tostring() { return("rectangle has width : " + width + "and has length : " + length); public static void main(string [] args) { GeometricObject [] myarray = new GeometricObject[10]; Circle mycircle = new Circle(20); Rectangle myrectangle = new Rectangle(10, 20); Circle hercircle = new Circle(50); hercircle.setcolor("purple"); myarray[0] = mycircle; myarray[1] = myrectangle; tostring() of rectangle overrides tostring() of GeometricObject. 2

myarray[2] = hercircle; for(int i = 0; i<3; i++) { myarray[i].tostring; tostring method is determined during execution time. myarray Reference variable of type GeometricObject it can refer a rectangular or circle.(dynamic binding) CLOCK Clock Day Clock am/pm clock I-am/pm clock: public class AmPmClock extends Clock { private String brandname; public AmPmClock() 3

brandname = "Casio"; public AmPmClock(int h, int m, int s, String bname) brandname = bname; public void display() // overrides the display() method defined in clock { String hzero, mzero, szero; int h = super.gethours(); int m = super.getminutes(); int s = super.getseconds(); if (h<12) System.out.println("am"); else //afternoon { h = h-12; h = (h==0)? 12:h; hzero = (h<10)? "0":""; mzero = (m<10)? "0":""; szero = (s<10)? "0":""; System.out.println("Time is: " + hzero +h+":" + mzero +m+ ":" + szero +s+ "pm"); // end of else System.out.println(""+brandName); // end of AmPmClock II-Day Clock: public class AmPmClock { private String brandname; public AmPmClock() brandname = "Casio"; public AmPmClock(int h, int m, int s, String bname) brandname = bname; public void display() // overrides the display() method defined in clock { String hzero, mzero, szero; int h = super.gethours(); int m = super.getminutes(); int s = super.getseconds(); if (h<12) System.out.println("am"); else //afternoon { h = h-12; h = (h==0)? 12:h; hzero = (h<10)? "0":""; mzero = (m<10)? "0":""; szero = (s<10)? "0":""; System.out.println("Time is: " + hzero + h + ":" + mzero + m + ":" + szero + s + "pm"); // end of else System.out.println(""+brandName); 4

// end of AmPmClock public class DayClock extends Clock { private String day; public DayClock() day = "Sunday"; public DayClock(int h, int m, int s, String d) day = d; public void tick() { super.tick(); if (get.hours()==0 && get.minutes==0 && get.seconds==0) { if (day == "Sunday") day = "Monday"; elseif (day == "Monday") day = "Tuesday"; elseif (day == "Tuesday") day = "Wednesday"; elseif (day == "Wednesday") day = "Thursday"; elseif (day == "Thursday"); day = "Friday"; elseif (day == "Friday") day = "Saturday"; else day = "Sunday"; // end of tick public void display() System.out.println(""+day); public String tostring() { return(super.tostring() + "" + day); // end of DayClock public static void main(string [] args) { Clock c = new Clock(0,0,0); DayClock dc = new DayClock(15,5,0, "Thursday"); AmPmClock ampmc = new AmPmClock(5,7,0, "Rolex"); dc.display(); System.out.println("AmPm Clock is : " + ampmc); c=dc; c.tick(); c.display(); // end of main method c dc Thursday Dynamic bounding (polymorphism) 5