logo Use CA10RAM to get 10%* Discount.
Order Nowlogo

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: 

  1. 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: 

  1. 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: 

  1. 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: 

  1. 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: 

  1. 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 

  1. Make 2 instances of TwoVals, both of which receive the values to hold through the 

constructor: 

  1. 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: 

  1. 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: 

  1. 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: 

  1. Declare a List of type that will subsequently be filled with instances of our 

    classes. The declaration looks like: 

    List TVL = new List(); 

    1. It calls BuildList, passing the list instance to the method 3. It displays the number of objects in the list 4. Using a foreach, it loops through the list, identifying what each object type is (using"is"), 

    casting the object to the type and calling the object's ShowAllData method with a phrase like "TwoVals :". 5. It determines how many of the objects in the list implement the IShowAllData interface 

    and displays the count. Perhaps "as" might be useful here. 6. For every object that has signed up as implementing the IShowAllData interface, it calls 

    the object through a reference to the interface. Since the call will expect a string to print out before it prints the object's data, I have provided the code to create the string in the starting code that is being provided. 

    The Test Requirements 

    1. You need to implement the missing functionality. 2. You need to implement Main() as stated above. 

    How to proceed with the solution: 

    It's up to you but here is some guidance. 

    Step 1: 

    I would start by downloading and compiling the code provided in the week's folder as Shell.txt. It has partial implementations for Main, BuildList and a complete Animal class. The program, when run, will create a List and call BuildList. In BuildList, it will display a menu of options that looks like this: 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 

    If you enter a bad number or the numbers 1, 3 or 4 it will just redisplay the menu. If you enter the number 2, you will find that it prompts for a type of animal. Try entering just a c/r or just some blanks. You will see it just loops, re-prompting for entry pf a type of animal. 

    Now enter something like "whale". Back will come the menu. 

    Take a moment to look inside the Animal class MakeInstance and observe the code that just got executed. You will see the loop for the prompt, the error checking, the call to make an instance of Animal with the received type of animal, and a return of the object just made. 

    Now look at BuildList() and see how it determined to call the Animal method MakeInstance. You will see it used the menu item number, called Animal. MakeInstance and added the object returned by MakeInstance to the list! 

    Enter a choice of 2 again, and this time enter "fish". 

    Now comes the good part.... Enter 5 to indicate we are done with placing objects in the list. You will see that it does Main step 2 and part of step 3. It looks like: 

    Calling method ShowAllData on 2 objects in the list... Animal : whale Animal : fish 

    Step 2: 

    I would do several things at this point. 

    1. I would fill out the interface IShowAllData by adding the declaration of method 

    ShowAllData. You can see what the expected method declaration looks like by looking it up in the Animal class or even in class MyProgram! Make sure your program still compiles at this point. 2. I would temporarily make Animal claim it implements interface IShowAllData. Make 

    sure your code still compiles. 3. I would add the code in Main() that makes a call to ShowAllData at the bottom of 

    Main(), passing it the string "S". Watch what happens if you repeat the exercise! 

    Calling method ShowAllData on 2 objects in the list... Animal : whale Animal : fish Animal whale Animal fish Press any key to continue . . . 

    1. See how the code that was added now senses that these objects satisfy the 

    IShowAllData interface and that the string passed to the call to ShowAllData contains the class name? See that the call produced a line with the class name and the object data?? 

    Step 3: 

    Time to fill in the missing code that counts the number of objects in the List that have implemented the interface IShowAllData and prints out the count. How you do that is up to you, but I strongly urge you to do so at this point!!! 

    It should tell you that 2 objects have implemented the interface. 

    Step 4: 

    Class MyProgram is supposed to implement the IShowAllData interface, so: 

    1. Add the code that claims it does! 2. Update the code in BuildList by adding a case for #4 that is modelled after case #2. 3. Make sure the program compiles. Run a test by choosing item 4 several times. Each time enter something like ProgramX and ProgramY. You can even continue to add in animals like whale and fish. When you enter 5, the number of objects in the list, the number of objects implementing IShowAllData and the printout of the data should make sense! All we did is complete supporting the class MyProgram
    2. Time to remove the temporary claim we made on the Animal class implementing IShowAllData. Delete the claim and rerun. Now, if you place 2 Animal and 2 MyProgram objects in the list, the number of objects printed will be 4 but the number of objects claiming to implement IShowAllData will be 2. Only the MyProgram objects will get listed in the claim! Perfect. 

    Step 5

    Time to either add class Employee or class TwoVals

    1. Model them on class Animal, but have them claim that they implement interface 

    IShowAllData. 2. Make sure you have the properties declared 3. Make sure their constructors receive parameters sufficient to set their properties 4. Make sure their ShowAllData prints out the string provided and then their properties 5. Make sure their MakeInstance prompts, detects errors and re-prompts until the 

    information is valid. See the example in the output below for error processing 6. Make sure they make a new instance of their class and return it 7. Be sure the code for the class you are creating compiles 

    Step 6: 

    Modify the code in BuildList to add a case for the class you have coded in Step 5. Compile the code and test it. You should now be able to create instances of yet another class and have it placed in the list and reported as implementing the IShowAllData interface. 

    Once it is working, repeat steps 5 and 6 for the remaining class and test it. 

    Step 7: 

    Checkout the code that immediately follows the call to BuildList. You will see code is missing for everything but Animal. Add the code for the 3 other classes. 

    If you have done everything, a test might look like this (complete with error detection and processing): 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 2 Enter the type of animal: whale 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 

    4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 3 Enter an employee name: Tom Enter the years with the company: z Enter the years with the company: 20 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 4 Enter a name for the MyProgram instance: ProgramX 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 1 Enter an integer: 12 Enter a float: a Enter a float: 2.33 

    Choose a class instance to add to the List or Finish to proceed: 

    1 - A TwoVals 2 - An Animal 3 - An Employee 4 - A MyProgram 5 - Finish Enter a number from 1 to 5: 5 

    There are 4 objects that have implemented method ShowAllData... Animal : whale Employee : Tom and 20 MyProgram : ProgramX TwoVals : 12 and 2.33 

    There are 3 objects that have implemented interface IShowAllData... Employee : Tom and 20 MyProgram : ProgramX TwoVals : 12 and 2.33 

    Press any key to continue . . . 

    Problem 3 

    This problem is designed to write a TwoVals class as a generic class, make instances of it and show the data contained within the instances. The class will be designed to support data of different types, specified in the generic declaration as T1 and T2. 

    It makes sure you remember 

    • how to use a generic class 
    • how to use positional and named parameter passage 
    • how to write a property that has a public "get" and private "set" 
    • how to use var 
    • how to use a common format specifier like we did early in the term 

    The Solution Requirements 

    Our class will be called GenericTwoVals. It has the following: 

    1. Three properties, all of which that have automatically generated backing fields. All three 

    have a public get and a private set. 

    1. Two properties, named Data1 and Data2 are generic. b. A third property named Identifier is a string and is not generic. 
    2. A constructor that receives three parameters and saves them as the data members 

    using the three properties. 

    The Test Requirements 

    Here is what your test steps must do 

    1. Make 3 instances of GenericTwoVals. At least 1 of the instances must provide parameters using named parameters and at least one of the instances must use positional parameters. The instances are meant to receive the values through the class constructor and hold 
    2. two ints (use an identifier of "GenericTwoVals<int, int>" and the numbers 1 and 

    2) b. an int and a float (use an identifier of "GenericTwoVals<int, float>" and the 

    numbers 3 and 4.5f) c. a string and a float (use an identifier of "GenericTwoVals<string, float>" and use 

    your first name and the number 99.9f) 2. For one of the instances, you must declare the instance with the constructed type on 

    each side of the assignment. You must declare the other two using the var keyword to infer the type. See page 429 of your text if you need assistance 

    1. For each instance, use properties Identifier, Data1 and Data2 to retrieve the data. Print it in your test code. This means that the GenericTwoVals class will have no console I/O. 

    Note that the printing of the data must use a single format specifier as we learned how to do early in the term 

    For your convenience, here are the comments from within my test: 

    static void Main(string[] args) 

    { // The format statement that will be used follows // Make an instance of GenericTwoVals for <int, int> using "var" // Print out the data in the object using the common format declared above // Make an instance of GenericTwoVals for <int, float> using "var" // Print out the data in the object using the common format declared above // Make an instance of GenericTwoVals for <string, float> using the constructed type // Print out the data in the object using the common format declared above } 

    For your convenience, here is the output of my program: 

    GenericTwoVals <int, int> has data 1 and 2 Generic TwoVals<int, float> has data 3 and 4.5 Generic TwoVals<string, float> has data Tom and 99.9 Press any key to continue . . . 

Expert's Answer

799 Times Downloaded

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

799 Times Downloaded

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

767 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

788 Answers

Hire Me
expert
Husnain SaeedComputer science

681 Answers

Hire Me
expert
Atharva PatilComputer science

648 Answers

Hire Me