Dr Shahriar Bijani Winter 2017

Slides:



Advertisements
Similar presentations
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
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.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Jozef Goetz,  2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2015 credits:
A DVANCED P ROGRAMMING C HAPTER 4: I NTRODUCTION TO I NTRODUCTION TO C LASSES, O BJECTS, M ETHODS AND STRINGS Dr Shahriar Bijani Winter 2016.
A DVANCED P ROGRAMMING C HAPTER 5 & 6: C ONTROL S TRUCTURES Dr Shahriar Bijani Spring 2016.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Introduction to Classes and Objects
Advanced Programming Chapter 7: Methods: A Deeper Look
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Advanced Programming Lecture 02: Introduction to C# Apps
IFS410: Advanced Analysis and Design
Advanced Programming Chapters 5 & 6: Control Structures
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
4 Introduction to Classes and Objects.
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Data Types Imran Rashid CTO at ManiWeber Technologies.
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Chapter 3 Introduction to Classes, Objects Methods and Strings
Visual Programming Lecture 4.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Classes and Objects
Presentation transcript:

Dr Shahriar Bijani Winter 2017 Advanced Programming Chapter 4: Introduction to Classes, Objects & Methods Dr Shahriar Bijani Winter 2017

References Visual C# 2012 How to Program, Paul Deitel & Harvey Deitel, 5th Edition, Prentice Hall.

4.2 Classes, Objects, Methods, Properties and Instance Variables Review of classes: Car example Methods describe the mechanisms that perform a tasks (e.g. acceleration) Hide complex tasks from the user; a driver does not need to know how the accelerator works but can use it. Classes must be defined before use; a car must be built before it can be driven Many objects can be created from the same class; many cars can be built from same engineering diagram

4.2 Classes, Objects, Methods, Properties and Instance Variables Method calls send messages to an object to perform tasks; pressing the gas pedal sends a message to the car to accelerate Objects have attributes; cars have color and speed gauge, each car knows how much fuel is in its own tank, but not how much is in the tanks of other cars. Properties, get Accessors and set Accessors The attributes are not necessarily accessible directly. The car manufacturer does not want drivers to access the car’s engine to observe the amount of fuel in its tank.

A Class Example 1 // Fig. 4.1: GradeBook.cs 2 // Class declaration with one method. 3 using System; 4 5 public class GradeBook 6 { 7 // display a welcome message to the GradeBook user 8 9 { 10 Console.WriteLine( "Welcome to the Grade Book!" ); 11 } // end method DisplayMessage 12 } // end class GradeBook

Create and Call an Object 1 // Fig. 4.2: GradeBookTest.cs 2 // Create a GradeBook object and call its DisplayMessage method. 3 public class GradeBookTest 4 { 5 // Main method begins program execution 6 public static void Main( string[] args ) 7 { 8 // create a GradeBook object & assign it to myGradeBook 9 GradeBook myGradeBook = new GradeBook(); 10 11 // call myGradeBook's DisplayMessage method 12 myGradeBook.DisplayMessage(); 13 } // end Main 14 } // end class GradeBookTest Fig. 4.2: Create a GradeBook object and call its DisplayMessage method. Welcome to the Grade Book!

4.4 Declaring a Method with a Parameter

4.4 Declaring a Method with a Parameter

UML Class Diagrams UML class diagram of class GradeBook with a public DisplayMessage operation Fig 4.6 GradeBook has a public DisplayMessage operation with a courseName parameter of type string.

4.5 Instance Variables and Properties

Software Engineering Observation 4.2 Precede every field and method declaration with an access modifier. Generally, instance variables should be declared private and methods and properties should be declared public. No access modifier = private declare a private method, if it will be accessed only by other methods of the class. Software Engineering Observation 4.3 private member variables and public methods and properties helps debugging because problems with data manipulations are localized to the class’s methods and properties, since the private member variables are accessible only to these methods and properties.

UML Class Diagram with a Property

4.8 Auto-Implemented Properties public string CourseName { get; set; } the C# compiler can automatically create a private instance variable, and the get and set accessors for it. Unlike a user-defined property, an auto-implemented property, must have both a get and a set accessor.

4.9 Value Types vs. Reference Types A value type variable contains a value of that type! C#’s simple types (like int and double) are all value types. int count = 7; All simple types, enum and struct type are value types Value types bool byte char decimal double enum float int long sbyte short struct uint ulong ushort

4.9 Value Types vs. Reference Types A reference variable contains the address of a location in memory. GradeBook myGradeBook = new GradeBook(); Reference-Type Instance Variables Initialized to null string is a reference type. value null is not an empty string ( "" or string.Empty)

4.10 Initializing Objects with Constructors Constructors methods can initialize an object C# requires a constructor call for every object that is created. The new operator calls the constructor.

4.11 Floating-Point Numbers and Type decimal type float represent precision with 7 significant digits. type double represent precision with15–16 significant digits Type decimal provides 28–29 significant digits. To type a decimal literal, you must type the letter “M” or “m” decimal d = 2.0M