Practices C#

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

M.

Sahim Med Said

Practices: OOP in C#
Constructors and destructors
Create a C# program that prompts the user for three names of people and stores
them in an array of Person-type objects. To do this, first create a Person class that
has a Name property of type string , a constructor that receives the name as a
parameter, a destructor that assigns the name to empty and overwrites the ToString
() method.

End the program by reading the people and executing the ToString () method on
screen.

Class person, student and teacher


Create a new C # project with three classes plus another class to test the logic in
your code. The main classes of the program are the following classes:

• Person
• Student
• Professor
The Student and Teacher classes inherit from the Person class.

The Student class will include a public Study() method that will write I'm
studying on the screen.

The Person class must have two public methods Greet() and SetAge(int age) that will
assign the age of the person.

The Teacher class will include a public Explain() method that will write I'm
explaining on the screen.

Also create a public method ShowAge() in the Student class that writes My age is: x
years old on the screen.

You must create another test class called StudentProfessorTest with a Main method
to do the following:

Page 1 sur 4
M.Sahim Med Said

• Create a new Person and make him say hello


• Create a new Student, set an age, say hello, and display her age on the
screen.
• Create a new Teacher, set an age, say hello and start the explanation.

Photo book class


Create a C# program to manage a photo book using object-oriented programming.

To start, create a class called PhotoBook with a private attribute numPages of


type int . It must also have a public method GetNumberPages that will return the
number of photo book pages.

The default constructor will create an album with 16 pages. There will be an
additional constructor, with which we can specify the number of pages we want in
the album.

There is also a BigPhotoBook class whose constructor will create an album with 64
pages.

Finally create a PhotoBookTest class to perform the following actions:

• Create a default photo book and show the number of pages


• Create a photo book with 24 pages and show the number of pages
• Create a large photo book and show the number of pages

Inheritance of objects
Create a C# program that prompts the user for three names of people and stores
them in an array of Person-type objects. There will be two people of the Student
type and one person of the Teacher type.

To do this, create a Person class that has a Name property of type string ,a
constructor that receives the name as a parameter and overrides the ToString ()
method.

Then create two more classes that inherit from the Person class, they will be called
Student and Teacher. The Student class has a Study method that writes by console
that the student is studying. The Teacher class will have an Explain method that

Page 2 sur 4
M.Sahim Med Said

writes to the console that the teacher is explaining. Remember to also create two
constructors on the child classes that call the parent constructor of the Person class.

End the program by reading the people (the teacher and the students) and execute
the Explain and Study methods.

Shapes - Class diagram


Create a C# program that represents the following UML class diagram. The
diagram represents public, private, and protected attributes as well as class
dependency and inheritance.

Interfaces
Create a C# program that implements an IVehiculo interface with two methods,
one for Drive of type void and another for Refuel of type bool that has a parameter
of type integer with the amount of gasoline to refuel. Then create a Car class with a
builder that receives a parameter with the car's starting gasoline amount and
implements the Drive and Refuel methods of the car.

Page 3 sur 4
M.Sahim Med Said

The Drive method will print on the screen that the car is Driving, if the gasoline is
greater than 0. The Refuel method will increase the gasoline of the car and return
true.

To carry out the tests, create an object of type Car with 0 of gasoline in the Main of
the program and ask the user for an amount of gasoline to refuel, finally execute
the Drive method of the car.

Abstract classes
Create a C# program that implements an abstract class Animal that has a Name
property of type text and three methods SetName (string name), GetName and Eat.
The Eat method will be an abstract method of type void.

You will also need to create a Dog class that implements the above Animal class
and the Eat method that says the dog is Eating.

To test the program ask the user for a dog name and create a new Dog type object
from the Main of the program, give the Dog object a name, and then execute the
GetName and Eat methods.

Page 4 sur 4

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