Lecture overview -- Keyboard shortcut: 'u'  Previous page: Classes: An Initial Example [Section] -- Keyboard shortcut: 'p'  Next page: Clients and Servers -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 2 : 29
Object-oriented Programming in C#
Classes and Objects
The Die Class

We start with an example: The class Die. This example illustrates many basic ideas behind classes and objects.

To be concrete we start by looking at a complete class written in C#

/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0-0/die.csThe class Die. This program is explained

The class Die encapsulates a couple of variables (numberOfEyes and randomNumberSupplier). It also implements a number of methods that allow us to use a die. The most important operation is Toss.

/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0-0/dieApp.csA program that tosses three dice.

A program that constructs and tosses a number of dies.

/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0-0/outputSample program output.

What is printed when we run the program shown above.

/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0-0-namespace/die.csThe class Die in the namespace Game.


/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0-0-namespace/dieApp.csA client of Die from namespace Game.


/user/normark/oop-csharp-1/sources/c-sharp/Die-variants/die-0/die.csA slightly more general Die class. This program is explained

This version of class Die is not limited to six sides. It can have an arbitrary number of sides.

 

Go to exerciseYahtzee
The Die class is a template or blueprint from which we can create an arbitrary number of die objects

Read more about class Die in the text book version of this material.