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

Defining Classes Exercises

The document provides examples and problems for defining classes in C#. It includes defining a Person class with name and age fields, adding constructors to the Person class, creating a Family class with methods to add members and get the oldest member, performing an opinion poll on people over 30, calculating date differences, defining an Employee class and finding the department with the highest average salary, tracking cars with a fuel amount and consumption, and defining a nested Car class with engine, cargo and tire components. The problems increase in complexity and cover key class concepts like fields, methods, nested classes and constructors.

Uploaded by

Tấn Phong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Defining Classes Exercises

The document provides examples and problems for defining classes in C#. It includes defining a Person class with name and age fields, adding constructors to the Person class, creating a Family class with methods to add members and get the oldest member, performing an opinion poll on people over 30, calculating date differences, defining an Employee class and finding the department with the highest average salary, tracking cars with a fuel amount and consumption, and defining a nested Car class with engine, cargo and tire components. The problems increase in complexity and cover key class concepts like fields, methods, nested classes and constructors.

Uploaded by

Tấn Phong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercises: Defining Classes

Problem 1. Define a class Person


Define a class Person with public fields for name and age.

Note
Add the following code to your main method and submit it to Judge.

static void Main(string[] args)


{
Type personType = typeof (Person);
FieldInfo[] fields = personType.GetFields(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine(fields.Length);
}

If you defined the class correctly, the test should pass.

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.

Problem 2. Creating Constructors


Add 3 constructors to the Person class from the last task, use constructor chaining to reuse code:

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.

Type personType = typeof(Person);


ConstructorInfo emptyCtor = personType.GetConstructor(new Type[] { });
ConstructorInfo ageCtor = personType.GetConstructor(new[] { typeof(int) });
ConstructorInfo nameAgeCtor = personType.GetConstructor(new[] { typeof(string), typeof(int) });
bool swapped = false;
if (nameAgeCtor == null)
{

Page 1 of 11
nameAgeCtor = personType.GetConstructor(new[] { typeof(int), typeof(string) });
swapped = true;
}

string name = Console.ReadLine();


int age = int.Parse(Console.ReadLine());

Person basePerson = (Person)emptyCtor.Invoke(new object[] { });


Person personWithAge = (Person)ageCtor.Invoke(new object[] { age });
Person personWithAgeAndName = swapped ? (Person)nameAgeCtor.Invoke(new object[] { age, name }) :
(Person)nameAgeCtor.Invoke(new object[] { name, age });

Console.WriteLine("{0} {1}", basePerson.name, basePerson.age);


Console.WriteLine("{0} {1}", personWithAge.name, personWithAge.age);
Console.WriteLine("{0} {1}", personWithAgeAndName.name, personWithAgeAndName.age);

If you defined the constructors correctly, the test should pass.

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

Problem 3. Oldest Family Member


Create class Person with fields name and age. Create a class Family. The class should have list of people, method for
adding members (void AddMember(Person member)) and a method returning the oldest family member (Person
GetOldestMember()). Write a program that reads name and age for N people and add them to the family. Then
print the name and age of the oldest member.

Note
Add the following code to your main method before your code and submit it to Judge.

MethodInfo oldestMemberMethod = typeof(Family).GetMethod("GetOldestMember");


MethodInfo addMemberMethod = typeof(Family).GetMethod("AddMember");
if(oldestMemberMethod == null || addMemberMethod == null)
{
throw new Exception();
}
If you defined the class correctly, the test should pass.

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

Problem 4. Opinion Poll


Using the Person class, write a program that reads from the console N lines of personal information and then prints
all people whose age is more than 30 years, sorted in alphabetical order.

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

Problem 5. Date Modifier


Create a class DateModifier which stores the difference of the days between two Dates. It should have a method
which takes two string parameters representing a date as strings and calculates the difference in the days between
them.

Examples
Input Output
1992 05 31
8783
2016 06 17
2016 05 31
42
2016 04 19

Problem 6. Company Roster


Define a class Employee that holds the following information: name, salary, position, department, email and age.
The name, salary, position and department are mandatory while the rest are optional.

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

Problem 7. Speed Racing


Your task is to implement a program that keeps track of cars and their fuel and supports methods for moving the
cars. Define a class Car that keeps track of a car’s Model, fuel amount, fuel consumption for 1 kilometer and
distance traveled. A Car’s Model is unique - there will never be 2 cars with the same model.

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

Problem 8. Raw Data


You are the owner of a courier company and want to make a system for tracking your cars and their cargo. Define a
class Car that holds information about model, engine, cargo and a collection of exactly 4 tires. The engine, cargo
and tire should be separate classes, create a constructor that receives all information about the Car and creates and
initializes its inner components (engine, cargo and tires).

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

Problem 9. Rectangle Intersection


Create a class Rectangle. It should consist of an ID, width, height and the coordinates of its top left corner
(horizontal and vertical). Create a method which receives as a parameter another Rectangle, checks if the two
rectangles intersect and returns true or false.

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

Problem 10.Car Salesman


Define two classes Car and Engine. A Car has a model, engine, weight and color. An Engine has model, power,
displacement and efficiency. A Car’s weight and color and its Engine’s displacements and efficiency are optional.

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

Problem 11.Pokemon Trainer


You wanna be the very best pokemon trainer, like no one ever was, so you set out to catch pokemon. Define a class
Trainer and a class Pokemon. Trainers have a name, number of badges and a collection of pokemon, Pokemon
have a name, an element and health, all values are mandatory. Every Trainer starts with 0 badges.

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:

 “<Name> company <companyName> <department> <salary>”


 “<Name> pokemon <pokemonName> <pokemonType>”
 “<Name> parents <parentName> <parentBirthday>”
 “<Name> children <childName> <childBirthday>”
 “<Name> car <carModel> <carSpeed>”

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:

JelioJelev pokemon Onyx Rock JelioJelev


JelioJelev parents JeleJelev 13/03/1933 Company:
GoshoGoshev pokemon Moltres Fire JeleInc Jelior 777.77
JelioJelev company JeleInc Jelior 777.77 Car:
JelioJelev children PudingJelev 01/01/2001 AudiA4 180
StamatStamatov pokemon Blastoise Water Pokemon:
JelioJelev car AudiA4 180 Onyx Rock
JelioJelev pokemon Charizard Fire Charizard Fire
End Parents:
JelioJelev JeleJelev 13/03/1933
Children:
PudingJelev 01/01/2001

Bonus*
Override the ToString() method in the classes to standardize the displaying of objects.

Problem 13.Family Tree


You want to build your family tree, so you went to ask your grandmother, sadly your grandmother keeps
remembering information about your predecessors in pieces, so it falls to you to group the information and build
the family tree.

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 information will be in one of the following formats:

 “FirstName LastName - FirstName LastName”


 “FirstName LastName - day/month/year”
 “day/month/year - FirstName LastName”
 “day/month/year - day/month/year”
 “FirstName LastName day/month/year”

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

Problem 14.*Cat Lady


Ginka has many cats in her house of various breeds, since some breeds have specific characteristics, Ginka needs
some way to catalogue the cats, help her by creating a class hierarchy with all her breeds of cats so that she can
easily check on their characteristics. Ginka has 3 specific breeds of cats “Siamese”, “Cymric” and the very famous
bulgarian breed “Street Extraordinaire”, each breed has a specific characteristic about which information should be
kept. For the Siamese cats their ear size should be kept, for Cymric cats - the length of their fur in centimeters and
for the Street Extraordinaire the decibels of their meowing during the night.

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:

 “Siamese <name> <earSize>”


 “Cymric <name> <furLength>”
 “StreetExtraordinaire <name> <decibelsOfMeows>”

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.

Problem 15.*Drawing tool


You are a young programmer and your Boss is giving you a task to create a tool which is drawing figures on the
console. He knows you are not so good at OOP tasks so he told you to create only a single class - CorDraw. Its task is
to draw rectangular figures on the screen.

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

Input Output Comment


Rectangle |- - - - - - -| The Rectangle’s width
7 | | is 7 and the length is 3
3 |- - - - - - -|

Page 11 of 11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy