Introduction

CONCEPT: Object Oriented Programming (OOP) is a modern way to code. It is a paradigm shift from the traditional, functional programming. The C# language and the .NET framework are built specifically for OOP. For an explantion of OOP go here.

The fundamental concept to grasp is that a class represents a blueprint of a real-world thing. As it is a blueprint, you can use the class to create Objects based on the class. Each ‘Object` is unique, just like values for numbers and strings, but that is where much of the similarity ends.

What is a Class?

As previously mentioned, a class is a blueprint for a real-world thing. A class consists of the following:

  1. Name: each class must have a unique name to separate it from all other classes.
  2. Properties: a property is a separate identifier of a class. Together, all the properties identify what the class is made up of.
  3. Accessors & Mutators:
    • Accessor: this is a method that allows programming code to read the value of a single property
    • Mutator: this is a method that allows programming code to write or change the value of a single property
  4. Class methods: class methods are the means to do something with the class; they rely on the properties, with their associated Accessors and Mutators, to make the class more functional.

Class Diagram

A Class Diagram is a visual representation of the class; it outlines the blueprint to code the class. (You are not required to create a Class Diagram in this course, only to use it to code the class.) A Class Diagram has three areas:

  • ClassName: the name of the class
  • Properties: a list of all the properties including the private member fields and their associated Accessors & Mutators
  • Methods: a list of all the methods including Constructor(s) and class methods

class-diagram

Die.cs

die-class-diagram

public class Die
{
    private Random rnd = new Random(); // used to get a random roll of a Die

    // private member fields
    private int _sides;
    private int _faceValue;

    // public Accessors & Mutators
    public int Sides
    {
        get { return _sides; } // public Accessor
        set
        {
            if (value >= 4)
            {
                _sides = value;
            }
            else
            {
                throw new Exception("Invalid sides for a Die");
            }
        }
    }// end of Sides

    public int FaceValue
    {
        get { return _faceValue; }
        set
        {
            if (value > 0 && value <= _sides) // could also write: if (value > 0 && value <= Sides)
            {
                _faceValue = value;
            }
            else
            {
                throw new Exception("Invalid face value for a Die");
            }
        }
    }// end of FaceValue

    // Constructor(s)
    // Empty Constructor
    public Die()
    {
        Sides = 6;
        FaceValue = 1;
    }

    // Greedy Constructor
    public Die(int sides)
    {
        Sides = sides;
        FaceValue = 1;
    }

    // Class method(s)
    public void Roll()
    {
        FaceValue = rnd.Next(1, Sides + 1);
    }

    public override string ToString()
    {
        //return "Sides = " + Sides + ", FaceValue = " + FaceValue;
        return string.Format("Sides = {0}, FaceValue = {1}", Sides, FaceValue)
    }
}

Program.cs

class Program
{
    static void Main(string[] args)
    {
        Setup();

        //Create and display a default instance of the class
        Die sixSidedDie = new Die();
        Console.WriteLine("Sides = {0}, FaceValue = {1}", sixSidedDie.Sides, sixSidedDie.FaceValue);

        //Display the instance of the class using a class method

        //Create and display a non-default instance of the class
        Die someDie = new Die(20);
        Console.WriteLine("Sides = {0}, FaceValue = {1}", someDie.Sides, someDie.FaceValue);

        //Change the values(s) of the instances of the class
        sixSidedDie.Roll();
        someDie.Roll();
        Console.WriteLine("\nCalling the properties directly");
        Console.WriteLine("Sides = {0}, FaceValue = {1}", sixSidedDie.Sides, sixSidedDie.FaceValue);
        Console.WriteLine("Sides = {0}, FaceValue = {1}", someDie.Sides, someDie.FaceValue);
        Console.WriteLine("\nCalling the .ToString() method");
        Console.WriteLine(sixSidedDie);
        Console.WriteLine(someDie);

        //Exceptions
        Console.WriteLine("\nThe try-catch working");
        try
        {
            //sixSidedDie.FaceValue = 7;
            someDie.Sides = 1;
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex.Message);
            Console.ForegroundColor = ConsoleColor.Black;
        }

        Console.ReadLine();
    }//eom

    #region Provided methods
    static void Setup()
    {
        Console.Title = "Die Class Demo";
        Console.ForegroundColor = ConsoleColor.Black;
        Console.BackgroundColor = ConsoleColor.White;
        Console.Clear();
    }//end of Setup
    #endregion
}//eoc

Coding Topics

TBD…

Practice Problems

Practice Object Problem 1
Practice Object Problem 2
Object Problems 1
Object Problems 2
Understanding Objects Worksheet

CPSC1012 Home