0% found this document useful (0 votes)
79 views

CSharp-OOP-Advanced-Generics-Exercises

This document defines exercises for a C# OOP Advanced course. It describes 11 problems involving generics - creating generic classes like Box and List that can hold different data types, and implementing generic methods to work with and compare their elements.

Uploaded by

Baron Samedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

CSharp-OOP-Advanced-Generics-Exercises

This document defines exercises for a C# OOP Advanced course. It describes 11 problems involving generics - creating generic classes like Box and List that can hold different data types, and implementing generic methods to work with and compare their elements.

Uploaded by

Baron Samedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercises: Generics

This document defines the exercises for "C# OOP Advanced" course @ Software University.
Please submit your solutions (source code) of all below described problems in Judge.

1. Generic Box of String


Create a generic class Box that can be initialized with any type and store the value. Override ToString() method
method and print the type and stored value in format {class full name: value}. On the first line, you will get n - the
number of strings to read from the console. On the next n lines, you will get the actual strings. For each of them
create a box and call its ToString() method to print its data on the console.

Examples
Input Output
2 System.String: life in a box
life in a box System.String: box in a life
box in a life

2. Generic Box of Integer


Use the description of the previous problem but now, test your generic box with Integers.

Examples
Input Output
3 System.Int32: 7
7 System.Int32: 123
123 System.Int32: 42
42

3. Generic Swap Method Strings


Create a generic method that receives a list, containing any type of data and swaps the elements at two given
indexes.
As in the previous problems read n number of boxes of type String and add them to the list. On the next line,
however you will receive a swap command consisting of two indexes. Use the method you've created to swap the
elements that correspond to the given indexes and print each element in the list.

Examples
Input Output
3 System.String: Swap me with Pesho
Pesho System.String: Gosho
Gosho System.String: Pesho
Swap me with Pesho
0 2

4. Generic Swap Method Integers


Use the description of the previous problem but now, test your list of generic boxes with Integers.

Examples
Input Output
3 System.Int32: 42
7 System.Int32: 123

© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 1 of 5
123 System.Int32: 7
42
0 2

5. Generic Count Method Strings


Create a method that receives as an argument a list of any type that can be compared and an element of the given
type. The method should return the count of the elements that are greater than the value of the given element.
Modify your Box class to support comparison by value of the stored data.
On the first line, you will receive n - the number of elements to add to the list. On the next n lines, you will receive
the actual elements. On the last line, you will get the value of the element to which you need to compare every
element in the list.

Examples
Input Output
3 2
aa
aaa
bb
aa

6. Generic Count Method Doubles


Use the description of the previous problem but now, test your list of generic boxes with doubles.

Examples
Input Output
3 2
7.13
123.22
42.78
7.55

7. Custom List
Create a generic data structure that can store any type that can be compared. Implement functions:
 void Add(T element)
 T Remove(int index)
 bool Contains(T element)
 void Swap(int index1, int index2)
 int CountGreaterThan(T element)
 T Max()
 T Min()
Create a command interpreter that reads commands and modifies the custom list that you have created. Set the
custom list’s type to string. Implement the commands:
 Add <element> - Adds the given element to the end of the list
 Remove <index> - Removes the element at the given index
 Contains <element> - Prints if the list contains the given element (True or False)
 Swap <index> <index> - Swaps the elements at the given indexes
 Greater <element> - Counts the elements that are greater than the given element and prints their count
 Max - Prints the maximum element in the list
 Min - Prints the minimum element in the list
 Print - Prints all of the elements in the list, each on a separate line

© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 2 of 5
 END - stops the reading of commands
There will not be any invalid input commands.

Examples
Input Output
Add aa cc
Add bb aa
Add cc 2
Max True
Min cc
Greater aa bb
Swap 0 2 aa
Contains aa
Print
END

8. Custom List Sorter


Extend the previous problem. It should have a method Sort() which can sort objects of type CustomList containing
any type that can be compared. Extend the command list to support one additional command Sort:
 Sort - Sort the elements in the list in ascending order.

Examples
Input Output
Add cc aa
Add bb bb
Add aa cc
Sort
Print
END

9. *Custom List Iterator


For the print command, you have probably used a for loop. Extend your custom list class by making it implement
IEnumerable<T>. This should allow you to iterate your list in a foreach statement.

Examples
Input Output
Add aa cc
Add bb aa
Add cc 2
Max cc
Min bb
Greater aa aa
Swap 0 2
Print
END

10. Tuple
There is something, really annoying in C#. It is called a Tuple. It is a class, which may store a few objects but let’s
focus on the type of Tuple which contains two objects. The first one is “item1” and the second one is “item2”. It is
kind of like a KeyValuePair except – it simply has items, which are neither key nor value. The annoyance is coming

© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 3 of 5
from the fact, that you have no idea what these objects are holding. The class name is telling you nothing, the
methods, which it has – also. So, let’s say for some reason we would like to try to implement it by ourselves.

The task: Create a class “Tuple”, which is holding two objects. Like we said, the first one, will be “ item1” and the
second one - “item2”. The tricky part here is to make the class hold generics. This means, that when you create a
new object of class - “Tuple”, there should be a way to explicitly, specify both the items’ type separately.
Input
The input consists of three lines:
 The first one is holding a person name and an address. They are separated by space(s). Your task is to collect
them in the tuple and print them on the console. Format of the input:
<first name> <last name>> <address>
 The second line holds a name of a person and the amount of beer (int) he can drink. Format:
<name> <liters of beer>
 The last line will hold an Integer and a Double. Format:
<Integer> <Double>

Output
 Print the tuples’ items in format: {item1} -> {item2}

Constraints
Use the good practices we have learned. Create the class and make it have getters and setters for its class variables.
The input will be valid, no need to check it explicitly!

Example
Input Output
Sofka Tripova Stolipinovo Sofka Tripova -> Stolipinovo
Az 2 Az -> 2
23 21.23212321 23 -> 21.23212321

11. Threeuple
Create a Class Threeuple. Its name is telling us, that it will hold no longer, just a pair of objects. The task is simple,
our Threeuple should hold three objects. Make it have getters and setters. You can even extend the previous class
Input
The input consists of three lines:
 The first one is holding a name, an address and a town. Format of the input:
<<first name> <last name>> <address> <town>
 The second line is holding a name, beer liters, and a Boolean variable with value - drunk or not. Format:
<name> <liters of beer> <drunk or not>
 The last line will hold a name, a bank balance (double) and a bank name. Format:
<name> <account balance> <bank name>

Output
 Print the Threeuples’ objects in format: {firstElement} -> {secondElement} -> {thirdElement}

Examples
Input Output
Sofka Tripova Stolipinovo Plovdiv Sofka Tripova -> Stolipinovo -> Plovdiv
MitkoShtaigata 18 drunk MitkoShtaigata -> 18 -> True
SashoKompota 0.10 NkqfaBanka SashoKompota -> 0.1 -> NkqfaBanka

© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 4 of 5
Ivan Ivanov Tepeto Plovdiv Ivan Ivanov -> Tepeto -> Plovdiv
Mitko 18 not Mitko -> 18 -> False
Sasho 0.10 NGB Sasho -> 0.1 -> NGB

Note
You may extend your previous solution.

© Software University Foundation. This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 5 of 5

You might also like