Sunteți pe pagina 1din 12

BIS3515 / BIS3516

Object Oriented Programming

Anan Osothsilp

Phd, Electrical and Computer Engineering

Semester: 2- 2014
Object Oriented Programming

Lab #1

Cover topics:
-Data vs Information
-Minimal Class Structure
-Information declaration (memory allocation for basic data type)
-Object creation
-Data getter and setter statements

-Basic Introduction for Sharpdevelop


-Program compilation and execution
-Basic input and output commands for console application
Object Oriented Programming: Data vs Information

has name as Information

Student has
gpa as Information

has
Business Entity/Class gender as Information

For example : (Extract data from the following statements )


Data
Andy is a male student. His gpa is 3.0.
Information Data
Name Andy
Gpa 3.0
Gender male
Object Oriented Programming: Memory allocation

Information Data memory


Name Andy
Gpa 3.0
Gender male

Memory allocation, In Programming language is called Variable declaration


Or Information declaration. Where the name and data type are required.

1 Data type Support Data

2 int Integer number Eg. 1,4,10,100,

3 double Real number 1.5, 2.75, 3.333

4 string text “Love”, “Joy”


“Good morning”
Object Oriented Programming: Memory allocation

gpa
Information Data memory
Name Andy
name 0x38afd
Gpa 3.0 gender

Gender male 0x14afe


0x44bcc

1. Allocate memory space for each information


(exact space address is 16th base number and difficult to remember)

In C# language, Note that each memory allocation requires

Data type Name

string name ; //memory allocation for name. Use string to support Text type
double gpa; //memory allocation for gpa, double is data type to support real number
string gender; //memory allocation for gender. Use string to support Text type
Object Oriented Programming: Memory allocation

gpa
Information Data memory
Name Andy
name 0x38afd
Gpa 3.0 gender

Gender male 0x14afe


0x44bcc

Creating memory space to support 10 students, in this case, requires 30 variable


Declarations statements

int student1_name; int student1_gpa; int student1_gender;


int student2_name; int student2_gpa; int student2_gender;
: : :
: : :
Int student10_name; Int student10_gpa; Int student10_gender;

Make them very difficult to manage as number of students increase.

To fix this, we introduce new memory allocation based on OOP concept


Object Oriented Programming: OOP memory allocation

Information Data
Name Andy
Gpa 3.0
Gender male

New Memory allocation requires the following steps

1. Create a new class structure as storage template

2. Add memory allocation for each information inside the class structure

3. Use Object creation statement to allocate memory space for each student
Object Oriented Programming: OOP memory allocation

Information Data
Name Andy
Gpa 3.0
Gender male

Step1: Create variable


Step2: empty Class
declaration
structure
statements
for Student
to support each information

class Student { //denote the begin of class


string name;
string gender;

double gpa;

} //denote the end of class


Object Oriented Programming: OOP memory allocation

memory
gpa gpa
name name
gender gender
s1
s2

Step3: Put access modifier in front of each information declaration

class Student {
public string name;
public string gender;
public double gpa;
}

Step4: Make
Step5: Use the
a variable
followingdeclaration
statement to hold
create
thememory
locationspace
of allocation
for each student

Student s1 = new Student () ; The name that point to memory location


Is referenced as the name of Object
Student s2 = new Student();
The statement above is called “Object Creation”
Object Oriented Programming: Data Access> Data assignment

Information Data Data


memory
gpa 3.0 gpa 4.0
name Andy Kathy
name Andy name Kathy
gpa 3.0 4.0 gender “male" gender “female"
s1
gender male female s2

Student s1 = new Student();


Student s2 = new Student();

To set data for each student’s information use the following statements

s1 .name = "Andy" ;
s1.gender = "male";
s1. gpa = 3.0;
s2.name = "Kathy“;
s2.gpa = 4.0;
s2.gender = "female";

The dot operator is used to access data from each student’s information
Object Oriented Programming: Data Accessment> Data Retrival

Information Data Data


memory
gpa 3.0 gpa 4.0
name Andy Kathy
name Andy name Kathy
gpa 3.0 4.0 gender “male" gender “female"
s1
gender male female s2

Student s1 = new Student();


Student s2 = new Student();

To get data from each student, use the following statements

s1.name //return Andy

In console application, use the following command to display first student’s name

Console.WriteLine( s1.name ); //display Andy

Console.WriteLine( "Name is" + s1.name); //display Name is Andy


Object Oriented Programming

Lab #0 : Excercise
Entity diagram Data Table
Information Data Data Data
Color
Make Model Camry Ecosport Mazda3

Make Toyota Ford Mazda


Yeare
Color Golden Pink Yellow
Car Model
Mileage 40,000 500 300
kilometers kilometers kilometers
Type Sedan Sedan Sedan
Mileage
Price Price 800,000 888,000 920,000
Type
Year 2012 2014 2014

Consider the Entity diagram and data Table above, and develop the following c#
Statements
-Class structure for Car class
-Information declaration for each car information
-Object creation statements for to support three different objects in the data table
-Create assignment statements to store data for each car.
-Draw memory allocation for each car

S-ar putea să vă placă și