C# Basics: Activity 7 - Classes (part 2)

CSharp · Console

/Program.cs

using System;


					
public class Program
{
	public static void Main()
	{
		Bird Patrick = new Bird("duck", "Patrick", "swimming", 12, true);
	}
}

public class Bird {
  // your code goes here
  private string name;
  private string species;
  private string hobby;
  private int age;
  private bool loveMusic;
  
  public Bird(string speciesInput, string nameInput, string hobbyInput, int ageInput, bool loveMusicInput) {
    species = speciesInput;
    name = nameInput;
    hobby = hobbyInput;
    age = ageInput;
    loveMusic = loveMusicInput;    
  }

  public string getName() {return name;}
  public string getSpecies() {return species;}
  public string getHobby() {return hobby;}
  public int getAge() {return age;}
  public bool getLoveMusic() {return loveMusic;}
	
	public string ToString(){
    // initialize variable info to have empty String
    string info = "";

    // append information to the String in the format: 
    // Hi, my name is [name]. I am a [age] year old [species] who likes to [hobby].
    info = info + "Hi, my name is " + getName() + 
           ". I am a " + getAge() + " year old " + getSpecies() + 
           " who likes to " + getHobby() + ". ";

    // if the Bird likes music, append the String "I also like music very much!" to info
    if(getLoveMusic()){
        info = info + "I also like music very much!";
    }else{        
        info = info + "However, I do not like music!";
    }

    // return the full string
    return info;
}
}
;