Test Your Knowledge: Exercises

Exercise 10-1. Declare a Dog class with two private members: weight (an int) and name (a string). Be sure to add properties to access the members. Then create an array that holds three Dog objects (Milo, 26 pounds; Frisky, 10 pounds; and Laika, 50 pounds). Output each dog’s name and weight.

Exercise 10-2. Create an array of 10 integers. Populate the array by having the user enter integers at the console (use Console.Readline). Don’t worry about error checking for this exercise. Output the integers sorted from greatest to least.

Exercise 10-3. Extend Exercise 10-1 by creating a two-dimensional array that represents a collection of strings that indicate the awards each dog has won at dog shows. Each dog may have a different number of awards won. Output the contents of the array to check its validity.

Exercise 10-4. Create a two-dimensional array that represents a chessboard (an 8 x 8 array). Each element in the array should contain the string “black” or the string “white”, depending on where it is on the board. Create a method that initializes the array with the strings. Then create a method that asks the reader to enter two integers for the coordinates of a square, and returns whether that square is black or white.

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.