CS 2120 CC Programming Concepts Questions

Programming Concepts IIThis is primarily an exercise in practicing “programming to the interface”, in the sense that the very simple UI is
structured so that all it knows is that “things” in the ListBox are animals so it “talks to them” only through the
IAnimal interface. The high level steps of what you need to do here are:


Define some classes and an interface:
○ Define an interface for Animals with at least:
■ A Name property
■ And a method called Speak that does not need any arguments and has void return type.
○ Define two classes, namely Dog and Cat, that implement that interface. Something to consider would an abstract base class ever be of use here? Either way, these 2 classes should:
■ Implement the Animal interface in such a way that when the Speak method is called on
objects of those classes an appropriate sound (i.e. .wav) file is played.
■ Override the ToString method (from base class Object) so that each instance returns the
animal’s name followed by a space and then the unicode “emoji” character representing
their species, namely the dog emoji and the cat emoji, e.g. as shown in the screenshot
below.
Complete the Win Forms UI:
○ Add a ListBox to the form to store/hold “animals”.
○ Make use of the Dog and Cat classes by creating and adding 3 objects to the ListBox on the
Form, for example at least 2 dogs and a cat.
○ Add a “Speak!” button to the form and implement a click event handler that gets the currently
selected animal from the ListBox, downcasts it to your Animal interface, and “asks it to speak”
by calling the Speak method on it.

Not mentioned on the sample output but put a Label on the top with your name with today’s
date and make it prominent by making bold text with a bigger size. [failure to do this will cost
you marks]
The following is a screenshot that shows 2 dog objects and 1 cat object added to the listbox on the left, with 3rd
(namely “Rex” the dog) highlighted, so that when the “Speak!” button is clicked, the barking dog audio file is
played.
And, of course, if “Garfield” the cat is selected then a meowing cat sound should play when that same button is
clicked. This is a simple example of “programming to the interface” in action. Why? Because the Form object
(inside the “Speak!” button Click event handler) is speaking (pun intended) to the objects in the listbox through
their “Animal interface” and asking them to “Speak”. Said differently, the Form object doesn’t care what kind of
animal objects are in the ListBox, only that they are animals, and so treats them as generic animals and asks
them to “speak”.
How to proceed?
There is a zip file of starting code in the in-class assignment #4 folder in eConestoga. Here are a few things to
note this starting point of code:


It consists of a Win Forms project, which is mostly an empty shell into which you must add your code.
The project has a folder called “SoundFiles” that contains 2 wav files, 1 of a dog barking and the other of
a cat meowing.
○ Note they are configured to “Copy to Output Directory” as a Build Action. You can see this by
selecting them and viewing the setting in the Properties window.
○ This means that, when the project is built, they are copied to the output directory (i.e. where
the exe file for the app resides), which in turn means they should be easier to find when needed
in code
.
The actual amount of code you need to write for this is very, very small – the challenge is more in getting your
head wrapped around the brand new OOP concepts such as pure interfaces, abstract classes, polymorphism,
and inheritance. Here are some that will help guide you:

In terms of guiding examples:
○ IColourable ”colour control” example covered in class for working with pure interfaces and for
how the idea of “programming to the interface” gets applied.
○ The Employee + SalesEmployee example that illustrates basic inheritance and polymorphism,
overriding ToString, and adding and retrieving objects to/from a ListBox.
○ The Alarm Clock solution you did for in class assignment #2 that used a SoundPlayer to play wav
files. Again, copying the wav files to the output directory should make them easier to find for
the sound player. You might recall from that assignment that it was shown that the following
code can be used to play a sound file:
■ System.Media.SoundPlayer player = new System.Media.SoundPlayer();
■ player.SoundLocation = @”c:\windows\media\Alarm01.wav”;
■ player.Play();

There are a few different ways to get an emoji into a C# string but the basic approach I used is this:
○ Figure out the unicode value for the emoji and declare an int with that value, using hex.
■ For example, the unicode value (in hex) of the Cow emoji is 1F42E. Thus..
■ int unicodeCowValue = 0x1F42E;
○ Then call the static ConvertFromUtf32 method on type char to convert that to a string. Thus
something like this would insert that emoji into a string..
■ string cowName = $”Bessy {char.ConvertFromUtf32(unicodeCowValue )}”;
Grading:
Grading of these in-class assignments is a simple 0, 1, 2, or 3 and is and based on the following scale:
Grade
Description
3
The solution builds and runs without errors and all 3 of the following criteria
are met:
● The interface is defined, the 2 animal classes are defined and
implement the interface
● Objects of the classes get instantiated and added to the list with the
correct string showing
● The correct sound plays when selected and the “speak!” button is
clicked.
2
The solution builds and runs with some functionality present but at least 1 of
the above criteria has not been met.
1
A poor or minimal solution was submitted. For instance, it might build and run
but almost none of the functionality is present, or there might be some code
provided but it either fails to build or runs with errors.
0
No solution submitted or no code attempted in the solution.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed with your coursework?
Get quality coursework help from an expert!