180 likes | 421 Views
Day 2. Review of OOP. Collections. Assignment 1. 1. Review of OOP. OOP = O bject O riented P rogramming. An object is an instance of a class. To do OOP in C#, we need 2 items . (1) A service class , e.g.: public class Divide { }
E N D
Day 2. Review of OOP. Collections. Assignment 1.
1. Review of OOP. • OOP = Object Oriented Programming. • An object is an instance of a class. • To do OOP in C#, we need 2 items. • (1) A service class, e.g.: • public class Divide { • } • (2) A driver class that contains a driver which is a client of the class (it instantiates the service class as an object).
Multi-file programming. • The service class and driver class can be placed in the same file, but it is considered better practice to maintain them in separate files. • Why? • What does this have to do with control abstraction and data abstraction? • See the example from CSC250. • Service class: Division_Class.cs • Driver class: Class_Division_Driver.cs • Note: both files must be included in the project.
The service class. • The service class is not itself a program, but defines a reusable service or component. • Therefore, it should not be so specific it can only help to solve the problems of just one application. • E.g. class Calculate_Chuck_Norris_Taxes • On the other hand, it cannot be too vague. • E.g. class Do_Stuff
The service class. • The class should contain: • (1) data members, which are usually private. • -- What does this mean? • (2) member methods, which are usually public (there are important exceptions). • -- What does this mean?
Member methods may include: • (1) A constructor, a special method which is used to initialize the class’s data members, and which is called automatically when the class is instantiated. • (2) One or more Set methods which allow the client to send data to the class. The client’s data should be tested for reasonableness before assigning it to the member data (GIGO). • (3) Transformation methods, which map input data to some results. • (4) One or more Get methods which returns data from the class to the client driver.
Class Transform Set Get Client Program
The driver class. • The driver class contains the program driver (and optionally additional methods not in the service class). • The program driver is the client of the service class. • The program driver instantiates the service class as an object, e.g. • Divide D = new Divide (); • It then uses the methods of the class to solve some problem.
2. Collections. • A collection = “a structured data type that stores data and provides operations [on the data].” (McMillan). • Operations (methods) include: • Adding (inserting) data • Updating data • Removing data • Reporting (observing) on the state of data • E.g.? • Returning data
Types of collection. • A linear collection holds data in a sequence. • A nonlinear collection does not hold data in sequence. • Which of the following data structures is a linear collection, which is a nonlinear collection? • Stacks • Queues • Lists • Sets • Trees.
Types of storage. • The data in a collection can be stored in a static or dynamic format. • Static storage means….. • Dynamic storage means…. • C# allows arrays to be either static or dynamic. • A static approach makes insert / delete difficult. Why?
Linear / sequential collections (linear lists). • Linear lists arrange data in sequence (can be 1 way or 2 way from one end to the other). • Items are accessed by their index. • A stack is a linear list with access restricted to….. • The key operations are…. • A queue isa linear list with access restricted to….. • The key operations are….
Nonlinear collections. • In nonlinear collections, data is not in sequence, and may either be (a) ordered in a different way (hierarchically, like the military) • or • (b) be unorderedgroups (like letters in a Scrabble bag) : • (a) hierarchical e.g. a module chart / binary tree. • (b) groups e.g. sets, graphs, networks.
Generic programming. • The idea of generic programming is to create code that can be reused with different data types. • The simplest example is a generic method. • E.g., see text p. 15: • static void Swap<T> (ref T val1, ref T val2) { • T temp; • temp = val1; • val1 = val2; • val2 = temp; • }
Generic programming. • The method uses a generic placeholder, or template data type, T that can stand for any data type. We then substitute in the type of elements we want to swap • Swap <int> (ref num1, ref num2); • Swap<string> (ref str1, ref str2);
Generic programming. • The same approach can be used for classes. • For example, we will develop stack class code that will work for a stack of any type of elements. • public class Stack<T> { • } • What is the point of this? • Avoiding “code bloat” from the Department of Redundancy Department.
3. Assignment 1. • For assignment 1 you do NOT need to use generic programming. • Rather, you can use the 2 files, Division_Class.cs and Class_Division_Driver.cs as examples. • Work through the code in detail. • Begin initial design of solution for assignment 1:
Design considerations. • In your service class: • (1) what private data do you need? • (2) what methods do you need? • What does the client need to provide? • What does the service class need to find?