In: Computer Science
In C# please
Exercise #4:
Create a two (2) instances of the “Dog” class in your “Main” method. One will be created using the default constructor and the other should be made using the overloaded constructor. That means that you should pass the overloaded constructor arguments that come from user input.
Print out each of the dog’s three attributes using the accessors. Then call the “UpdateBark” method for both and print out their fields again, but this time through the “ToString” method.
Example (blue is default dog’s attributes, green is the user’s dog’s):
Creating a default dog…
Finished creating a default dog!
Default dog bark sound is Yelp!
Default dog size is 20 inches.
Default dog’s cuteness is cute dog.
Continued on third page…
Please enter the sound of your dog’s bark: “MOOOOOO!”
Please enter the size of your dog: “10”
Please enter the cuteness of your dog: “ugly dog.”
Your dog bark sound is MOOOOOO!
Your dog size is 10 inches.
Your dog’s cuteness is ugly dog.
Both dogs are doing a scary bark! Their cuteness has been affected!
Default dog bark sound is Yelp!
Default dog size is 20 inches.
Default dog’s cuteness is average.
Your dog bark sound is MOOOOOO!
Your dog size is 10 inches.
Your dog’s cuteness is average.
using System;
class Dog
{
private string barkSound;
private int size;
private string cuteness;
public Dog()
{
Console.WriteLine("Creating a
default dog…");
barkSound = "Yelp!";
size = 20;
cuteness = "cute";
Console.WriteLine("Finished
creating a default dog!");
}
public Dog(string barkSound,int size,string
cuteness)
{
this.barkSound = barkSound;
this.size = size;
this.cuteness = cuteness;
}
public string getBarkSound()
{
return barkSound;
}
public int getSize()
{
return size;
}
public string getCuteness()
{
return cuteness;
}
public void updateBark()
{
cuteness = "average";
}
public override string ToString()
{
return "dog bark sound is
"+getBarkSound()+
"\ndog size is "+getSize()+"
inches."+
"\ndog’s cuteness is
"+getCuteness()+".";
}
}
public class Test
{
public static void Main()
{
Dog d1 = new Dog();
Console.WriteLine("Default dog bark
sound is "+d1.getBarkSound());
Console.WriteLine("Default dog size
is "+d1.getSize()+" inches.");
Console.WriteLine("Default dog’s
cuteness is "+d1.getCuteness()+" dog.");
Console.WriteLine("Please enter
the sound of your dog’s bark: ");
string sound =
Console.ReadLine();
Console.WriteLine("Please enter
the size of your dog: ");
int size =
Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter
the cuteness of your dog: ");
string cuteness =
Console.ReadLine();
Dog d2 = new
Dog(sound,size,cuteness);
Console.WriteLine("Your dog bark
sound is "+d2.getBarkSound());
Console.WriteLine("Your dog size is
"+d2.getSize()+" inches.");
Console.WriteLine("Your dog’s
cuteness is "+d2.getCuteness()+" dog.");
d1.updateBark();
d2.updateBark();
Console.WriteLine(d1.ToString());
Console.WriteLine(d2.ToString());
}
}
Output:
Creating a default dog… Finished creating a default dog! Default dog bark sound is Yelp! Default dog size is 20 inches. Default dog’s cuteness is cute dog. Please enter the sound of your dog’s bark: MOOOOOO! Please enter the size of your dog: 10 Please enter the cuteness of your dog: ugly Your dog bark sound is MOOOOOO! Your dog size is 10 inches. Your dog’s cuteness is ugly dog. dog bark sound is Yelp! dog size is 20 inches. dog’s cuteness is average. dog bark sound is MOOOOOO! dog size is 10 inches. dog’s cuteness is average.
Do ask if any doubt. Please up-vote.