Question

IN C# Objectives: Create an application that uses a dictionary collection to store information ...

IN C#

Objectives:

  • Create an application that uses a dictionary collection to store information about an object.

  • Understanding of abstract classes and how to use them

  • Utilize override with an abstract class

  • Understanding of Interfaces and how to use them

  • Implement an Interface to create a “contract” between classes.

  • Compare and contrast inheritance and interfaces.

Instructions:

Interface:

Create an interface called ITrainable which contains the following:

Dictionary<string, string> Behaviors{ get; set; }
string Perform(String signal);
string Train(String signal, string behavior);

Here is an explanation of each of the signatures above; (Note: The implementations are described below but should be in the class that implements the interface, not the interface itself):

Dictionary<string, string> Behaviors{ get; set; }

A dictionary property named Behaviors to be implemented in classes that implement ITrainable

o The key (a string) is the signal the trainer/user will use to ask the animal to perform a behavior.

o The value (a string) is the behavior the animal will perform when it sees the signal.

string Perform(String signal);

A method named Perform to be implemented in classes that implement ITrainable.

o Needs a string parameter
o Fetch the behavior from the Behaviors dictionary based on the signal.
o Should return a string describing how the animal performed the behavior in response to the signal.

string Train(String signal, string behavior);

A method named Train to be implemented in classes that implement ITrainable.

o Needs a string parameter that holds the signal sent by the user.
o Will add the behavior to the Behaviors dictionary using the signal as the key
o Should return a string describing how the animal has been trained and will respond to the specified signal.

Base Abstract Class:

Create a base abstract class named Animal which contains the following properties and fields:

  • A property named Species. This will hold the animal’s name (i.e. dolphin)

  • A field named _foodConsumed to keep track of how much food the animal has

    consumed.

  • A field named _treat used to store the name of the food this animal likes to eat

    as a treat. (like fish for dolphins, rats for snakes). This will need to be set to protected access since it will be used in subclasses to set the value unique to each animal.

The following methods:

• An Eat method

o Tracks how much food has been consumed.
o If the animal eats over 4 times it should trigger the Poop() method and

reset the _foodConsumed field.
o It should return a string describing how the animal ate the food that looks

like this: "The dolphin ate a herring”

• A MakeNoise method

o This will be overridden later and should be virtual.
o This should return a string. This will be overriding later but should return a

default string. (If your code is correctly created you should not see the default in your working application.)

• A Poop method

o This should not return anything.
o This should write out to the console that the animal has pooped. For ex:

"The dolphin pooped!”

Animal Classes:

You will need to implement at least 6 animal classes all should inherit from the Animal base class.

  • The constructors will need to initialize the Species and Trainable properties as well as the _treat field.

  • An overriding method named MakeNoise that returns a string that describes the animal making noise. Ex: “A gargled roar comes from the alligator.”

Some animals are trainable (dogs, dolphins, even cats) and some aren’t (snakes, fish etc) and your application should reflect that. At least half of the animals should implement the ITrainable interface. This will mean they will be required to implement the properties and methods as described above in the Interface section.

  • The Perform method should return a string similar to: "After you signaled clapping hands the otter will now perform the 'jump through the hoop' behavior."

  • The Train method should collect the signal and behavior strings and input them into the Behavior dictionary and return a string such as: "The dolphin learned to slap it’s tail when you point to the sky."

  • Contain a Behaviors property that will hold the signals and behaviors the given animal can perform based on what the user enters.

Zookeeper Application:

Your main program application class should have the following:

  • A List object which will contain all the animals you create.

  • A method which welcomes the user and adds instances of each animal into the List object.

Your application should list the animals you’ve initialized, specify which are trainable, and give the user the option of selecting one.

Upon selecting an animal it will announce which animal was selected and then provide a menu for activities the user can do with the selected animal:

Training:

If the user selects Training, the program should ask the user what behavior they would like to teach the animal and what signal they will associate with the behavior. The program then stores this information and reprints the menu:

Feeding the Animal:

For feeding an animal a treat (this is for all animals, not just the trainable ones) it produces the following result, printing the string produced by the Eat() method in the Animal base class:

Get the animal to perform a behavior:

When signaling the animal, if the animal is trainable, then the program should ask the user for the relevant signal. If the animal is not trainable a message should show to the user: “The animal you selected is not a trainable animal. Please select a different activity or exit to select a different animal.”

If the signal is in the dictionary it should respond. (If it is not a message should be shown “The dolphin does not know this signal. Train it to recognize this signal and associate it with a behavior.” )

Make noise:

When this option is selected the animal should make a noise: Or the user can select a different animal.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
class ZooKeeper
{
static List<Animal> animals = new List<Animal>();

static void Main(string[] args)
{
Welcome();
//ListAnimals();

do
{
ListAnimals();
int animaloption = SelectAnimal();
AnnounceAnimal(animaloption);
int itemselect;
do
{
itemselect = ActivityMenu(animaloption);
switch (itemselect)
{
case 1:
{
string behaviour = GetBehaviour(animaloption, itemselect);
string signal = GetSignal(animaloption, behaviour);
ITrainable itrain = (ITrainable)animals[animaloption - 1];
itrain.Behaviors[signal] = behaviour;
break;
}
case 2:
{
((Animal)animals[animaloption - 1]).Eat();
break;
}
case 3:
{
if (animals[animaloption - 1] is ITrainable)
{
string signal = ReadSignal(animaloption);
if(((ITrainable)animals[animaloption - 1]).Behaviors.ContainsKey(signal))
{

}
else
{
Console.WriteLine("“The {0} does not know this signal. Train it to recognize this signal and associate it with a behavior.”", animals[animaloption-1].Species);
}
}
else
{
Console.WriteLine("The animal you selected is not a trainable animal. Please select a different activity or exit to select a different animal.");
}
break;
}
case 4:
{
animals[animaloption - 1].MakeNoise();
break;
}
case 5:
break;
}
} while (itemselect != 5);
} while (true);

}

private static string ReadSignal(int animaloption)
{
Console.WriteLine("Read Signal");
string signal = Console.ReadLine();
return signal;
}

private static string GetSignal(int animaloption, string behaviour)
{
Console.WriteLine("Enter Signal");
return Console.ReadLine();
}

private static string GetBehaviour(int animaloption, int itemselect)
{
Console.WriteLine("Enter Behaviour");
return Console.ReadLine();
}

private static int ActivityMenu(int option)
{
Console.WriteLine("1. Training");
Console.WriteLine("2. Feeding");
//if (animals[option - 1] is ITrainable)
//{
Console.WriteLine("3. Performing");
//}
Console.WriteLine("4. Make Noise");
Console.WriteLine("5. Exit");
int select;
bool success;
do
{
success = int.TryParse(Console.ReadLine(), out select);
} while (success == false && (select >= 1 && select <= 5));
return select;
}

private static void AnnounceAnimal(int option)
{
Console.WriteLine("Animnal {0} is selected", animals[option-1].Species);
}

private static int SelectAnimal()
{
Console.WriteLine("Select Option");
int option;
bool success;
do {
success = int.TryParse(Console.ReadLine(), out option);
} while (success == false && (option <= animals.Count));
return option;
}

private static void ListAnimals()
{
//foreach (var item in animals)
//{
// Console.WriteLine("{0} ")
//}

for (int i = 0; i < animals.Count; i++)
{
Console.WriteLine("{0} {1} {2}", i+1, animals[i].Species, animals[i] is ITrainable?"Trainable":"Not Trainable");

}
}

public static void Welcome()
{
Console.WriteLine("***********Welcome*************");
Cat c = new Cat("Cat", "Milk");
  
animals.Add(c);
}
}

public interface ITrainable
{
// A dictionary property named Behaviors to be implemented in classes that implement ITrainable
// The key (a string) is the signal the trainer/user will use to ask the animal to perform a behavior.
// The value (a string) is the behavior the animal will perform when it sees the signal.
Dictionary<string, string> Behaviors { get; set; }
  
//
string Perform(String signal);
string Train(String signal, string behavior);
}

public abstract class Animal
{
public string Species { get; set; }
int _foodConsumed;
protected string _treat;

public string Eat()
{
_foodConsumed++;
if (_foodConsumed > 4)
{
Poop();
_foodConsumed = 0;
}
return "Eat complete";
}

void Poop()
{
Console.WriteLine("The " + this.Species + " pooped.");
}

public virtual string MakeNoise()
{
return "";
}

}

public class Cat : Animal, ITrainable
{
public Dictionary<string, string> Behaviors { get; set; }

// n
public string Perform(String signal)
{
return Behaviors[signal];
}
public string Train(String signal, string behavior)
{
Behaviors[signal] = behavior;
return behavior;
}

public Cat(string Species, string treat)
{
this.Species = Species;
this._treat = treat;
this.Behaviors = new Dictionary<string, string>();
}

public new string Eat()
{
base.Eat();
return "Cat eats Meat";
}

public override string MakeNoise()
{
return "Mew";
}
}

//public class Dog : Animal, ITrainable
//{

//}

//public class Fish : Animal
//{

//}

//public class Dolphin : Animal, ITrainable
//{

//}

//public class Snakes : Animal
//{

//}

//public class Alligator : Animal
//{

//}
}

Add a comment
Know the answer?
Add Answer to:
IN C# Objectives: Create an application that uses a dictionary collection to store information ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • C# Programming : Create a housing application for a property manager. Include a base class named...

    C# Programming : Create a housing application for a property manager. Include a base class named Housing. Include data characteristics such as address and year built. Include a virtual method that returns the total projected rental amount. Define an interface named IUnits that has a method that returns the number of units. The MultiUnit class should implement this interface. Create subclasses for MultiUnit and SingleFamily. SingleFamily should include characteristics such as size in square feet and availability of garage. MultiUnit...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • You must write C# classes which are used to manage product review data for Books and...

    You must write C# classes which are used to manage product review data for Books and Cars. You will write an abstract class called Review which abstracts common properties (review rating and comments). You will then create an Interface responsible for the functionality to display the Reviewcontent. Then, you will create 2 concrete classes which inherit from Review and implement the Interface. Finally, you will create console applicationwhich will create 2 CarReview instances and 2 BookReview instances and display them...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information...

    ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information that may appear on a mailing label. Design and implement each of the described classes below. public interface AddrLabelInterface { String getAttnName(); String getTitle(); // Mr., Mrs., etc. String getName(); String getNameSuffix(); // e.g., Jr., III String getProfessionalSuffix(); // O.D. (doctor of optometry) String getStreet(); String getSuiteNum(); String getCity(); String getState(); String getZipCode(); } Step 1 Create an abstract AddrLabel class that implements the...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT