3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.



Similar documents
Inheritance, overloading and overriding

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Java: overview by example

CS 111 Classes I 1. Software Organization View to this point:

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Java Programming Language

CS193j, Stanford Handout #10 OOP 3

Chapter 13 - Inheritance

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

5. Advanced Object-Oriented Programming Language-Oriented Programming

public void creditaccount(string accountnumber, float amount) { this.accounts.get(accountnumber).credit(amount); }

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

El Dorado Union High School District Educational Services

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

CISC 181 Project 3 Designing Classes for Bank Accounts

Java Application Developer Certificate Program Competencies

Fundamentals of Java Programming

ICOM 4015: Advanced Programming

D06 PROGRAMMING with JAVA

More on Objects and Classes

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Java SE 8 Programming

Java Interview Questions and Answers

AP Computer Science Java Subset

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Object Oriented Software Design II

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Facebook Twitter YouTube Google Plus Website

Lecture J - Exceptions

The C Programming Language course syllabus associate level

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Java (12 Weeks) Introduction to Java Programming Language

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

PHP Object Oriented Classes and objects

Python for Rookies. Example Examination Paper

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Description of Class Mutation Mutation Operators for Java

Tutorial on Writing Modular Programs in Scala

Introduction to Object-Oriented Programming

Specialized Programme on Web Application Development using Open Source Tools

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Formal Engineering for Industrial Software Development

Web Development in Java

Konzepte objektorientierter Programmierung

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

C++ INTERVIEW QUESTIONS

RMI Client Application Programming Interface

Glossary of Object Oriented Terms

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

JUnit - A Whole Lot of Testing Going On

Introducing Variance into the Java Programming Language DRAFT

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Moving from CS 61A Scheme to CS 61B Java

Lecture 7: Class design for security

The Interface Concept

J a v a Quiz (Unit 3, Test 0 Practice)

C++FA 5.1 PRACTICE MID-TERM EXAM

Computing Concepts with Java Essentials

Syllabus for CS 134 Java Programming

Outline of this lecture G52CON: Concepts of Concurrency

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

History OOP languages Year Language 1967 Simula Smalltalk

: provid.ir

Part I. Multiple Choice Questions (2 points each):

Java CPD (I) Frans Coenen Department of Computer Science

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Ch 7-1. Object-Oriented Programming and Classes

CIS 190: C/C++ Programming. Polymorphism

Unit Testing & JUnit

16 Collection Classes

Creating a List UI with Android. Michele Schimd

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

An Incomplete C++ Primer. University of Wyoming MA 5310

Java EE Web Development Course Program

java Features Version April 19, 2013 by Thorsten Kracht

Design Patterns in Java

Brent A. Perdue. July 15, 2009

Java SE 7 Programming

High-Level Language. Building a Modern Computer From First Principles.

Android Application Development Course Program

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

AP COMPUTER SCIENCE A 2007 SCORING GUIDELINES

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Programmation 2. Introduction à la programmation Java

Safe Object-Oriented Software: The Verified Design-By-Contract Paradigm

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

11 November

Unit Testing and JUnit

Transcription:

Industrial Programming Systems Programming & Scripting Lecture 12: C# Revision 3 Pillars of Object-oriented Programming Encapsulation: each class should be selfcontained to localise changes. Realised through public and private attributes. Specialisation: model relationships between classes. Realised through inheritance. Polymorphism: treat a collection of items as a group. Realised through methods at the right level in the class hierarchy. Industrial Programming 1 Industrial Programming 2 Bank Account Extending the Example Attribute: Account No. Name Balance (in s) Methods: Deposit Withdraw Class BankAccount accountno : ulong name: string balance: decimal Deposit(decimal) Withdraw(decimal) Example Object (BankAccount) 11111 Savings account 101.00 Define another class ProperBankAccount with an overdraft facility. Automatically assign account numbers to new accounts when generating them. Keep all field information secure within the account. Industrial Programming 3 Industrial Programming 4

Designing the Class Fields: Those in BankAccount + overdraft Methods: Those in BankAccount but modified Invariants: Balance in ProperBankAccount is never lower than the negative overdraft. class BankAccount { protected static ulong latestaccountno = 1000; protected ulong accountno; protected decimal balance; protected string name; We use the access modifier protected to hide all fields from other classes, except derived classes. We use a static field to keep track of the assigned account numbers. Industrial Programming 5 Industrial Programming 6 public BankAccount(string name) { latestaccountno++; this.accountno = latestaccountno; this.name = name; this.balance = 0M; public BankAccount(ulong no, string name) { this.accountno = no; this.name = name; this.balance = 0M; We use overloading of the constructor class and the static field to auto assign account numbers. public void Deposit(decimal x) { this.balance += x; The Deposit method is unchanged. Its access modifier is public. Industrial Programming 7 Industrial Programming 8

public virtual void Withdraw(decimal x) { if (this.balance >= x) { this.balance -= x; else { throw new InsufficientBalance("Balance too low: {0", this.balance); We use exceptions to cover the case of an insufficient balance for making a withdrawl. The method must be declared virtual to allow overriding in a sub-class. public decimal GetBalance() { return this.balance; public void ShowBalance() { Console.WriteLine("Current Balance: " + this.balance.tostring()); public virtual void ShowAccount() { Console.WriteLine("Account Number: {0\tAccount Name: {1\tCurrent Balance: {2", this.accountno, this.name, this.balance.tostring()); ShowAccount must be declared virtual to allow overriding in sub-classes. The other methods are unchanged. Industrial Programming 9 Industrial Programming 10 Invariants // Class invariants: // invariant: this.balance >= 0 We record the above class invariants: this predicate must hold at any point in the lifetime of an object of this class. public class InsufficientBalance : System.Exception { public InsufficientBalance(string msg, decimal x):base(msg) { Console.WriteLine(" " + x.tostring()); The exception class derives from System.Exception It prints a message by calling the constructor of this base class Additionally, it prints the balance. Industrial Programming 11 Industrial Programming 12

Implementing the Class class ProperBankAccount: BankAccount { public decimal overdraft { get ; set; ProperBankAccount inherits from BankAccount, thus all non-private fields and methods are available. The overdraft is implemented as a property with default get and set methods. Industrial Programming 13 Implementing the Class (cont'd) public ProperBankAccount(string name) :base(name) { // nothing; use set property on overdraft public ProperBankAccount(ulong no, string name) :base(no,name) { // nothing; use set property on overdraft We use overloading to implement 2 constructors for ProperBankAccount The static field is used to keep track of assigned account numbers. Industrial Programming 14 Implementing the Class public override void Withdraw(decimal x) { if (this.balance+this.overdraft >= x) { this.balance -= x; else { throw new InsufficientBalance("Balance (including overdraft) too low", this.balance); By declaring the Withdraw method as override, the instance in ProperBankAccount overrides/replaces the one in BankAccount Implementing the Class public override void ShowAccount() { base.showaccount(); Console.WriteLine("\twith an overdraft of {0", this.overdraft); Similarly, the ShowAccount method is overridden to additionally show the overdraft for this account. base.showaccount() calls the method in the base class. Industrial Programming 15 Industrial Programming 16

Implementing the Class // Class invariants: // invariant: this.balance >= - this.overdraft Finally, we record the class invariants for this class. Industrial Programming 17 Testing the Class public void RunTransactions(BankAccount acct) { // if it has an overdraft facility, initialise its value ProperBankAccount pacct = acct as ProperBankAccount; if (pacct!= null) { pacct.overdraft = 200; acct.showaccount(); acct.showbalance(); // first, deposit something decimal x = 600M; Console.WriteLine("Depositing " + x); acct.deposit(x); acct.showbalance(); // then, try to withdraw something decimal y = 400M; Console.WriteLine("Withdrawing " + y); try { acct.withdraw(y); catch (InsufficientBalance e) { Console.WriteLine("InsufficientBalance {0 for withdrawl of {1", acct.getbalance(), y); acct.showbalance(); // then, try to withdraw the same amount again... Industrial Programming 18 public static void Main(){ RunTester t = new RunTester(); The Main Method // create a basic account BankAccount mine = new BankAccount("MyAccount"); // create a proper account ProperBankAccount mineovdft = new ProperBankAccount("MyProperAccount"); // collect them in an array BankAccount[] accts = new BankAccount[2] { mine, mineovdft ; for (int i=0; i<accts.length; i++) { t.runtransactions(accts[i]); We can use the same RunTransactions method for both accounts. We use polymorphism in defining the array accts, holding both types of accounts. Industrial Programming 19 Running the Program Account Number: 1001 Account Name: MyAccount Current Balance: 0 Current Balance: 0 Depositing 600 Current Balance: 600 Current Balance: 200 200 InsufficientBalance 200 for withdrawl of 400 Current Balance: 200 Account Number: 1001 Account Name: MyAccount Current Balance: 200 Account Number: 1002 Account Name: MyProperAccount Current Balance: 0 with an overdraft of 200 Current Balance: 0 Depositing 600 Current Balance: 600 Current Balance: 200 Current Balance: -200 Account Number: 1002 Account Name: MyProperAccount Current Balance: -200 with an overdraft of 200 Industrial Programming 20

Concepts used in the Example Overloading to have several constructors with different numbers of arguments. Inheritance of methods is used to share code. Overriding of methods is used to modify the behaviour of methods in sub-classes. Polymorphism is used to collect (sub-)classes. Exceptions are used for error handling. Access modifiers are used to hide fields. Properties are used for convenience. Static fields are used to count instances. Industrial Programming 21