Telerik School Academy Meeting #1 – November 2010 – Intro to C# Homework Assignments Svetlin Nakov Telerik Corporation www.telerik.com Prepare IT Test Questions Prepare IT Test Questions Prepare at least 20 questions for preparation for the National Olympiad's IT test Prepare at least one question for each category from the official conspectus Categories are officially published at http://edusoft.fmi.unisofia.bg/documents/Conspect0910.pdf Follow strictly the IT test template: IT-TestQuestions-Template.pptx 3 .NET Framework Overview .NET Framework 1. Install at home .NET Framework 4 and Microsoft Visual Studio 2010 (or VS Express) Write a "Hello World" application in C# 2. Install at home the .NET Reflector Play with it by decompiling the System.Console class 5 C# Language Overview C# Language Overview 1. Write a C# program that prints all numbers 1…100. 2. Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7. 3. Write a program that reads from the console a sequence of N integer numbers and returns the minimal and maximal of them. 4. Write a program that calculates N!/K! for given N and K (1<N<K). 5. Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, … 7 C# Language Overview (2) 6. Write a program that finds the maximal sequence of equal elements in an array. Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1} {2, 2, 2}. 7. 9. 10. Write a program that finds the maximal increasing sequence of elements in an array. Example: {3, 2, 3, 4, 2, 2, 4} {2, 3, 4}. Write a program that finds the most frequent number in an array. Example: {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} 4 (5 times) Write a program that finds all prime numbers in the range [1...10 000 000]. Use the sieve of Eratosthenes algorithm (find it in Wikipedia). 8 C# Language Overview (3) 11. Write a program that prints to the console which day of the week is today. Use System.DateTime. 12. Write methods that calculate the surface of a triangle by given: side and an altitude to it; three sides; two sides and an angle between them. Use System.Math. 13. Write a program that enters file name along with its full file path (e.g. C:\WINDOWS\win.ini), reads its contents and prints it on the console. Find in MSDN how to use System.IO.File.ReadAllText(…). Be sure to catch all possible exceptions and print userfriendly error messages. 9 C# Language Overview (4) 14. Write a program that downloads a file from Internet (e.g. http://www.devbg.org/img/Logo-BASD.jpg) and stores it the current directory. Find in Google how to download files in C#. Be sure to catch all exceptions and to free any used resources. 15. Write a program that reads a string, reverses it and prints it on the console. Example: "sample" "elpmas". 16. Write a program that reads from a text file a list of strings, sorts them and prints them on the console in alphabetical order. 10 C# Language Overview (5) 17. Write a program that finds how many times a substring is contained in a given text (perform case insensitive search). Example: The target substring is "in". The text is as follows: We are living in an yellow submarine. We don't have anything else. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. The result is: 9. 11 C# Language Overview (6) 18. Write a program that parses an URL address given in the format: [protocol]://[server]/[resource] and extracts from it the [protocol], [server] and [resource] elements. For example from the URL http://www.devbg.org/forum/index.php the following information should be extracted: [protocol] = "http" [server] = "www.devbg.org" [resource] = "/forum/index.php" 12 C# Language Overview (7) 19. Write a program that finds in given array of integers (all belonging to the range [0..1000]) how many times each of them occurs. Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2} 2 2 times 3 4 times 4 3 times 20. Write a program that reads a text file and finds all words used in it and how many times each word occurs. Assume that words differing in character casing only are the same. 13 Object-Oriented Programming in C# OOP in C# 1. Define a class Human with first name and last name. Define new class Student which is derived from Human and has new field – grade. Define class Worker derived from Human with new field weekSalary and work-hours per day and method MoneyPerHour() that returns money earned by hour by the worker. Define the proper constructors and properties for this hierarchy. Initialize an array of 10 students and sort them by grade in ascending order. Initialize an array of 10 workers and sort them by money per hour in descending order. 15 OOP in C# (2) 2. Define abstract class Shape with only one virtual method CalculateSurface() and fields width and height. Define two new classes Triangle and Rectangle that implement the virtual method and return the surface of the figure (height*width for rectangle and height*width/2 for triangle). Define class Circle and suitable constructor so that on initialization height must be kept equal to width and implement the CalculateSurface() method. Write a program that tests the behavior of the CalculateSurface() method for different shapes (Circle, Rectangle, Triangle) stored in an array. 16 OOP in C# (3) 3. Create a hierarchy Dog, Frog, Cat, Kitten, Tomcat and define suitable constructors and methods according to the following rules: all of this are Animals. Kittens and tomcats are cats. All animals are described by age, name and sex. Kittens can be only female and tomcats can be only male. Each animal produce a sound. Create arrays of different kinds of animals and calculate the average age of each kind of animal using static methods. Create static method in the animal class that identifies the animal by its sound. 17 Lambda Expressions, Extension Methods and LINQ LINQ 1. Create a class student with properties FirstName, LastName, FN, Tel, Email, Marks (a List<int>), GroupNumber. Create a List<Student> with sample students. Select only the students that are from group number 2. Use LINQ query. Order the students by FirstName. 2. Implement the previous using the same query expressed with extension methods. 3. Extract all students that have email in abv.bg. Use string methods and LINQ. 4. Extract all students having phones in Sofia (staring with 02). Use LINQ and regular expressions. 19 LINQ (2) 5. Select all students that have at least one mark Excellent (6) into a new anonymous class that has properties – FullName and Marks. Use LINQ. 6. Write down a similar program that extracts the students with exactly two marks "2". Use extension methods. 7. Extract all Marks of the students that enrolled in 2006. (The students from 2006 have 06 as their 5-th and 6-th digit in the FN). 8. Create a class Group with properties GroupNumber and DepartmentName. Introduce a property Group in the Student class. Extract all students from "Mathematics" department. Use the Join operator. 20 LINQ (3) 9. Write a program to return the string with maximum length from an array of strings. Use LINQ. 10. Create a program that extracts all students grouped by GroupName and then prints them to the console. Use LINQ. 11. Rewrite the previous using extension methods. 21 Submission Instructions and Deadline Submission Instructions Homework solutions should be submitted at the following Web site: http://nakov.devbg.org/schoolacademy-uploads/ Solutions should be packed in a single ZIP or RAR archive with a following structure IT-Test – directory for the IT test CSharp – directory for the C# solutions OOP – directory for the OOP solutions LINQ – directory for the LINQ solutions 23 Further Instructions The deadline for the homework is: A week before the next training session Everybody is free to use help from friends, teachers or Internet Submission of the same work by different authors may result in a disqualification Ask your questions in the Telerik School Academy official discussion group: http://groups.google.com/group/it-olymp 24 Homework Assignments Questions? http://schoolacademy.telerik.com