In: Computer Science
Challenge: Dog
Description: Create a Dog class that contains specified properties and methods. Create an instance of Dog and use its methods.
Purpose: This application provides experience with creating classes and instances of objects in C#.
Requirements:
Project Name: Dog
Target Platform: Console
Programming Language: C#
Documentation:
Create a class called Dog.
Dog is to have the following public properties:
Gender is a enum type you need to create that contains Male and Female.
Create a constructor (Links to an external site.) in the class to initialize all of the properties when creating an instance.
Create a method (function in the class) called Bark that takes one parameter that is the number of times to bark and prints “Woof!” that many times to the Console.
Create a method called GetTag that takes no parameters and returns a string. The method returns “If lost, call [owner]. [‘Her’|‘His’] name is [name] and [‘she’|‘he’] is [age] [‘year’|‘years’] old.”
[owner] means replace with the name of the owner.
[‘Her’|‘His’] means choose to display “Her” or “His” based on the gender of the Dog.
[name] means replace with the name of the Dog.
[‘she’|‘he’] means choose to display “she” or “he” based on the gender of the Dog.
[‘year’|‘years’] means to choose to display “year” or “years” depending on the age of the Dog. If age is 1, use “year”. If any other age, use “years”.
In the Main function of the application, test that instances of Dog can be created and that the methods work. In the following, “Orion” is the name of the Dog instance, “Shawn” is the owner, 1 is the age, and Gender.Male is the gender.
Dog puppy = Dog("Orion", "Shawn", 1, Gender.Male); // create object instance puppy.Bark(3); // output: Woof!Woof!Woof! Console.WriteLine(puppy.GetTag()); // output: If lost, call Shawn. His name is Orion and he is 1 year old. Dog myDog = Dog("Lileu", "Dale", 4, Gender.Female); // create object instance myDog.Bark(1); // output: Woof! Console.WriteLine(myDog.GetTag()); // output: If lost, call Dale. Her name is Lileu and she is 4 years old.
Submission
You are to submit a zip file of the project directory for the application you create. The entire project is to be submitted. Find the project folder that holds the Visual Studio project and zip it.
When you finish can you send pictures of the code with spacing
Screenshot
Program
using System;
namespace DogTest
{
//Create an enum Gender
public enum Gender { Male,Female};
//Create a class Dog
class Dog
{
//Instance
variables
private string
name;
private string
owner;
private int age;
private Gender
gender;
//Constructor
public Dog(string
name,string owner,int age,Gender gender)
{
this.name = name;
this.owner = owner;
this.age = age;
this.gender = gender;
}
//Method bark used to
print the bark sound
public void Bark(int
times)
{
for(int i = 0; i < times; i++)
{
Console.Write("Woof!");
}
Console.WriteLine();
}
//Method to get a dog
went missing, means tag generator for dog
public string
GetTag()
{
string tag = "If lost, call "+owner+". ";
if (gender == 0)
{
tag += "His name is " + name + " he is " + age.ToString();
}
else
{
tag += "Her name is " + name + " she is " + age.ToString();
}
if (age > 1)
{
tag += " years old.";
}
else
{
tag += " year old.";
}
return tag;
}
}
class Program
{
static void
Main(string[] args)
{
//Class Dog Test
Dog puppy = new Dog("Orion", "Shawn", 1, Gender.Male); // create
object instance
puppy.Bark(3); // output: Woof!Woof!Woof!
Console.WriteLine(puppy.GetTag()); // output: If lost, call Shawn.
His name is Orion and he is 1 year old.
Dog myDog =new Dog("Lileu", "Dale", 4, Gender.Female); // create
object instance
myDog.Bark(1); // output: Woof!
Console.WriteLine(myDog.GetTag()); // output: If lost, call Dale.
Her name is Lileu and she is 4 years old.
}
}
}
----------------------------------------------------------------------------------------------------------
Output
Woof!Woof!Woof!
If lost, call Shawn. His name is Orion he is 1 year old.
Woof!
If lost, call Dale. Her name is Lileu she is 4 years old.