CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Java Syntax, Java Conventions, CSE 115 Conventions (Part 2) CSE 115 Spring 2006 January 30, February 1 & 3, 2006.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Object Oriented Software Development
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CET203 SOFTWARE DEVELOPMENT Session 3B Interfaces.
Writing Classes (Chapter 4)
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSC 212 – Data Structures Lecture 12: Java Review.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Classes CS 21a: Introduction to Computing I First Semester,
Agenda Object Oriented Programming Reading: Chapter 14.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object Oriented Analysis and Design Class and Object Diagrams.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Properties and Indexers 9. © Aptech Ltd.Building Applications Using C# / Session 92 Objectives  Define properties in C#  Explain properties, fields,
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Creating Your Own Classes
Objects as a programming concept
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Creating and Using Classes
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Defining Your Own Classes
Chapter 9 Objects and Classes
Defining Classes and Methods
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Presentation transcript:

CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes

Objectives Introduction to Object Oriented Programming Model a basic class using the UML class diagram notation Implement a class in C# Introduce new naming conventions and C# properties Design and implement a simple multi-class system

Object Orientation Class Object 1 Object 2 Object 3 Object 4 A ‘class’ is a software design which describes the general properties of something which the software is modelling. Individual ‘objects’ are created from the class design for each actual thing

Example A class Car used in a computer system for a second-hand car showroom The class would be a model of the important characteristics of a car for the purposes of the business For each car in the showroom the system would have a separate object – an instance of class Car.

Class Diagram Class name Instance variables Methods The type of object the class is defining (e.g. Car) The information (properties) the objects will record (e.g. model, price) The actions the class can perform (e.g. sell)

An example class - Employee We will create a class to model an employee of a company The company needs to store the following information about each employee – Name – Department – Salary The company determines the salary of an employee based on their profits for the year Therefore the salary can go up or down by a percentage amount each year We will model this in a UML class diagram

UML Class Diagram Employee name : String department : String salary : int IncreaseSalary(percent : int) DecreaseSalary(percent : int) Methods with parameters Instance variables (note the data type comes after the name in the class diagram!)

Sections of a class We can consider a class as containing two distinct sections The interface is the details that the programmer of another class needs to know in order to use the class The implementation is the section that defines how the class actually works A programmer using the class has no need to know about the implementation In fact it is better that they don’t know (or make use of) the details of the implementation Information hiding makes use of this separation

Access modifiers C# provides two keywords to enable information hiding We apply these to the methods and instance variables of the class We use public to identify the interface of the class We use private to identify the implementation of the class These keywords are known as access modifiers In good O-O programming – Instance variables should be private – Methods which are called from outside the class are public – Methods which are only used within the class (helper methods) are private

Access Modifiers on a class diagram Employee - name : String - department : String - salary : int + IncreaseSalary(percent : int) + DecreaseSalary(percent : int) + denotes public - denotes private (note the data type comes after the name in the class diagram!)

Accessor/Mutator methods In CET101 we gained access to an instance variable from outside the class using two special methods An accessor method or “getter” returned the value of an instance variable public String getName() { return name; } A mutator method or “setter” modified the value of an instance variable public String setName(String pName) { name = pName; }

An alternative approach in C# Accessor (getter) and mutator (setter) methods can be replaced with an alternative called Properties Properties are defined with a special code syntax for getting/setting the instance variable Properties often have the same name as the instance variable but with an upper-case first letter Our convention will be to remove the m_ and the data type character from instance variables (as used in CET101) and use case to distinguish between the instance variable and the property For example the property associated with instance variable name will be called Name

Defining a property private String name; public String Name { get { return name; } set { name = value; } Instance variable Property Obtain value from instance variable Assign value to instance variable

Auto-implemented properties Any number of statements can be included in the get/set accessors but often they simply return or assign a value C# 3.0 introduced a more concise format for properties private String name; public String Name {get; set}; The properties implementation is created automatically with the assumptions – The set accessor simply assigns a value to the appropriate field – The get accessor simply returns the value of the appropriate field

Creating objects and using properties Employee emp1 = new Employee(); emp1.Name = "Liz Gandy"; Console.WriteLine("Employee name:” + emp1.Name); Create object Assign value to instance variable via property Obtain value from instance variable via property

Calling Methods To call a method we link it to the reference name using the “dot” notation: emp1.IncreaseSalary(); If the method requires parameters we place these in the brackets (as we would for functions): emp1.IncreaseSalary(10); If the method requires multiple parameters then we separate them with commas emp1.IncreaseSalary(10, "monthly");

Constructors Constructors are special methods which are called when an object of the class is first created (using new) They are always defined as public and have the same name as the class They should not return a value They may require parameters which are used to initialise instance variables or they may initialise them to default values Classes can contain multiple constructors with different parameter sets The programmer can then choose whichever is most appropriate for their needs

Employee Class Constructors //constructor with no parameters public Employee() { name = "No name"; department = "Not set"; salary = 0; } //constructor with parameters public Employee(String name, String dept, int salary) { this.name = name; this.department = dept; this.salary = salary; } Note the use of this to distinguish instance variable from parameter

Creating objects using constructors Using the constructor with no parameters: Employee emp1 = new Employee(); Using the constructor with parameters Employee emp2 = new Employee("Jane Smith", "Accounts", 21500);

Demonstration EmployeeClass.sln

Designing multiple class systems We have seen that object oriented systems model the real world Systems in the real world normally contain many entities inter-linked with each other In an OO model these entities will be represented by classes A real OO system will therefore comprise multiple classes These classes will have “relationships” between them

The Bank/Employee System We will now incorporate our Employee class into a banking/employee system A bank will contain branches, each located in a particular town or city Each branch will employ staff These employees will be assigned to a particular department and receive a particular salary Depending on the banks profits and employee performance their salaries may increase or decrease each year

The Branch class Branch - location : String - employeeCount : int + AddEmployee(employee : Employee) + GetEmployee(index : int) : Employee

Relationships We need to identify the relationships in our system The Branch “uses” Employee(s) We can represent this on a simplified class diagram The arrow indicates the relationship between the classes BranchEmployee

References Relationships are represented by a special type of variable We call this a reference to another class If ClassA uses ClassB then we provide a reference in ClassA References are declared just like other variables The data type of the reference variable will be the name of the class it refers to The Branch class requires a reference to the Employee class In this case because multiple employees may work for a branch we need the reference to be an array Employee [] employeeList; We will use employeeList to store Employee objects and access the methods of the Employee class

The full Class Diagram Branch - location : String - employeeCount : int + AddEmployee(employee : Employee) + GetEmployee(index : int) : Employee Reference is placed here Employee - name : String - department : String - salary : int + Employee(name : String, dept : String, salary : int) + IncreaseSalary(percent : int) + DecreaseSalary(percent : int) employeeList[ ] Note: parameterised constructors are normally shown on the class diagram

Coding the Branch class The reference is declared like any other instance variable private Employee[] employeeList; private String location; private int employeeCount; The actual object is created in the constructor public Branch(String location) { employeeList = new Employee[MAXEMPLOYEES]; this.location = location; employeeCount = 0; }

Adding new employees Objects can be passed into methods as parameters and used like any other variable/parameter public bool AddEmployee(Employee employee) { if (employeeCount < MAXEMPLOYEES) { employeeList[employeeCount] = employee; employeeCount++; return true; } else { return false; } Because the employee list is an array we need make sure we don’t overflow it’s size!

Returning a chosen employee Methods can also return values which are objects public Employee GetEmployee(int index) { if (index < employeeCount) { return employeeList[index]; } else { return null; }

Demonstration Bank.sln