Defining Classes Exercises
Defining Classes Exercises
Note
Add the following code to your main method and submit it to Judge.
Bonus*
Try to create a few objects of type Person:
Name Age
Pesho 20
Gosho 18
Stamat 43
Use both the inline initialization and the default constructor. Your fields should be public, otherwise you won’t see
them.
1. The first should take no arguments and produce a person with name “No name” and age = 1.
2. The second should accept only an integer number for the age and produce a person with name “No name”
and age equal to the passed parameter.
3. The third one should accept a string for the name and an integer for the age and should produce a person
with the given name and age.
Add the following code to your main method and submit it to Judge.
Page 1 of 11
nameAgeCtor = personType.GetConstructor(new[] { typeof(int), typeof(string) });
swapped = true;
}
Examples
Input Output
Pesho No name 1
20 No name 20
Pesho 20
Gosho No name 1
18 No name 18
Gosho 18
Stamat No name 1
43 No name 43
Stamat 43
Note
Add the following code to your main method before your code and submit it to Judge.
Page 2 of 11
Examples
Input Output Input Output
3 Annie 5 5 Ivan 35
Pesho 3 Steve 10
Gosho 4 Christopher 15
Annie 5 Annie 4
Ivan 35
Maria 34
Examples
Input Output
3 Ivan - 48
Pesho 12 Stamat - 31
Stamat 31
Ivan 48
5 Lyubo - 44
Nikolai 33 Nikolai - 33
Yordan 88 Yordan - 88
Tosho 22
Lyubo 44
Stanislav 11
Examples
Input Output
1992 05 31
8783
2016 06 17
2016 05 31
42
2016 04 19
Page 3 of 11
Your task is to write a program which takes N lines of employees from the console and calculates the department
with the highest average salary and prints for each employee in that department his name, salary, email and age –
sorted by salary in descending order. If an employee doesn’t have an email – in place of that field you should print
“n/a” instead, if he doesn’t have an age – print “-1” instead. The salary should be printed to two decimal places
after the seperator.
Examples
Input Output
4 Highest Average Salary: Development
Pesho 120.00 Dev Development pesho@abv.bg 28 Ivan 840.20 ivan@ivan.com -1
Toncho 333.33 Manager Marketing 33 Pesho 120.00 pesho@abv.bg 28
Ivan 840.20 ProjectLeader Development ivan@ivan.com
Gosho 0.20 Freeloader Nowhere 18
6 Highest Average Salary: Sales
Stanimir 496.37 Temp Coding stancho@yahoo.com Yovcho 610.13 n/a -1
Yovcho 610.13 Manager Sales Toshko 609.99 toshko@abv.bg 44
Toshko 609.99 Manager Sales toshko@abv.bg 44
Venci 0.02 Director BeerDrinking beer@beer.br 23
Andrei 700.00 Director Coding
Popeye 13.3333 Sailor SpinachGroup popeye@pop.ey
On the first line of the input you will receive a number N – the number of cars you need to track, on each of the
next N lines you will receive information for a car in the following format “<Model> <FuelAmount>
<FuelConsumptionFor1km>”, all cars start at 0 kilometers traveled.
After the N lines, until the command “End” is received, you will receive a commands in the following format “Drive
<CarModel> <amountOfKm>”. Implement a method in the Car class to calculate whether or not a car can move
that distance, if it can the car’s fuel amount should be reduced by the amount of used fuel and its distance traveled
should be increased by the amount of kilometers traveled, otherwise the car should not move (Its fuel amount and
distance traveled should stay the same) and you should print on the console “Insufficient fuel for the drive”. After
the “End” command is received, print each car and its current fuel amount and distance traveled in the format
“<Model> <fuelAmount> <distanceTraveled>”, where the fuel amount should be printed to two decimal places
after the separator.
Examples
Input Output
2 AudiA4 17.60 18
AudiA4 23 0.3 BMW-M2 21.48 56
BMW-M2 45 0.42
Drive BMW-M2 56
Drive AudiA4 5
Drive AudiA4 13
Page 4 of 11
End
3 Insufficient fuel for the drive
AudiA4 18 0.34 Insufficient fuel for the drive
BMW-M2 33 0.41 AudiA4 1.00 50
Ferrari-488Spider 50 0.47 BMW-M2 33.00 0
Drive Ferrari-488Spider 97 Ferrari-488Spider 4.41 97
Drive Ferrari-488Spider 35
Drive AudiA4 85
Drive AudiA4 50
End
On the first line of input you will receive a number N - the amount of cars you have, on each of the next N lines you
will receive information about a car in the format “<Model> <EngineSpeed> <EnginePower> <CargoWeight>
<CargoType> <Tire1Pressure> <Tire1Age> <Tire2Pressure> <Tire2Age> <Tire3Pressure> <Tire3Age>
<Tire4Pressure> <Tire4Age>” where the speed, power, weight and tire age are integers, tire pressure is a double.
After the N lines you will receive a single line with one of 2 commands “fragile” or “flamable” , if the command is
“fragile” print all cars whose Cargo Type is “fragile” with a tire whose pressure is < 1, if the command is “flamable”
print all cars whose Cargo Type is “flamable” and have Engine Power > 250. The cars should be printed in order of
appearing in the input.
Examples
Input Output
2 Citroen2CV
ChevroletAstro 200 180 1000 fragile 1.3 1 1.5 2 1.4 2 1.7 4
Citroen2CV 190 165 1200 fragile 0.9 3 0.85 2 0.95 2 1.1 1
fragile
4 ChevroletExpress
ChevroletExpress 215 255 1200 flamable 2.5 1 2.4 2 2.7 1 2.8 1 DaciaDokker
ChevroletAstro 210 230 1000 flamable 2 1 1.9 2 1.7 3 2.1 1
DaciaDokker 230 275 1400 flamable 2.2 1 2.3 1 2.4 1 2 1
Citroen2CV 190 165 1200 fragile 0.8 3 0.85 2 0.7 5 0.95 2
flamable
On the first line you will receive the number of rectangles – N and the number of intersection checks – M. On the
next N lines, you will get the rectangles with their ID, width, height and coordinates. On the last M lines, you will
get pairs of IDs which represent rectangles. Print if each of the pairs intersect.
Page 5 of 11
You will always receive valid data. There is no need to check if a rectangle exists.
Examples
Input Output
2 1 true
Pesho 2 2 0 0
Gosho 2 2 0 0
Pesho Gosho
On the first line you will read a number N which will specify how many lines of engines you will receive, on each of
the next N lines you will receive information about an Engine in the following format “<Model> <Power>
<Displacement> <Efficiency>”. After the lines with engines, on the next line you will receive a number M –
specifying the number of Cars that will follow, on each of the next M lines information about a Car will follow in the
following format “<Model> <Engine> <Weight> <Color>”, where the engine in the format will be the model of an
existing Engine. When creating the object for a Car, you should keep a reference to the real engine in it, instead of
just the engine’s model, note that the optional properties might be missing from the formats.
Your task is to print each car (in the order you received them) and its information in the format defined bellow, if
any of the optional fields has not been given print “n/a” in its place instead:
<CarModel>:
<EngineModel>:
Power: <EnginePower>
Displacement: <EngineDisplacement>
Efficiency: <EngineEfficiency>
Weight: <CarWeight>
Color: <CarColor>
Bonus*
Override the classes’s ToString() methods to have a reusable way of displaying the objects.
Examples
Input Output
2 FordFocus:
V8-101 220 50 V4-33:
V4-33 140 28 B Power: 140
3 Displacement: 28
FordFocus V4-33 1300 Silver Efficiency: B
FordMustang V8-101 Weight: 1300
VolkswagenGolf V4-33 Orange Color: Silver
FordMustang:
V8-101:
Power: 220
Page 6 of 11
Displacement: 50
Efficiency: n/a
Weight: n/a
Color: n/a
VolkswagenGolf:
V4-33:
Power: 140
Displacement: 28
Efficiency: B
Weight: n/a
Color: Orange
4 FordMondeo:
DSL-10 280 B DSL-13:
V7-55 200 35 Power: 305
DSL-13 305 55 A+ Displacement: 55
V7-54 190 30 D Efficiency: A+
4 Weight: n/a
FordMondeo DSL-13 Purple Color: Purple
VolkswagenPolo V7-54 1200 Yellow VolkswagenPolo:
VolkswagenPassat DSL-10 1375 Blue V7-54:
FordFusion DSL-13 Power: 190
Displacement: 30
Efficiency: D
Weight: 1200
Color: Yellow
VolkswagenPassat:
DSL-10:
Power: 280
Displacement: n/a
Efficiency: B
Weight: 1375
Color: Blue
FordFusion:
DSL-13:
Power: 305
Displacement: 55
Efficiency: A+
Weight: n/a
Color: n/a
From the console you will receive an unknown number of lines until you receive the command “ Tournament”, each
line will carry information about a pokemon and the trainer who caught it in the format “<TrainerName>
<PokemonName> <PokemonElement> <PokemonHealth>” where TrainerName is the name of the Trainer who
caught the pokemon, names are unique there cannot be 2 trainers with the same name. After receiving the
command “Tournament” an unknown number of lines containing one of the three elements “Fire”, “Water”,
“Electricity” will follow until the command “End” is received. For every command you must check if a trainer has at
Page 7 of 11
least 1 pokemon with the given element, if he does he receives 1 badge, otherwise all his pokemon lose 10 health, if
a pokemon falls to 0 or less health he dies and must be deleted from the trainer’s collection. After the command
“End” is received you should print all trainers sorted by the amount of badges they have in descending order (if
two trainers have the same amount of badges they should be sorted by order of appearance in the input) in the
format “<TrainerName> <Badges> <NumberOfPokemon>”.
Examples
Input Output
Pesho Charizard Fire 100 Pesho 2 2
Gosho Squirtle Water 38 Gosho 0 1
Pesho Pikachu Electricity 10
Tournament
Fire
Electricity
End
Stamat Blastoise Water 18 Nasko 1 1
Nasko Pikachu Electricity 22 Stamat 0 0
Jicata Kadabra Psychic 90 Jicata 0 1
Tournament
Fire
Electricity
Fire
End
Problem 12.Google
Google is always watching you, so it should come as no surprise that they know everything about you (even your
pokemon collection), since you’re really good at writing classes Google asked you to design a Class that can hold all
the information they need for people.
From the console you will receive an unkown amount of lines until the command “End” is read, on each of those
lines there will be information about a person in one of the following formats:
You should structure all information about a person in a class with nested subclasses. People’s names are unique -
there won’t be 2 people with the same name, a person can also have only 1 company and car, but can have
multiple parents, children and pokemons. After the command “End” is received on the next line you will receive a
single name, you should print all information about that person. Note that information can change during the input,
for instance if we receive multiple lines which specify a person’s company, only the last one should be the one
remembered. The salary must be formated to two decimal places after the seperator.
Examples
Input Output
PeshoPeshev company PeshInc Management 1000.00 TonchoTonchev
Page 8 of 11
TonchoTonchev car Trabant 30 Company:
PeshoPeshev pokemon Pikachu Electricity Car:
PeshoPeshev parents PoshoPeshev 22/02/1920 Trabant 30
TonchoTonchev pokemon Electrode Electricity Pokemon:
End Electrode Electricity
TonchoTonchev Parents:
Children:
Bonus*
Override the ToString() method in the classes to standardize the displaying of objects.
On the first line of the input you will receive either a name or a birthdate in the format “<FirstName> <LastName>”
or “day/month/year” – your task is to find the person’s information in the family tree. On the next lines until the
command “End” is received you will receive information about your predecessors that you will use to build the
family tree.
The first 4 formats reveal a family tie – the person on the left is parent to the person on the right (as you can see
the format does not need to contain names, for example the 4 th format means the person born on the left date is
parent to the person born on the right date). The last format ties different information together – i.e. the person
with that name was born on that date. Names and birthdates are unique – there won’t be 2 people with the same
name or birthdate, there will always be enough entries to construct the family tree (all people’s names and
birthdates are known and they have atleast one connection to another person in the tree).
Page 9 of 11
After the command “End” is received you should print all information about the person whose name or birthdate
you received on the first line – his name, birthday, parents and children (check the examples for the format). The
people in the parents and childrens lists should be ordered by their first appearance in the input (regardless if they
appeared as a birthdate or a name, for example in the first input Stamat is before Penka because he has appeared
first on the second line, while she appears on the third one).
Examples
Input Output
Pesho Peshev Pesho Peshev 23/5/1980
11/11/1951 - 23/5/1980 Parents:
Penka Pesheva - 23/5/1980 Stamat Peshev 11/11/1951
Penka Pesheva 9/2/1953 Penka Pesheva 9/2/1953
Pesho Peshev - Gancho Peshev Children:
Gancho Peshev 1/1/2005 Gancho Peshev 1/1/2005
Stamat Peshev 11/11/1951
Pesho Peshev 23/5/1980
End
13/12/1993 Moncho Tonchev 13/12/1993
25/3/1934 - 4/4/1961 Parents:
Poncho Tonchev 25/3/1934 Toncho Tonchev 4/4/1961
4/4/1961 - Moncho Tonchev Children:
Toncho Tonchev - Lomcho Tonchev
Moncho Tonchev 13/12/1993
Lomcho Tonchev 7/7/1995
Toncho Tonchev 4/4/1961
End
From the console you will receive lines of information with cats. Until the command “End” is received, the
information will come in one of the following formats:
On the last line after the “End” command you will receive the name of a cat, you should print that cat’s information
in the same format in which you received it (with fur size being formated to two decimal places after the separator).
Ear size and decibels will always be positive integer numbers, cat names are unique.
Example
Input Output
Page 10 of 11
StreetExtraordinaire Maca 85 Cymric Top 2.80
Siamese Sim 4
Cymric Tom 2.80
End
Tom
StreetExtraordinaire Koti 80 StreetExtraordinaire Maca 100
StreetExtraordinaire Maca 100
Cymric Tim 3.10
End
Maca
Hint
Use class inheritance to represent the cat hierarchy and override the ToString() methods of concrete breeds to allow
for easy printing of the cat, regardless the breed.
CorDraw’s constructor should take as parameter a Square instance or a Rectangle instance, extract its characteristics
and draw the figure. Like we said your Boss is a good guy and he has some more info for you:
One of the extra classes you will need should be a class named Square that should have only one method – Draw()
which uses the length of the square’s sides and draws them on the console. For horizontal lines, use dashes ("-") and
spaces (" "). For vertical lines – pipes ("|"). If the size of the figure is 6, dashes should also be 6.
Hint
Search in the internet for abstract classes and try implementing one. This will help you to reduce the input
parameters in the CorDraw’s constructor to a single one.
Examples
Input Output Comment
Square |- - -| Square’s size is 3 so we
3 | | draw 3 pipes down and
|- - -| 3 dashes across
Page 11 of 11