This assignment exercises interfaces and generics. Be sure and read the assignment submission instructions provided previously. It gives you the content and format of what I expect you to post.
INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS
Assignment 8
This assignment exercises interfaces and generics. Be sure and read the assignment submission instructions provided previously. It gives you the content and format of what I expect you to post.
What I will look for in the submission
I will look for several things in your assignment submission:
You have done all of the above steps in each problem. 2. Your programs contain documenting statements describing what is going on. I am not a stickler, but I don’t want to have to presume what your code is doing. More is better than less, so long as the documentation is not a simple restatement of the code 3. Code indentation. Once again, I am not a stickler on this. Just make sure that code
blocks contain statements that are indented. 4. All types and variables you use in this problem MUST be declared in Camel and Pascal
casing. 5. Just as in previous weeks, you must submit the code and the output from running the
code. If the code has indicators of where things like commenting and code need to be added, delete the indicators. I will deduct if you leave them in.
You will find that I don’t give you a line-by-line statement of how your program should work. The reason for this is because different people have different coding styles and approaches and I want to respect your independence unless it prevents you from accomplishing all the assignment steps.
Don't forget to re-read the assignment submission instructions for documentation details!
Don't forget that you have to remove all comment statements like "", as a deduction will occur if you leave them in.
This Week!
Homework is worth 30 points this week.
Problem 1 – 10 points Problem 2 – 20 points Problem 3 – 10 points
You must do either Problems 1 and 3 OR Problems 2 and 3. That means that if you choose Problems 1 and 3, you will achieve only 20 of the 30 points, while if you choose to do Problems 2 and 3, you can achieve all 30 points.
Bottom line, there is NO extra credit in play. You just get to decide how much work you want to do, with the understanding that choosing less work means fewer points.
Be sure and declare the interface(s) in Problem 1 or 2 "public". Your book mentions interface access and shows an example on page 395 in Chapter 16.
Without a public modifier, the interface will default to private and you will find the interface is not accessible when you try to make a reference to the interface from outside the classes that implement the interface.
To bring in a List for Problems 1 or 2, you will need the following at the top of your code:
using System; using System.Collections.Generic;
The 2nd "using" brings in the understanding of a List<> for you to use.
To complete Assignment 8:
Follow the exercise instructions to download the necessary file. 2. Create your solutions and run them 3. Construct a solution file with the content and format provided in the Assignment
Submission document
To submit your solution:
Click on the "Assignment 8" link. 2. Scroll down to the "Attach File" section and choose "Browse My Computer" 3. Locate your solution file 4. Double Click on the file and you will see your attachment 5. Important: Be sure to scroll to the bottom of the page and click "Submit"
Due Date: See Syllabus
Problem 1
This problem is designed to make sure you can write derived classes that implement different interfaces, make instances, detect which objects have implemented the interfaces and use the interfaces to access the class contents and print them. It makes sure you know:
how to use a List (mandatory in this problem)
how to use positional and named parameter passage
how to iterate through a List using a foreach
how to write a private property that has a "get" and a "set" that automatically generate the backing field instead of declaring the fields yourself
how to declare an abstract base class that has an abstract method implemented by the derived classes
how to inherit two classes from the base class, implement the base class abstract method and implement one or more interfaces
The Solution Requirements
To do this problem, you will need to write two interfaces and three classes.
The interfaces you need to write are called IShow2 and IShow3.
IShow2 has a single method with the following signature:
void Show2(string InfoMsg);
IShow3 has a single method with the following signature:
void Show3(string InfoMsg);
The classes you need to write are called DataHoldingObjects, TwoVals and ThreeVals.
DataHoldingObjects is an abstract class that is the base class used for both the TwoVals and ThreeVals derived classes. It holds no data, but it has a single public abstract method called ShowAll that each derived class must implement. See the information on TwoVals and ThreeVals to see what ShowAll does.
TwoVals derives from DataHoldingObjects, implements the IShow2 interface and has:
Two private properties, named FirstInt and FirstFloat that have automatically
generated backing fields. The properties have both a get and set. These properties must be used by both the class constructor and the Show2 method described below. 2. A constructor that receives the two values and saves them using the properties. 3. A Show2() public method that implements the interface IShow2. The Show2 method
must print out the information string provided as an input and then the class data using FirstInt and FirstFloat. 4. A ShowAll public method that overrides the abstract ShowAll in the base class. This
method must log that it is a TwoVals object and then the data in the object. How? Why not call Show2 with an appropriate message!
ThreeVals derives from DataHoldingObjects, implements both the IShow2 and the IShow3 interface and it has:
Three private properties, named FirstInt, FirstFloat and SecondInt that have
automatically generated backing fields. The properties have both a get and set. These properties must be used by the class constructor, the Show2 method and the Show3 method described below. 2. A constructor that receives the three values and saves them using the int and float
properties. 3. A Show2() public method that implements the interface IShow2. The Show2 method must print out the information string and then just the int class data using FirstInt and SecondInt. 4. A Show3() public method that implements the interface IShow3. The Show3 method must print out the information string and then the class data using FirstInt, SecondInt and FirstFloat. 5. A ShowAll public method that overrides the abstract ShowAll in the base class. This
method must log that it is a ThreeVals object and then the data in the object. How? Why not call Show3 with an appropriate message!
Think about what we have set up in this problem. We have an abstract base class and we are deriving two classes from it. One class implements an interface, while the other class implements multiple interfaces. Moreover, since both classes will override an abstract method in the base class, they can both provide information about their contents.
Structuring the problem this way is going to allow us to place instances of the objects in a List of type DataHoldingObjects. We will be able to iterate through the list, printing out all of the data in each object by using the abstract method override ShowAll method. When we loop through the List, we can use the "is" and "as" operators to see if an object supports an interface, is a specific type (or even both conditions!) and we can then invoke it through its interface to do the printing!
The class diagram is provided below. You can see the classes, interfaces, what classes implement the interfaces and the properties and methods in each class. You can see that all classes are derived from a base class called "Object".
Are you surprised to see "Program" as a class? After all, it is a class, just like any other class.
The Test Requirements
Here is what your test steps must do
Make 2 instances of TwoVals, both of which receive the values to hold through the
constructor:
One instance gets the values 1 and 20.5f as positional parameters b. One instance gets the values 20 and -12.3f as named parameters 2. Make 2 instances of ThreeVals, both of which receive the values to hold through the
constructor:
One instance gets the values 30, 200 and 1.5f as positional parameters b. One instance gets the values 20, -100 and -3.3f as named parameters 3. Place the instances in a List of type DataHoldingObjects. Feel free to combine steps
1-3, but you must use a List in this exercise, no matter what your approach is. You saw the use of a List when we did the flowers exercise. 4. Display the data in all of the objects by first logging the number of items in the List, then
using a foreach to iterate through the List and calling the ShowAll() member function to display an object's data. 5. Display the data in the List for each object that implements the IShow2 interface. This
is done by using a foreach statement to iterate through the array, calling Show2 only if the object supports the IShow2 interface. To do this, you will need to see if the object supports the IShow2 interface (use "is") and, if true, declare a reference of type IShow2, set the reference to the object (use "as") and call IShow2 through the interface reference. 6. Display the data in the List for each object that implements the IShow3 interface using
an interface reference. 7. Display the data in the List for each object that is both a TwoVals object AND
implements the IShow3 interface using an interface reference.
Again, how will you know if the object supports an interface and is a certain type? You will have to use the "is" and "as" operators since one derived class supports both interfaces, while the other class only supports one of the interfaces.
You may not combine steps 5, 6 and 7.
Here is what my test routine prints out. Yours must do something VERY similar, including the end of test message:
There are 4 objects in the List Calling ShowAll on each object
The TwoVals object contains: 1, 20.5 The TwoVals object contains: 20, -12.3 The ThreeVals object contains: 30, 200, 1.5 The ThreeVals object contains: 20, -100, -3.3 Looking for all objects that implement IShow2
As an IShow2, the object contains: 1, 20.5 As an IShow2, the object contains: 20, -12.3 As an IShow2, the object contains: 30, 200 As an IShow2, the object contains: 20, -100 Looking for all objects that implement IShow3
As an IShow3, the object contains: 30, 200, 1.5 As an IShow3, the object contains: 20, -100, -3.3 Looking for all objects that are TwoVals and implement IShow3 End of test
Press any key to continue . . .
Problem 2
This problem is designed to make sure you can write a class that implements an interface, make instances of it, and use the interface to access the class contents and print them. It makes sure you know:
how to use a List (mandatory in this problem)
how to use positional and named parameter passage
how to iterate through a List using a foreach
how to write a private properties that have a "get" and "set"
how to build the ability to make instances of classes from within a class through the use of a static member
how to use Console.ReadLine to read in a line of text to convert to the desired data type
how to use TryParse to check console input for a data type other than you want and respond to the error condition
Here is a drawing of what I am giving you to start out with as code. Notice that there is a single interface called IShowAllData. It has not been filled in as yet with the name of the method that will be implemented in class MyProgram and within two other classes that you will write called Employee and TwoVals.
Look at the MyProgram class. It has a property called InstanceName, and 5 methods.
Class Animal has a property called Animaltype and 3 methods.
All of these will be described right after we show what we are trying to achieve as a picture.
Here is a drawing of what you must achieve. Notice that there is a single interface called IShowAllData, but now it is shown to declare a single method called ShowAllData. Classes TwoVals, Employee and MyProgram implement it, but class Animal does not do so and that is intentional!
Classes Animal, TwoVals, Employee and MyProgram are similar in that they each have private properties that hold their data, they each have a constructor, and they each have a static method called MakeInstance. You can see the properties as "wrenches" in the picture
Class MyProgram holds the test entry point in method Main and a very unique method static method called BuildList that is used to prompt the user for the types of objects to place in our List.
All class content will now be described.
The Solution Requirements
The classes Animal, Employee, TwoVals and MyProgram all have private properties that have must have automatically generated backing fields. The properties have both a get and set. These properties must be used by the class constructor and their ShowAllData implementations. The data types are as follows:
Class Property Data type for the Property
Animal Animaltype string
TwoVals FloatData float
TwoVals IntData int
Employee EmployeeName string
Employee EmploymentYears int
MyProgram InstanceName string
The classes Animal, Employee, TwoVals and MyProgram all have a method named ShowAllData. The method is public, since it will be called from outside the class. It receives a string as a parameter and its task is to write out the string and then the data for the class instance.
The classes Animal, Employee, TwoVals and MyProgram all have a single constructor. The constructor in each class must receive parameters to fill in the class properties and must fill in the properties with the received data. For instance, the constructor for Animal must receive a string that it will save in "Animaltype". There can be no defaults used in the classes.
The classes Animal, Employee, TwoVals and MyProgram all have a static method called MakeInstance. When it is called, the method must do the following:
It must prompt the user for a value to use for each of the class properties. It must
continue to re-prompt if the data being requested is other than the desired type (eg. an int) and it must re-prompt if it expects a string and it receives a string that is blank. 2. Once the needed data for the properties has been received, it must make a new
instance of the class, passing the data to the constructor for the class as the constructor parameters. 3. It must return the newly made instance of the class. The means that the return type of MakeInstance is the class name. For instance, in Animal, the method MakeInstance would be declared as:
public static Animal MakeInstance () {....}
Class MyProgram holds your static Main(). It must do the following: