C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.

Slides:



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

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
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.
Advanced Object-Oriented Programming Features
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
ASP.NET Programming with C# and SQL Server First Edition
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 8: User-Defined Classes and ADTs
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
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?
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
C# Programming: From Problem Analysis to Program Design1 4 Methods and Behaviors.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
C# Programming: From Problem Analysis to Program Design
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Introduction to Object Oriented Programming Chapter 10.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 4 Methods and Behaviors Microsoft Visual C#.NET: From Problem Analysis to Program.
Class Fundamentals BCIS 3680 Enterprise Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-Oriented Programming (part 1 – Data Encapsulation)
Creating Your Own Classes
Classes (Part 1) Lecture 3
Advanced Object-Oriented Programming Features
Reference: COS240 Syllabus
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Creating Your Own Classes
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 8: User-Defined Classes and ADTs
Data Types and Expressions
CIS 199 Final Review.
Presentation transcript:

C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition 4

C# Programming: From Problem Analysis to Program Design2 Chapter Objectives Become familiar with the components of a class Learn about the different methods and properties used for object-oriented development Create and use constructors Write your own instance methods to include mutators and accessors Call instance methods including mutators and accessors Work through a programming example that illustrates the chapter’s concepts

C# Programming: From Problem Analysis to Program Design3 The Object Concept Solution is defined in terms of a collection of cooperating objects Class serves as template from which many objects can be created Abstraction –Attributes (data) –Behaviors (processes on the data)

Private Member Data All code you write is placed in a class When you define a class, you declare instance variables or fields that represent state of an object –Fields are declared inside the class, but not inside any specific method –Fields become visible to all members of the class, including all of the methods Data members are defined to have private access C# Programming: From Problem Analysis to Program Design4

Private Member Data ( continued ) public class Student { private int studentNumber; private string studentFirstName; private string studentLastName; private int score1; private int score2; private int score3; private string major; C# Programming: From Problem Analysis to Program Design5

Add a Class Use the PROJECT menu or the Solution Explorer Window Right-mouse click and select the option Add, Class Solution Explorer Window enables you to create a class diagram C# Programming: From Problem Analysis to Program Design6

Class Diagram C# Programming: From Problem Analysis to Program Design7 Figure 4-1 Student class diagram created in Visual Studio Open the Class Diagram from Solution Explorer, View Class Diagram

Class Diagram ( continued ) C# Programming: From Problem Analysis to Program Design 8 Figure 4-2 Student class diagram details After the class diagram is created, add the names of data members or fields and methods using the Class Details section Right click on class diagram to open Class Details window

Class Diagram ( continued ) When you complete class details using the Class Diagram tool, code is automatically placed in the file. C# Programming: From Problem Analysis to Program Design9 Figure 4-3 Auto generated code from Student class diagram

Constructor Special type of method used to create objects –Create instances of class Instantiate the class Constructors differ from other methods –Constructors do not return a value Also do not include keyword void –Constructors use same identifier (name) as class Constructors use public access modifiers C# Programming: From Problem Analysis to Program Design10

Constructor ( continued ) public access modifier is always associated with constructors //Default constructor public Student ( ) { } //Constructor with one parameter public Student (int sID ) { studentNumber = sID; } C# Programming: From Problem Analysis to Program Design11

Constructor ( continued ) //Constructor with three parameters public Student (string sID, string firstName, string lastName) { studentNumber = sID; studentFirstName = firstName; studentLastName = lastName; } Design classes to be as flexible and as full-featured as possible –Include multiple constructors C# Programming: From Problem Analysis to Program Design12

Writing Your Own Instance Methods Constructors Accessors Mutators Other methods to perform behaviors of the class C# Programming: From Problem Analysis to Program Design13

C# Programming: From Problem Analysis to Program Design14 Writing Your Own Instance Methods Do not use static keyword –Static associated with class method Constructor – special type of instance method –Do not return a value –void is not included –Same identifier as the class name –Overloaded –Default constructor No arguments Write one constructor and you lose the default one

C# Programming: From Problem Analysis to Program Design15 Accessor Getters Returns the current value Standard naming convention → prefix with “get” –Accessor for noOfSquareYards public double GetNoOfSqYards( ) { return noOfSqYards; } Properties serve purpose Accessor

Mutators Setters Normally includes one parameter Method body → single assignment statement Standard naming convention → prefix with ”Set” Can be overloaded C# Programming: From Problem Analysis to Program Design16

C# Programming: From Problem Analysis to Program Design17 Mutator Examples public double SetNoOfSqYards(double squareYards) { return noOfSquareYards; } public void SetNoOfSquareYards(double length, double width) { noOfSquareYards = squareYards; } Mutator Overloaded

Other Instance Methods No need to pass arguments to these methods – –Defined in the same class as the data members –Instance methods can directly access private data members Define methods as opposed to storing values that are calculated from other private data members public double CalculateAverage( ) { return (score1 + score2 + score3) / 3.0; } C# Programming: From Problem Analysis to Program Design18

C# Programming: From Problem Analysis to Program Design19 Property Can replace accessors and mutators Properties looks like a data field –More closely aligned to methods Standard naming convention in C# for properties –Use the same name as the instance variable or field, but start with uppercase character Doesn’t have to be the same name – no syntax error will be thrown

Property public double NoOfSqYards { get { return noOfSqYards; } set { noOfSqYards = value; } C# Programming: From Problem Analysis to Program Design20

Property ( continued ) If an instantiated object is named berber, to change the NoOfSqYards, write: berber.NoOfSqYards = 45; C# Programming: From Problem Analysis to Program Design21

C# Programming: From Problem Analysis to Program Design22 ToString( ) Method All user-defined classes inherit four methods from the object class –ToString( ) –Equals( ) –GetType( ) –GetHashCode( ) ToString( ) method is called automatically by several methods –Write( ) –WriteLine( ) methods Can also invoke or call the ToString( ) method directly

C# Programming: From Problem Analysis to Program Design23 ToString( ) Method ( continued ) Returns a human-readable string Can write a new definition for the ToString( ) method to include useful details public override string ToString( ) { // return string value } Keyword override added to provide new implementation details Always override the ToString( ) method –This enables you to decide what should be displayed if the object is printed

ToString( ) Example public override string ToString( ) { return "Price Per Square Yard: " + pricePerSqYard.ToString("C") + "\nTotal Square Yards needed: " + noOfSqYards.ToString("F1") + "\nTotal Price: " + DetermineTotalCost( ).ToString("C"); } C# Programming: From Problem Analysis to Program Design24

ToString( ) method Sometimes useful to add format specifiers as one of the arguments to the Write( ) and WriteLine( ) methods – Invoke ToString( ) –Numeric data types such as int, double, float, and decimal data types have overloaded ToString( ) methods pricePerSqYard.ToString("C") C# Programming: From Problem Analysis to Program Design25

C# Programming: From Problem Analysis to Program Design26 Calling Instance Methods Instance methods are nonstatic method –Call nonstatic methods with objects – not classes Static calls to members of Math and Console classes answer = Math.Pow(4, 2) Console.WriteLine( ) If you need to invoke the method inside the class it is defined in, simply use method name If you need to invoke the method outside the class it is defined in, precede method name with object identifier berber.GetNoOfSquareYards( )

Calling the Constructor Normally first method called Creates an object instance of the class ClassName objectName = new ClassName(argumentList); or ClassName objectName; objectName = new ClassName(argumentList); Keyword new used as operator to call constructor methods CarpetCalculator plush = new CarpetCalculator ( ); CarpetCalculator pile = new CarpetCalculator (37.90, 17.95); C# Programming: From Problem Analysis to Program Design27

C# Programming: From Problem Analysis to Program Design28 Constructor ( continued ) Default values are assigned to variables of the value types when no arguments are sent to constructor Table 4-1 Value type defaults

C# Programming: From Problem Analysis to Program Design29 Calling Accessor and Mutator Methods Method name is preceded by the object name berber.SetNoOfSquareYards(27.83); Console.WriteLine("{0:N2}", berber.GetNoOfSquareYards( )); If property defined, can use property instead of accessor and/or mutators Using properties PropertyName = value; // Acts like mutator here Console.Write( " Total Cost at {0:C} ", berber.Price); // Acts like accessor here

C# Programming: From Problem Analysis to Program Design30 Calling Other Instance Methods Call must match method signature If method returns a value, must be a place for a value to be returned Student aStudentObject = new Student("1234", "Maria", "Smith", 97, 75, 87, "CS"); average = aStudentObject.CalculateAverage( ); No arguments needed as parameters to the CalculateAverage( ) method. CalculateAverage( ) is a member of the Student class and has full access to all Student members.

Testing Your New Class Different class is needed for testing and using your class Test class has Main( ) in it Construct objects of your class Use the properties to assign and retrieve values Invoke instance methods using the objects you construct C# Programming: From Problem Analysis to Program Design31

Calling the Constructor Method C# Programming: From Problem Analysis to Program Design32 Figure 4-4 Intellisense displays available constructors

Using Public Members C# Programming: From Problem Analysis to Program Design33 Figure 4-5 Public members of the Student class

StudentApp C# Programming: From Problem Analysis to Program Design34 Review StudentApp Project Figure 4-6 Output from StudentApp

Test Class With multiclass solutions all input and output should be included in the class that has the Main( ) method –Eventual goal will be to place your class files, like Student and CarpetCalculator, in a library so that the classes can be used by different applications Some of these applications might be Windows applications; some may be console applications; others may be Web application Do not include ReadLine( ) or WriteLine( ) in your class methods C# Programming: From Problem Analysis to Program Design35

Testing Your New Class C# Programming: From Problem Analysis to Program Design36 Review CarpetCalculator Project Figure 4-7 Output from Carpet example using instance methods

C# Programming: From Problem Analysis to Program Design37 RealEstateInvestment Example Figure 4-8 Problem specification for RealEstateInvestment example

C# Programming: From Problem Analysis to Program Design38 Data for the RealEstateInvestment Example Table 4-2 Instance variables for the RealEstateInvestment class

C# Programming: From Problem Analysis to Program Design39 Data for the RealEstateInvestment Example ( continued ) Table 4-3 local variables for the property application class

C# Programming: From Problem Analysis to Program Design40 RealEstateInvestment Example ( continued ) Figure 4-9 Prototype

C# Programming: From Problem Analysis to Program Design41 RealEstateInvestment Example ( continued ) Figure 4-10 Class diagrams

C# Programming: From Problem Analysis to Program Design42 RealEstateInvestment Example ( continued ) Table 4-4 Properties for the RealEstateInvestment class

C# Programming: From Problem Analysis to Program Design43 Figure 4-11 Structured English for the RealEstateInvestment example RealEstateInvestment Example ( continued )

Class Diagram C# Programming: From Problem Analysis to Program Design44 Figure 4-12 RealEstateInvestment class diagram

Test and Debug C# Programming: From Problem Analysis to Program Design45 Figure 4-13 Output from RealEstate Investment example View RealEstateInvestment

Coding Standards Naming Conventions –Classes –Properties –Methods Constructor Guidelines Spacing Guidelines C# Programming: From Problem Analysis to Program Design46

Resources C# Coding Standards and Best Practices – – C# Station Tutorial - Introduction to Classes – – Object-Oriented Programming – – Introduction to Objects and Classes in C# – Objects-and-Classes-in-C-sharp/ Objects-and-Classes-in-C-sharp/ C# Programming: From Problem Analysis to Program Design47

C# Programming: From Problem Analysis to Program Design48 Chapter Summary Components of a method Class methods –Parameters Predefined methods Value- and nonvalue-returning methods

C# Programming: From Problem Analysis to Program Design49 Chapter Summary ( continued ) Properties Instance methods –Constructors –Mutators –Accessors Types of parameters