Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a generic class, madLabClass, with one type parameter (the generic stuff we learned) The class will have 3 backing fields One is an integer
Create a generic class, madLabClass, with one type parameter (the generic stuff we learned)
- The class will have 3 backing fields
- One is an integer for an index
- The second is an array of the type passed in through the type parameter
- The third is an integer for insertion index
- A constructor for the class will take in one parameter of int
- This will initialize the backing field array to be of the size sent in
- Remember: initialize means making the actual object itself
- The index backing field will be set to 0
- The insertion index backing field will be set to 0
- This will initialize the backing field array to be of the size sent in
- A method called Stuff
- This will have one parameter
- Type will be the type parameter
- When called, it will insert the value provided to the method at the index position stored in the insertion index field
- Increment the insertion index field
- The insertion index field stop at the maximum value allowed for the array
- All future calls to stuff will keep setting the last item of the array to the new value
- This will have one parameter
- A method called Stuff
- This will have two parameters
- One is of the type parameter
- The other is an integer
- This will insert the value (the type parameter) into the location specified by the integer
- If the integer value is too large, display a message saying that the value could not be inserted into that position (tell me the value and the position) and tell me how many items the array can actually hold
- This will have two parameters
- A method called DisplayValue
- print out the current index and the current value of the element in the array being pointed at by the index
- E.g. The first time this is called and if my array has {"green", "red", "blue"} we may see
- print out the current index and the current value of the element in the array being pointed at by the index
index 0 = green
- advance the index by one
- if the index is out of range, loop it back to the beginning
- if the array has 3 items, but index is 3 (integer = 3), we are now wanting to access the fourth item, which does not exist.
- If this is the case, how do we get back to the first item?
- Show it working
In the main of the program
- instantiate your class using one data type
- Add some values to your array
- Run DisplayValue numerous times
- Instantiate another variable of the madLabClass using a different datatype
- Add some values
- Run DisplayValue more times than there are array elements (e.g. if my array can hold 21 items, run it 23 times)
Question 2
Add to your madLabClass a generic method called talkToMe
- Will have a type parameter list of 2 types
- Will have 3 method parameters, using a mix of the 2 types (mix it up)
- E.g. 2 method parameters of type1 and 1 of type2, or ...
- Will print out the value of the 3 objects and the types of the variables
- Remember to use the toString function (we don't know if they can be printed out unless we explicitly make them strings)
- The GetType function will return the datatype of the variable
- myVariable.GetType()
- Will be a static method
- Will not return anything
Invoke and execute a few different variations of talkToMe
Example output could be:
varl (System.String) = Hello var2 (System.Int32) = 1 var3 (System.String) Press any key to continue = bob
Step by Step Solution
There are 3 Steps involved in it
Step: 1
using System public class madLabClass T private int index ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started