Session 02 and 03

oatmealbrothersSoftware and s/w Development

Nov 18, 2013 (3 years and 7 months ago)

102 views

Session 0
2

and 03


For more than a decade object
-
oriented programming has been the dominating programming paradigm. So
in this course we will study object
-
oriented programming. This session is the first introduction OOP.

Intended learning outcome

(What
you should be able to do after the session)



Can explain the concepts of objects and classes.



Can write simple classes in C# (attributes, constructors, methods and properties).



Can draw and explain simple UML class diagrams.



Can write simple programmes
where two or more objects interact.



And, from last session:

o

Can write, compile and execute simple C# programs.

o

Can use native data types and arrays in C#.

o

Can write simple programs using conditional statements and loops in C#.

Exercises

Below you will find

a set of programming exercises, which will train you in objects cooperating with objects
of other classes. In these exercises, there is only one object of each class, and the important thing is to get
them to cooperate.

In general when you have made one
class, compile it and test out if it works as you want it to, before you
continue.

A forest owner wants to register information about his forest.


The Forest class:

1.

Create a new class called Forest. The class Forest shall have the following instants varia
bles
(fields, attributes): name, location, squareMetre, and price.


You have to decide the data types of the instants variables. Name is the name of the forest.
Location is the part of the country where the forest is located. Square metre is the size of the
forest. Price is the price per square metre.

2.

Write a constructor
that initialises the name, location, squareMetre, price given from input
parameters.


3.

Add a property to the class Forest that makes it possible to give the price a new value.



4.

Add properties for the other attributes


consider if some should be read
-
only

(get).


5.

Add a ToString method that returns information about a Forest object in a printable format.


6.

Add a method that can return the value of the forest. The value is calculated by multiplying the
price by squareMetre. Use the following method heading:



public double CalculateValue()



The Owner class:

7.

Create a new class called Owner. The class Owner shall have the following attributes: ownerName,
address, zip
-
code, and town.


You have to decide the data types of the instants variables. OwnerName is th
e name of the owner;
address is the address on the owner.

8.

Write a constructor that initialises the ownerName and address.


9.

Write suitable properties for each of the fields.


10.

Write a ToString method that returns information about the owner in a printable fo
rmat.


The Owner class:

11.


Add a new field to the Owner class; thi
s field shall be of the type Fo
rest. Name it forest.



12.

Add a property (get and set) for the new attribute (forest) to the Owner class.


13.

Change the ToString

method in the class Owner, so that it also returns information about the
forest.


The Tree Class:

14.

Create a new class Tree, which is to hold information about the kinds of trees in a forest. Attributes
should be: treeSpecies, lifeTime, costPerSquareMeter,
earningsPerSquareMeter.


15.

And a constructor and properties to the class.


16.

Add a ToString method to the class.



The Forest class:

17.

Add an attribute of type Tree to class Forest.


18.

Modify relevant parts of class Forest and Owner according to the new design.


19.

A
dd a method to Forest that returns the net earnings of the forest.


And remember to test it all.

20.

Draw an UML class diagram for your solution.