CSharp-OOP-Advanced-Generics-Exercises
CSharp-OOP-Advanced-Generics-Exercises
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.
Examples
Input Output
2 System.String: life in a box
life in a box System.String: box in a life
box in a life
Examples
Input Output
3 System.Int32: 7
7 System.Int32: 123
123 System.Int32: 42
42
Examples
Input Output
3 System.String: Swap me with Pesho
Pesho System.String: Gosho
Gosho System.String: Pesho
Swap me with Pesho
0 2
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
Examples
Input Output
3 2
aa
aaa
bb
aa
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
Examples
Input Output
Add cc aa
Add bb bb
Add aa cc
Sort
Print
END
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