Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In Forest.cs
- using System;
- namespace BasicClasses
- {
- class Forest
- {
- //Fields
- public int age;
- private string biome;
- //Constructor
- public Forest(string name, string biome){
- Name = name; //set the Name property
- Biome = biome; //set the Biome property
- Age = 0;
- }
- //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;
- }
- }
- }
- ============================================================================================================================
- //In Program.cs file
- using System;
- namespace BasicClasses
- {
- class Program
- {
- static void Main(string[] args)
- {
- Forest f = new Forest("Congo", "Desert" ); //call the constructor by writing variable values as the arguments
- f.Trees = 0;
- /* Hence we can delete
- f.Name = "Congo";
- f.Biome = "Desert";
- as the lines are not needed
- */
- Console.WriteLine(f.Name);
- Console.WriteLine(f.Biome);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement