Quotes help make search much faster. Example: "Practice Makes Perfect"

Wednesday, September 4, 2019

Codecademy Static Fields and Properties 2/8


// Sample Solution
///////////////////////////////////////////////////
// Forest.cs code
using System;

namespace StaticMembers
{
  class Forest
  {
    // FIELDS
   
    public int age;
    private string biome;
    private static int forestsCreated;
    public static int ForestsCreated
    {
      get { return forestsCreated; }
      private set { forestsCreated = value; }
    }
   
    // CONSTRUCTORS
   
    public Forest(string name, string biome)
    {
      this.Name = name;
      this.Biome = biome;
      Age = 0;
      ForestsCreated++;
    }
   
    public Forest(string name) : this(name, "Unknown")
    { }
   
    // PROPERTIES
   
    public string Name
    { get; set; }
   
    public int Trees
    { get; set; }
   
    public string Biome
    {
      get { return biome; }
      set
      {
        if (value == "Tropical" ||
            value == "Temperate" ||
            value == "Boreal")
        {
          biome = value;
        }
        else
        {
          biome = "Unknown";
        }
      }
    }
   
    public int Age
    {
      get { return age; }
      private set { age = value; }
    }
   
    // METHODS
   
    public int Grow()
    {
      Trees += 30;
      Age += 1;
      return Trees;
    }
   
    public int Burn()
    {
      Trees -= 20;
      Age += 1;
      return Trees;
    }
   
  }

}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts