c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.228 seguidores
Pré-visualização50 páginas
state that is the object\u2019s current value
\u2022	 a behavior that tells what you can do with the object
\u2022	 a life cycle from where the object are created to it again are deleted
An object is created from a class, and assigned at that time a reference that identifies the object. The 
class\u2019s instance variables determine which variables to be created for the object and the value of these 
variables is state of the object. The class\u2019s methods define what can be done with the object and hence 
the object\u2019s behavior. The last point of concerning is object\u2019s life cycle linked to the concept of scope, as 
explained later.
Exam23
Dice
The task is to write a program that simulates that you throw 5 cubes until they are all alike. Finally the 
program will print out how many times the cubes is thrown \u2013 an example which, incidentally, I\u2019ve seen 
in the past (Exam20), but by that time I had no concept of class available.
How to
The first task is to find (decide) the program\u2019s classes, and basically we need a class that can represent 
a die (a cube), where a die is characterized by a number of eyes, and it should be possible to throw the 
die and get knowing what it shows.
Another obvious class is a cup with 5 dice, and here it must be possible to toss the cup and get to know 
what the dice show.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
83 
Design of classes
Finally, I will write a class that can represent the actual game. That is, a class which has a cup and a 
method which carries out the game.
This corresponds to the following design:
Dice
eyes: int
Eyes: int
Throw()
Cup
Coint: int
Dice[]
Toss()
Game
Yatzy()
Play()
Program
Main()
1 1
5
Note that the design is somehow arbitrarily in the sense that I have not followed certain guidelines, but 
the choice of classes is made by the developer, and there could be other solutions that could be just as 
good. Design is largely an activity, based on experience and by watching and relate to what others have 
done, and it is seldom possible to decide exactly what is the best design but a design can be more or 
less appropriate.
It should be added that there are methods and techniques that can help developers to find a program\u2019s 
classes. These are just issues which fall outside the scope of this book, but for larger projects are that 
kind of systems development methodologies not only useful but also necessary.
If I were to attempt a justification of the above design, then the task is to write a program that can simulate 
a simple dice game. The term I first catch sight of, is a die, as something of what all the fuss is about. 
Therefore a class Dice, which may represent a die. In the game, you could throw 5 dice and inspired by 
the way, you for example plays Yatzy, it is natural to think of a cup, which you can toss with. Therefore 
I have a class Cup. Finally the game rules and logic to play must be somewhere and it is not properties 
of a cup, because in the real world one does not produce cups for certain games \u2013 a cup is general and 
can be applied to many kinds of games. Then the game itself is represented by a class Game.
With this design in place, it is relatively simple to write each class. The class of a die can be written as 
follows:
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
84 
Design of classes
public class Dice
{
private static Random rand = new Random();
private int eyes;
public Dice()
{
Throw();
}
public void Throw()
{
eyes = rand.Next(1, 7);
}
public int Eyes
{
get { return eyes; }
}
public override string ToString()
{
return "" + eyes;
}
}
careers.total.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
85 
Design of classes
Then there is the class for a cup that can best be characterized as an encapsulation of an array with Dice 
objects:
public class Cup
{
private Dice[] dice;
public Cup(int n)
{
dice = new Dice[n];
for (int i = 0; i < n; ++i) dice[i] = new Dice();
}
public int Count
{
get { return dice.Length; }
}
public Dice this[int i]
{
get { return dice[i]; }
}
public virtual void Toss()
{
for (int i = 0; i < dice.Length; ++i) dice[i].Throw();
}
public override string ToString()
{
string text = &quot;&quot;;
for (int i = 0; i < dice.Length; ++i)
{
if (i > 0) text += &quot; &quot;;
text += dice[i].Eyes;
}
return text;
}
}
The class to the game itself is very simple and is basically a question that the code from Exam20 has 
moved into its own class:
public class Game
{
private Cup cup;
public Game(int n)
{
cup = new Cup(n);
}
public void Play()
{
int count = 0;
do
{
cup.Toss();
++count;
Console.WriteLine(cup);
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
86 
Design of classes
while (!Yatzy());
Console.WriteLine(&quot;You've got yatzy after {0} attempts&quot;, count);
}
private bool Yatzy()
{
for (int i = 1; i < cup.Count; ++i)
if (cup[i].Eyes != cup[0].Eyes) return false;
return true;
}
}
Finally, there is the Main() method:
class Program
{
static void Main(string[] args)
{
Game game = new Game(5);
game.Play();
}
}
Explanation
If you compare the class Dice with the class Coin you will note that in principle they are similar and 
there is not much to explain except that the data representation this time is an int.
The class Cup represents a dice cup, which is implemented as a class with a container for Dice objects. 
The container is an array:
private Dice []dice;
but to make the cup more flexible and allow it to be used in other contexts (where it does not necessarily 
have to contain 5 cubes) the number of cubes is a parameter to the constructor. Dice is a reference type, 
and when you create an array of reference types, you get an array that contains references, but not objects 
of that type. They must be created explicit:
for (int i = 0; i < n; ++i) dice[i] = new Dice();
Since the class has a constructor with a parameter, it has no default constructor. It means that you can\u2019t 
create a cup in the following way:
Cup cup = new Cup(); // illegal
The class has a read only property that returns the number of cubes in the cup. It is necessary for users 
of the class. Otherwise they can\u2019t get to know how many cubes the cup contains. 
In addition, the class has a read only property that returns the cube that has index i:
public Dice this[int i]
{
get { return dice[i]; }
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
87 
Design of classes
This is a somewhat special syntax that uses this as a property name, and I will not go into detail here, 
but it means that in a program you can write the following:
Cup cup = new Cup(5);
Dice d = cup[1];
and refer to the individual cubes via an index \u2013 that is as if a cup was an array. The user can then use the 
class as if it were an array, but the user can\u2019t see (or need not have knowledge about) whether the internal 
representation in the class is an array or something else. In this case, these are a read only property. The 
fact that it is read only, means that a user for example can\u2019t write:
cup[1] = new Dice(); // ulovligt
The user can not put another cube in the cup, but since the operator returns a reference the user can do 
anything with the cubes in the cup, for example
Dice d = cup[1];
d.Throw();
The method Toss() is simple and consists merely of a loop that runs over all cubes and throwing them.
With the class Cup is the class Game simple and it has a constructor with a parameter, and based on 
that parameter it creates a