Domain Classes Chapter 9.

Slides:



Advertisements
Similar presentations
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Advertisements

C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Multiple Choice Solutions True/False a c b e d   T F.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Agenda Object Oriented Programming Reading: Chapter 14.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Inheritance ITK 169 Fall 2003 Base Class Also called the Parent Class This is the class that later classes will be build on. Suppose the Red Bird Rec.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Java: Base Types All information has a type or class designation
Object Oriented Programming
Chapter 14: More About Classes.
Java: Base Types All information has a type or class designation
Object-Oriented Programming: Classes and Objects
Haskell Chapter 2.
Chapter 6: The Stack Abstract Data Type
Inheritance and Polymorphism
Writing Classes Chapter 4.
Intro To Classes Review
Agenda Warmup AP Exam Review: Litvin A2
Singleton Pattern Command Pattern
Introduction to Classes
Object-Oriented Programming
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Object-Oriented Programming: Classes and Objects
Templates.
CS Week 13 Jim Williams, PhD.
Chapter 14: More About Classes.
Object Oriented Programming
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Classes
Chapter 4: Writing classes
null, true, and false are also reserved.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
CS139 October 11, 2004.
Java Inheritance.
CS 350 – Software Design Singleton – Chapter 21
CS3220 Web and Internet Programming Expression Language (EL)
Method of Classes Chapter 7, page 155 Lecture /4/6.
Object Oriented Programming in java
CIS 199 Final Review.
Class.
CS3220 Web and Internet Programming Expression Language (EL)
OO Programming Concepts
CMPE212 – Reminders Assignment 2 due next Friday.
Chapter 5 Classes.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Domain Classes Chapter 9

You will learn to: 1. Code a Domain Class 2. Use the Domain Class within a Client Class (Form1) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods …which will instantiate objects 1111 Doe John JD true stu1 Doe John true JD

Domain Class Student (UML) Student Student - C# code Fields Methods public class Student { //Fields //Properties //Constructors //Methods } Student - C# code

Instance Fields Student (UML) Student Student - C# code stu1 stu2 1111 -idNum -lastName -firstName -inGoodStanding -initials Student Methods public class Student { //Fields--------------------------------------------- //Instance Fields (nonstatic) private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; } Student - C# code stu1 stu2 1111 Doe John true JD 2222 Miller Sue false SM

Static Fields – i.e. at the Class level Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods COST_PER_HOUR public class Student { //Fields---------------------------------------------- //Instance Fields (nonstatic) private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; //Static Fields (i.e. class-level) public const double COST_PER_HOUR = 300.00; private static int totalStudents; } Student - C# code 300.00 totalStudents 2 1111 Doe John JD true 2222 Miller Sue SM false stu1 stu2 Doe John true JD Miller Sue false SM A note on Constants: Constants are static by default. C# does not support const properties. So, you must make the field public.

Properties Fields are private BUT we still need a way to give other classes access to the data The solution is Properties: Properties are similar to both: Like a Field – it can read/update data Like a Method – it can contain code Types of Properties Standard – explicitly written Auto-Implemented – the easy version public class Student { //Fields--------------------------------------- private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; public const double COST_PER_HOUR = 300.00; private static int totalStudents; //Properties--------------------------------- } Student - C# code

Properties You have used Properties before…

Properties Student C# field Form property LastName { } lastName Student set get Form Last name: Smith Save get: fetches lastName: Doe set: updates lastName: Smith C# //Field private string lastName; //Property public string LastName { get { return lastName; } set { lastName = value; } } Standard Property - explicitly written

Properties – 2 approaches field property LastName { } lastName Standard Property - explicitly written set get C# Non-accessible Backing field Auto-Implemented Property //Field private string lastName; //Property public string LastName { get { return lastName; } set { lastName = value; } } //Field private string lastName; //Property public string LastName { get; set; } NOTE: Preferred way to get started!

Properties    Standard Property Auto-implemented property Basic – get & set Basic – get & set public string LastName { get; set; } public string LastName { get { return lastName; } set { lastName = value; } } vs.  Additional Code Additional Code public string LastName { get { return lastName; } set { lastName = value; UpdateInitials(); } } Cannot add additional code in an auto-implemented property vs.  Read Only Read Only public string Initials { get { return initials; } } public string Initials { get; private set; } vs. 

Writing Properties – Step 1: Try to make them all Auto-Implemented Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods public class Student { //Fields---------------------------------------------- public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Properties--------------------------------------------------- //Instance Properties public int IdNum { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public bool InGoodStanding { get; set; } public string Initials { get; set; } //Static Properties public static int TotalStudents { get; set; }

Writing Properties – Step 2: Decide which ones require code or are read only Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Properties--------------------------------------------------- //Instance Properties public int IdNum { get; set; } public string LastName get { return lastName; } set { lastName = value; UpdateInitials(); } public string FirstName get { return firstName; } set { firstName = value; UpdateInitials(); } public bool InGoodStanding { get; set; } public string Initials { get; private set; } //Static Properties public static int TotalStudents { get; set; } Update initials Update initials Read only

Constructors Student (UML) Student Student - C# code 4 totalStudents Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) Student Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Constructors------------------------------------------------ public Student() InGoodStanding = true; TotalStudents++;   public Student(int studId, string fname, string lname) IdNum = studId; FirstName = fname; LastName = lname; IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static 1111 Doe John JD true stu3 true 2222 Miller Sue SM false stu2 SM stu4 stu1 JD 4 totalStudents

Methods Student - C# code Student (UML) Student Fields Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Methods-------------------------------------------------- //Instance Methods private void UpdateInitials() if (FirstName != null && LastName != null) Initials = FirstName.Substring(0, 1) + LastName.Substring(0, 1);   public string GenEmailAddress() string email = ""; if (FirstName != "") email = FirstName + "@abc.edu"; return email; public string ReverseName() string formattedName = LastName + ", " + FirstName; return formattedName; //Static Methods public static List<Student> GetNamesStartingWith() //see the code for this on the next page… IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static Student() Student(StudID, fname, lname) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods

Methods Student - C# code Student (UML) Student Fields Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Methods-------------------------------------------------- //Instance Methods private void UpdateInitials() if (FirstName != null && LastName != null) Initials = FirstName.Substring(0, 1) + LastName.Substring(0, 1);   public string GenEmailAddress() string email = ""; if (FirstName != "") email = FirstName + "@abc.edu"; return email; public string ReverseName() string formattedName = LastName + ", " + FirstName; return formattedName; //Static Methods public static List<Student> GetNamesStartingWith() //see the code for this on the next page… IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static Student() Student(StudID, fname, lname) UpdateInitials() GenEmailAddress() ReverseName() GetNameStartWith() Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods

Methods //Static Methods public static List<Student> GetNamesStartingWith(List<Student> students, string pattern) { int patternLen; List<Student> patternMatchStudents = new List<Student>(); patternLen = pattern.Length; foreach (Student aStudent in students) if (aStudent.lastName.Substring(0, patternLen) == pattern) patternMatchStudents.Add(aStudent); } return patternMatchStudents;

UI Design – with errors  Class Instance Properties Methods Values produced via Methods Class  Instance field but read-only Static field – not instance In red: intentionally showing mistakes in the UI Design. Some fields on the domain class are static fields & thus do not pertain to an individual student object). Some fields are read only & the client class cannot update its values.

Ch9Ex1 - Practice Coding a Domain Class Open project: Ch9Ex1-DomainClass Create the class called Student  Code the Fields/Properties -code all fields in the UML Diagram as auto- implemented properties then decide if you need to change anything Id Num Last Name - when this is updated also execute UpdateInitials() First Name - when this is updated also execute UpdateInitials() In Good Standing - a boolean Initials - read only Cost Per Hour - a constant set to 300.00 Total Students - a static (i.e. class level) field  Code the Constructors 1st Constructor - no parameters; sets In Good Standing to true in the new object & and increments Total Students 2nd Constructor - 3 parameters (student id, first name, last name); update these values in the new object & and increments Total Students   Code the Methods UpdateInitials() - returns nothing but directly updates Initials if Last Name & First Name are not empty GenEmailAddress() - returns the email address (firstname@abc.edu) ReverseName() - returns a concatenated name: LastName + ", " + FirstName GetNamesStartingWith(list of students, pattern) - a "static" method that returns a list of students whose last name starts with the pattern provided – skip this method as it is not used in this form (but you might add it later for more practice) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods

Ch9Ex2 - Practice Using the Domain Class Client Class: Form1 Open project: Ch9Ex2-ClientClass Code the Submit button to utilize the Student class to do the following: Instantiate (i.e. create) a student object Fill the Object –with the data from the Form Display the object – in a label Re-Display the Total number of students (from the Student class) Code the Clear button to: Just invoke LoadInitialValues() This has the Domain class already coded