Exercises for OOP in C# .NET lessons 12-13

C# .NET OOP Exercises for OOP in C# .NET lessons 12-13

The following exercises will help you put your knowledge of OOP in C# .NET to the test, based off of what you've learned from previous lessons. Try to solve them on your own, but know that you could always download the working solutions under the article. Nonetheless, beware that when you view the solution without solving the exercise, you won't learn anything :)

If you find yourself stuck at some point, you should always try going back to check previous tutorials first and figure the problem out by yourself.

Easy exercise

Write an application that asks the user to enter words and continues to do so until the user enters "end". The application would then print the unique words, no repeats, that they entered (the output would be in all lower case). Put all of the entered words in a List collection.

Application screen sample:

Console application
Enter a word: John Enter a word: has Enter a word: apple Enter a word: Charles Enter a word: has Enter a word: orange Enter a word: Lucy Enter a word: has Enter a word: apple Enter a word: and Enter a word: orange Enter a word: end You've entered the following words: john, has, apple, charles, orange, lucy, end

Intermediate exercise

Write an application that manages cars in a used car dealership. The car dealership has an address, certain amount of money in a cash register, and cars. Each car has a license plate, a brand (e.g. Porsche) and a price. The car dealership must have a method to add cars. It also would also have to have a method for selling cars. When a car is sold, the car would be removed from the dealership and its price would be added to the cash register. The last necessary method for the car dealership would be one that returns the total price of all the cars. There would also be trucks available, which would have an additional, cargo capacity, attribute.

Make sure to test your application out using realistic data.

The application screen sample:

Console application
There are 5 cars in the car dealership The car dealers have 174000 Eur Total price of the cars is 326000 Eur

Advanced exercise

I'm sure all of you remember the snake game that was popular back in the Nokia phones era. The games is about a snake that moves around on the game screen. Its length is just a few blocks at the beginning, and the food is generated at random positions. If the snake eats the food, touching the block counts as eating in this game, its length is increased by one block. The food would then be generated at a new random location (the location cannot be on the snake's body). The snake's body is made up of several blocks, the head always moves in whichever direction the user last chose and the other body parts follow (the snake never stops). The game ends when the snake exits the game screen or if it bites its own body.

You'll need to learn a few tricks before you actually start making the game. Let's get right into it!

Games usually work in a loop, sometimes called the game-loop, which repeats 5 actions:

  1. Clear the screen
  2. Draw the scene
  3. Move objects
  4. Wait (the game would be too fast otherwise)
  5. React to pressed keys

You can use the following code in your game. Which would most likely be in Program.cs:

Snake snake = new Snake(); // The snake instance
while (snake.IsAlive) // The game loop
{
        Console.BackgroundColor = ConsoleColor.Green; // Green background
        Console.Clear(); // Clear the console
        snake.Draw(); // Draw the snake
        snake.Move(); // Move the snake
        Thread.Sleep(50); // Wait 50 ms
        // If any key is pressed
        if (Console.KeyAvailable)
        {
                ConsoleKeyInfo key = Console.ReadKey(); // Get the key
                // Reaction to each key
                if (key.Key == ConsoleKey.RightArrow)
                        had.Direction = 0;
                if (key.Key == ConsoleKey.LeftArrow)
                        had.Direction = 180;
                if (key.Key == ConsoleKey.DownArrow)
                        had.Direction = 270;
                if (key.Key == ConsoleKey.UpArrow)
                        had.Direction = 90;
        }
}

The code above is documented and should be understandable. To be able to use the Thread class, you would have to add using System.Threading; at the beginning of the file. We cannot use Console.ReadLine() in our application since it would be stuck waiting until we press enter. Which is why we use the KeyAvailable property, which returns whether a key has been pressed. Then, we figure out which key has been pressed using the Console.ReadKey() method and store it into a variable of the ConsoleKeyInfo type.

Tips:

The size of the console is 80x25 characters by default. Since characters are rectangular, it would be best to print each block as 2 rectangles next to each other to make it look like a square. Use the following code to draw a rectangle to the console at a specific location (using X and Y coordinates):

Console.CursorLeft = X * 2; // To use up less horizontal space, we draw each block as 2 characters
Console.CursorTop = Y;
Console.ForegroundColor = Color;
Console.Write("██");

If you want to add color to your application, use the ConsoleColor data type. A ConsoleColor value can be set to several preset colors, e.g. ConsoleColor.Y­ellow.

As for the movement of the snake, the easiest solution is to store the snake as a collection of blocks and add a new head and remove the tail in each step. That way it will look like all of the parts are moving. Then, you could just check whether the snake's head hit anything. If so, the game would be over. Start by drawing the snake and then add movement.

Application screen sample:

Console application
  ██████████████████ ██ ██ ██ ████████ ██

 

Download

Downloaded 13x (128.93 kB)
Application includes source codes in language C#

 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
1 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Thumbnail
All articles in this section
Object-oriented programming in C# .NET
Thumbnail
Next article
Interfaces in C# .NET
Activities (10)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!