






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























Did you like this?
Data (day, month, year) Methods Day of the week Month name …
State
number of sides roll count
Methods
Dice(int sides) // constructor – constructs a die with given number of sides int Roll() // return the random roll int NumSides() // how many sides int NumRolls() // # of times this die rolled
Console.WriteLine("Rolling {0} sided die.", cube.NumSides()); Console.WriteLine(cube.Roll()); Console.WriteLine(cube.Roll()); Console.WriteLine("Rolled {0} times.", cube.NumRolls());
methods
Dice cube = new Dice(6); // construct six-sided die
Dice dodeca = new Dice(12);// construct twelve-sided die
constructor
static void Main(string[] args) { Dice cube = new Dice(6); Dice dodeca = new Dice(12);
Console.WriteLine(cube.Roll());
Objects constructed
0
myRollCount mySides 6
cube
0
myRollCount mySides 12
dodeca
Method invoked
1
myRollCount mySides 6
cube
Methods and Constructors as seen by programmer Programmer can use the methods and properties defined in the public section only
Mostly the data part of the class Necessary for internal implementation of class Not accessible by programmer
class Dice
{
private int myRollCount; private int mySides;
}
Dice cube = new Dice(6); Console.WriteLine("Number of sides: {0}", cube.mySides);
Console.WriteLine("Number of sides: {0}",
cube.NumSides());