Lesson11-Inheritance-Abstract-Classes. The GeometricObject case

Similar documents
Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

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

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

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

CS1150 Principles of Computer Science Objects and Classes

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 13 Abstract Classes and Interfaces

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

CSC330 Object Oriented Programming. Inheritance

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

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

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

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.

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

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I

Inheritance and Polymorphism

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

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Abstract Classes Interfaces

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

Object Oriented Programming

Inheritance (Deitel chapter 9)

CS2 Assignment A1S The Simple Shapes Package

Topic 7: Algebraic Data Types

COMP 250. inheritance (cont.) interfaces abstract classes

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

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

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object Oriented Programming in C#

Chapter 3: Inheritance and Polymorphism

CS Week 13. Jim Williams, PhD

Lab 9: Creating a Reusable Class

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance, and Polymorphism.

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

Java Object Oriented Design. CSC207 Fall 2014

Chapter 9 - Object-Oriented Programming: Polymorphism

CS-202 Introduction to Object Oriented Programming

Reusing Classes. Hendrik Speleers

Chapter 14 Abstract Classes and Interfaces

Department of Networks College of Bardarash Technical Institute DUHOK Polytechnic University Subject: Programming Fundamental by JAVA Course Book

Chapter 13 Abstract Classes and Interfaces

CS 162, Lecture 25: Exam II Review. 30 May 2018

What is an interface? Why is an interface useful?

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

We are on the GUI fast track path

AP CS Unit 6: Inheritance Programs

This is an open-book test. You may use the text book Be Sharp with C# but no other sources, written or electronic, will be allowed.

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

CSCE145 Test 2-Review 03/29/2015 Hongkai Yu

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS CSIS1614

Session 04 - Object-Oriented Programming 1 Self-Assessment

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

F I N A L E X A M I N A T I O N

CSE 143 Lecture 20. Circle

Polymorphism CSCI 201 Principles of Software Development

Final Examination Semester 3 / Year 2010

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

Chapter 11 Inheritance and Polymorphism

Chapter 6: Inheritance

Polymorphism. Polymorphism. CSC 330 Object Oriented Programming. What is Polymorphism? Why polymorphism? Class-Object to Base-Class.

Programming 2. Inheritance & Polymorphism

25. Generic Programming

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

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

Advanced Placement Computer Science. Inheritance and Polymorphism

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

Programming by Delegation

Introduction to C# Applications

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

UNIVERSITY OF THE FREE STATE MAIN CAMPUS CSIS2614 DEPARTMENT: COMPUTER SCIENCE AND INFORMATICS CONTACT NUMBER:

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 214 MODULE TEST 1

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Java Programming. Abstract classes and Interfaces

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS171:Introduction to Computer Science II. Li Xiong

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS RIS 114 CLASS TEST 2 and 3 DATE: 4 August 2014 TIME: 180 minutes MARKS: 70

Chapter 11 Inheritance and Polymorphism

Equality in.net. Gregory Adam 07/12/2008. This article describes how equality works in.net

Chapter 13 Abstract Classes and Interfaces

CS 113 PRACTICE FINAL

Creating Your Own Classes

Lesson07-Arrays-Part4. Program class. namespace Lesson07ArraysPart4Prep { class Program { static double METER_TO_INCHES = 100 / 2.

CSCI 102 Fall 2010 Exam #1

Friend Functions, Inheritance

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

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

Data Structures and Other Objects Using C++

Transcription:

Lesson11-Inheritance-Abstract-Classes The GeometricObject case

GeometricObject class public abstract class GeometricObject private string color = "White"; private DateTime datecreated = new DateTime(2017, 12, 31, 23, 59, 59); private bool filled = false; // Properties (classic style) public string Color get return color; set string temp = value.toupper(); char firstletter = temp[0]; color = firstletter + temp.substring(1).tolower(); public DateTime DateCreated get return datecreated; private set datecreated = value; public bool Filled get return filled; set filled = value; //constructors public GeometricObject() Color = "White"; Filled = true; DateCreated = DateTime.Now; public GeometricObject(string colorvalue, bool filledvalue) Color = colorvalue; Filled = filledvalue; DateCreated = DateTime.Now; //user-defined methods public override string ToString() return string.format($"geometricobject [Color: Color, " + $"Filled:Filled, " + $"DateCreated:DateCreated.ToString("yyy-MM-dd")]"); //Observe that teh following two methods have no body! //Descendants of the GeometricObject class MUST implement them. public abstract double GetArea(); public abstract double GetArea(int numdecimals); public abstract double GetPerimeter(); public virtual int CalculateCost() //an arbitrary way to assess figure's cost int cost = 0; if (Color == "Gold" Color=="Silver") cost += 300;

if (filled) cost += 200; return cost; Circle class class Circle : GeometricObject //data member(s) private double radius = 0; //properties public double Radius get return radius; set radius = Math.Abs(value); //this is an alternative way of defining method GetArea() //public double Area // // get return Math.PI * Math.Pow(Radius, 2); // //constructor(s) public Circle() : base() Radius = 0; public Circle(string colorvalue, bool filledvalue, double radiusvalue) :base(colorvalue, filledvalue) Radius = radiusvalue; //user-defined methods public override string ToString() //showing two possible versions of the ToString() method int option = 1; switch (option) case 1: //OPTION1. mixed message child + ancestor string ancestormsg = base.tostring(); string childmsg1 = string.format($"circle [Radius: Radius ] "); return childmsg1 + " child of " + ancestormsg; case 2: //OPTION2. only child's msg string childmsg2 = string.format($"circle [Radius:Radius, " + $"Color:Color, Filled:Filled, " +

return childmsg2; $"Created:DateCreated ]"); return "Circle"; //The following three methods override the empty definition imposed by ancestor class. //The base class demanded that its ABSTRACT METHODS must be implemented by its children. public override double GetArea() //NOTE: this method could be implemented instead as a read-only property //(see above defintion of property Area) return Math.PI * Math.Pow(Radius, 2); public override double GetArea(int numdecimals) //display area using a given number of decimal positions double area = Math.PI * Math.Pow(Radius, 2); string numformat = "0:N" + numdecimals + ""; string strarea = string.format(numformat, area); return Convert.ToDouble(strArea); public override double GetPerimeter() return 2 * Math.PI * Radius; //Circle //namespace Rectangle class class Rectangle : GeometricObject //data members private double height = 0; private double width = 0; //properties public double Width get return width; set width = value; public double Height

get return height; set height = value; //constructors public Rectangle() : base() Height = 0; Width = 0; public Rectangle(double widthvalue, double heightvalue, string colorvalue, bool filledvalue) :base(colorvalue, filledvalue) Width = widthvalue; Height = heightvalue; //user defined methods public override string ToString() return string.format($"rectangle [Height:Height:N2, Width:Width:N2, " + $"Color:Color, Filled:Filled, " + $"Created:DateCreated ]"); //The following three methods override the empty definition imposed by ancestor class. //The base class demanded that its ABSTRACT METHODS must be implemented by its children. public override double GetArea() return Height * Width; public override double GetArea(int numdecimals) //display area using a given number of decimal positions double area = Height * Width; string numformat = "0:N" + numdecimals + ""; string strarea = string.format(numformat, area); return Convert.ToDouble(strArea); public override double GetPerimeter() return 2 * (Height + Width); Program class class Program static void Main(string[] args)

//this declaration is invalid when GeometricObject is abstract //GeometricObject g1 = new GeometricObject("Blue", true); //Console.WriteLine(g1); Circle c1 = new Circle("Red", true, 2); Console.WriteLine(c1); Circle c2 = new Circle(); Console.WriteLine(c2); Rectangle r1 = new Rectangle(); Console.WriteLine(r1); Rectangle r2 = new Rectangle(4.33, 2.1789, "Gold", true); Console.WriteLine(r2); Console.WriteLine("r2 area 0", r2.getarea()); Console.WriteLine("r2 area 0", r2.getarea(2)); Console.WriteLine("r2 perimeter 0", r2.getperimeter()); Console.WriteLine("r2 cost 0", r2.calculatecost()); Console.Read(); Console