1 / 18

Day 2.

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 { }

upton
Download Presentation

Day 2.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 2. Review of OOP. Collections. Assignment 1.

  2. 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).

  3. 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.

  4. 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

  5. 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?

  6. 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.

  7. Class Transform Set Get Client Program

  8. 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.

  9. 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

  10. 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.

  11. 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?

  12. 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….

  13. 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.

  14. 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; • }

  15. 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);

  16. 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.

  17. 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:

  18. 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?

More Related