Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented (Component-Based) Program Design Process

Similar presentations


Presentation on theme: "Object-Oriented (Component-Based) Program Design Process"— Presentation transcript:

1 Object-Oriented (Component-Based) Program Design Process
CSE 101 Yinong Chen

2 Table of Contents 1 Paradigms of Computing & Languages 2
Design Process of an OO Program 3 Class and Objects from a Class 4 Implement the Solution in C# 5 Interface: Console vs. GUI

3 Granularity of Abstraction
Granularity Level Programming-in-the-large Service-Oriented Programming Java, C#, VPL Logic Programming e.g. Prolog Functional Programming e.g. LISP/Scheme Object-Oriented Programming C++, Java, C# Component-Based Programming Alice, LabView C++, Java, C# Procedural Programming Pascal, C Imperative Programming Assembly, Algo, FORTRAN 50s 60s 70s 80s 90s 00s 10s Time Programming-in-the-small 22 September September 2018

4 Object-Oriented Paradigm
What is a Program? Program Algorithm Data Data Process (steps) of data manipulation Objects of the manipulation Emphasis: Imperative / Procedural Paradigms Emphasis: Object-Oriented Paradigm

5 Imperative Paradigm Fully specified and fully controlled manipulation of named data in a step-wise fashion. Developed as abstractions of von Neumann machine (stored program concept). Programs are algorithmic in nature: do this, then that, then repeat this ten times -- focuses on how rather than what. Why popular Performance – match the machine Culture – reading manuals

6 Features of Object-Oriented Programming
Data are organized in Object/Class: Operations are part of an object for manipulating the data in the object; Abstract data type (class): Encapsulation of state in an object that can only be accessed through operations defined on them. Clean interface -- public and private components. Inheritance: extending a class by keeping the unchanged parts. Supports code reuse. Classes are organized in hierarchies. Dynamic memory allocation and de-allocation Dynamic binding Polymorphism

7 Traditional Software Design Process
Software development What is the need of users Requirement analysis What needs to be created to meet the user’s need? Specification How to do it? (Algorithms) Alternative designs Implementation (coding) How to code it? (programming ) Testing / Evaluation Run it in a virtual environment Deployment Put it in the real environment 22 September September 2018

8 Object-Oriented / Component-Based Software Development
Bricks and Tiles traditional Component- based 22 September September 2018

9 Object-Oriented and Service-Oriented Software Development
Requirement analysis Programmers Programmers Services development Object-oriented development Problem decomposition Service repository Services testing Component library Object testing Application builder Application building Testing To be discussed later Deployment

10 Travel Preparation Problem Definition (Requirements)
Input: The numbers of days to travel; The country name; The local temperature in Celsius; Output: The amount of local currency needed The local temperature in Fahrenheit

11 Problem Decomposition
Main class Enter numbers of days to travel; Enter the country name Make use of other classes to perform computation; Print output; days amount in USD Fahrenheit Celsius Amount in local currency Hotel cost in USD; Rental car cost in USD; Meal cost in USD; Total cost Convert Celsius to Fahrenheit; Convert Fahrenheit to Celsius; amount in USD TemperatureConversion class myCost class Convert USD amount into local currency amount; CurrencyConversion class

12 Class Members A class consists of a list of members Each member can be
a data member, or called a variable a function member, or called a method Function members Constructor: is a function member that has the same name as the class name. It is used to Initialize the data members in the class Pass values into the class, if the values will be used by multiple function members Other function members are used Manipulate data members; Provide reusable functions for other classes to call;

13 Class versus Objects A class is a structural design, or a called blueprint A member can be private or public private members can be accessed in the class only Public members can also be accessed outside the class Static member: If static keyword is used, the member can be accessed without instantiation: className.memberName A class can be used to instantiate one or more objects ClassName refName = new ClassName(); refName.memberName to access the member A set of library functions (methods) are grouped in one class; A group of classes are organized as a namespace

14 using System; class TravelPreparation { static void Main(string[] args) { // The main method Console.WriteLine("Please enter the number of days you will travel"); String str = Console.ReadLine(); // read a string of characters Int32 daysToStay= Convert.ToInt32(str); // Convert string to integer myCost usdObject = new myCost(daysToStay); // Create an object int usdCash = usdObject.total(); // Call a method in the object Console.WriteLine("Please enter the country name you will travel to"); String country = Console.ReadLine(); CurrencyConversion exchange = new CurrencyConversion(); Double AmountLocal = exchange.usdToLocalCurrency(country, usdCash); Console.WriteLine("The amount of local currency is: " + AmountLocal); Console.WriteLine("Please enter the temperature in Celsius"); str = Console.ReadLine(); Int32 celsius = Convert.ToInt32(str); TemperatureConversion c2f = new TemperatureConversion(); Int32 fahrenheit = c2f.getFahrenheit(celsius); Console.WriteLine("The local temperature in Fahrenheit is: " + fahrenheit); } Main Class is the class with the Main method

15 TemperatureConversion c2f = new TemperatureConversion();
Class name Return type Method name No constructor class TemperatureConversion { public Int32 getFahrenheit(Int32 c) Double f = c * 9 / ; return Convert.ToInt32(f); } public Int32 getCelsius(Int32 f) Double c = (f - 32) * 5 / 9; return Convert.ToInt32(c); Method 1 Parameter passing into function directly Methods2 TemperatureConversion c2f = new TemperatureConversion(); Int32 fahrenheit = c2f.getFahrenheit(celsius);

16 Reference to an Object TemperatureConversion c2f = new TemperatureConversion(); Int32 fahrenheit = c2f.getFahrenheit(celsius); class TemperatureConversion { public Int32 getFahrenheit(Int32 c) Double f = c * 9 / ; return Convert.ToInt32(f); } public Int32 getCelsius(Int32 f) Double c = (f - 32) * 5 / 9; return Convert.ToInt32(c); 42F1D761C c2f 42F1D761C c2f.getFahrenheit(23); c2f. getCelsius(98);

17 class myCost { private Int32 days; public myCost(Int32 daysToStay) { days = daysToStay; } private Int32 hotel() { return 100 * days; private Int32 rentalCar() { return 30 * days; private Int32 meals() { return 20 * days; public Int32 total() { return hotel() + rentalCar() + meals(); Constructor: is a method that has the same name as the class. Allow to pass a value into the class and used by all methods Data member The value is provided when an object is created. myCost usdObject = new myCost(daysToStay); int usdCash = usdObject.total();

18 class CurrencyConversion {
public Double usdToLocalCurrency(String country, Int32 usdAmount) { switch(country) { case "Japan": return usdAmount * 117; case "EU": return usdAmount * 0.71; case "Hong Kong": return usdAmount * 7.7; case "UK": return usdAmount * 0.49; case "South Africa": return usdAmount * 6.8; default: return -1; } This class does not need to have a constructor: It has no data member; The parameter is passed to the function member, instead of into the class for all member functions to use. There is one function member only in this class. If there would be more methods and all methods use the same parameters, we would want to pass the parameters into the class, instead of into individual methods.

19 HW 1: Question 1 Anti Block System and Traction Control of a Car
Main class Get sensor values from Sensors class; Compare sensor values Take action by calling Actuator class; Power level velocities Brake force level Read wheel speed; Read body Speed; Control break Control engine make wheelDiameter; mpg; tankSize baseRental Sensors class Actuators class Cars class

20 class Program { static void Main(string[] args) Console.WriteLine("Please enter the make of the car"); String make = Console.ReadLine(); // read a string, e.g. bmw Console.WriteLine("Please enter the miles to drive"); String str = Console.ReadLine(); // read a string, e.g, 1000 Int32 miles = Convert.ToInt32(str); Cars cRef = new Cars(make); // create an object of Cars Double cost = cRef.rentalCost(miles); Console.WriteLine("The cost of driving is " + cost); Sensors sRef = new Sensors(cRef); // create an object of Sensors Actuators aRef = new Actuators(); // create an object of Actuators for (Int32 i = miles; i > 0; i= i - 10) { Double wv = sRef.wheel_velocity(); Double bv = sRef.body_velocity(); //Console.WriteLine("wv = {0} bv = {1}", wv, bv); if (Math.Abs(bv - wv) < 1) // 1 is the tolerance Console.WriteLine("no action"); else if (bv > wv) aRef.brakeForce(); else aRef.enginPower(); }

21 class Cars { private Double wheelDiameter; private Double mpg; // miles per galon private Double tank; private Double baseRental; public Double rentalCost(Int32 m) { return baseRental + (m / mpg) * 3.0; // $3/gal } public Cars(String make) { if (make == "bmw") { wheelDiameter = 17; mpg = 20; // miles per gallon tank = 25; // gallons of gasoline baseRental = 100; else { wheelDiameter = 15; mpg = 24; baseRental = 50;

22 class Sensors { private Double wheel_diameter; // inches Random randomizer; public Sensors(Cars carRef) wheel_diameter = carRef.getDiameter(); randomizer = new Random(); } public Double wheel_velocity() { Double mile_inch = ; // inches per mile Double pi = Math.PI; Double rps = randomizer.Next(25); Double wv = (pi * wheel_diameter * rps * 3600) / mile_inch; return wv; public Double body_velocity() { Int32 bv = randomizer.Next(70); return bv;

23 class Actuators { public void brakeForce() Console.WriteLine("reduce brake force!"); } public void enginPower() Console.WriteLine("reduce engine power level!");

24 Console Interface versus Graphic User Interface


Download ppt "Object-Oriented (Component-Based) Program Design Process"

Similar presentations


Ads by Google