Answers to Chapter 4 Review Questions

   


Chapter 4

1:

What are namespaces used for in C# and .NET?

A:

Namespaces act as containers for classes. They help us organize the classes of our programs and to provide easy access to them (when reused) by outside parties.

2:

What is the advantage of including the keyword using followed by the name of a namespace in the beginning of a program?

A:

It allows us to write short names instead of long, fully-qualified names when we refer to classes in a namespace. This makes it easier to type the names and improves the readability of the source code.

3:

Which namespace contains classes related to mathematical calculations and console input/output.

A:

The System namespace.

4:

How should comments be applied in the source code?

A:

Like salt in your food, excessive amounts of comments are as damaging as too few comments.

5:

Describe a variable of type int.

A:

A variable of type int occupies 32 bits of memory and can represent whole numbers between 2147483648 and 2147483647.

6:

Why are x and y often regarded as unacceptable variable identifiers? Why are they acceptable in Listing 4.1?

A:

The identifiers x and y do not convey any information about what kind of value they represent, as opposed to names like accountBalance and height. However, x and y in Listing 4.1 represent a variety of values that cannot be pinpointed to a specific name, so the generic names x and y are acceptable.

7:

What are the fundamental parts of a method?

A:

A method header and its method body that is enclosed by curly braces.

8:

Why is MoveLeft a bad name for a class? For which C# construct would it be better suited?

A:

Classes generally represent items (tangible or conceptual) so nouns constitute better names for classes than verbs. Verbs, such as MoveLeft, are better suited to methods that take actions.

9:

How do you specify that a method does not return a value?

A:

By writing the keyword void in front of the method identifier in the method header.

10:

How do you specify that a method returns a value of type int?

A:

By writing the keyword int in front of the method identifier in the method header.

11:

What are arguments (in method calls)?

A:

The value of a method argument is passed along to the called method.

12:

What are formal parameters?

A:

A formal parameter is part of the method header and has a certain value as the execution of the method commences.

13:

How do arguments and formal parameters relate?

A:

Each argument of a method call is assigned to its corresponding formal parameter during a method call.

14:

Does the + operator only perform arithmetic additions?

A:

It has the potential (through operator overloading) to perform many different operations, depending on the types involved. We have seen examples of arithmetic addition and string concatenation.

15:

How can you break down the inner complexities of a class?

A:

By separating a few complex actions into several simpler actions (methods).

16:

What is a cohesive method?

A:

A method that accomplishes one clear task.

Answers to Chapter 4 Programming Exercises

1:

Change the multi-line comments in lines 3 6 to two single line comments.

2:

Apart from addition and multiplication, allow the user to perform a subtraction. Among other changes, you need to add a Subtract method. (The symbol is used to perform subtractions in C#).

A:

The following program contains answers to Exercise 1 and 2. The parts related to Exercise 1 and Exercise 2 are marked Ex.1 and Ex.2 respectively.

 using System; //Ex. 1  //This class finds the sum, product, difference,  //min and max of two numbers public class SimpleCalculator {     public static void Main()     {         int x;         int y;         Console.Write("Enter first number: ");         x = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter second number: ");         y = Convert.ToInt32(Console.ReadLine());         Console.WriteLine("The sum is: " + Sum(x, y));         Console.WriteLine("The product is: " + Product(x, y));         Console.WriteLine("The maximum number is: " + Math.Max(x, y));         Console.WriteLine("The minimum number is: " + Math.Min(x, y));         Console.WriteLine("The difference is: " + Subtract(x, y)); //Ex. 2     }      // Sum calculates the sum of two int's     public static int Sum(int a, int b)     {         int sumTotal;         sumTotal = a + b;         return sumTotal;     }      // Product calculates the product of two int's     public static int Product(int a, int b)     {         int productTotal;         productTotal = a * b;         return productTotal;     }      // Ex.2      // Subtract calculates the difference between two int's     public static int Subtract(int a, int b)     {         int difference;         difference = a - b;         return difference;     } } 
3:

Instead of calculating the sum and the product of two numbers, make the program perform the calculations on three numbers. (You can ignore the Max and Min functions here.) Hint: You need to declare another int variable in Main. The Sum, Product, and Subtract methods must accept three arguments instead of two. You must allow the user to input a third number, and you must include the third argument when these methods are called.

4:

Create two methods called MyMax and MyMin that both take three arguments and find the maximum and minimum value of these arguments. Hint: Math.Max(Math.Max(a, b),c) returns the max of a, b and c.

A:

The following program constitutes answers to Exercises 3 and 4. It allows the calculations to be performed on three values and implements the MyMax and MyMin methods:

 using System; //This class finds the sum, product, difference, //min and max of three numbers public class SimpleCalculator {     public static void Main()     {         int x;         int y;         int z;      // Ex.3         Console.Write("Enter first number: ");         x = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter second number: ");         y = Convert.ToInt32(Console.ReadLine());          // Ex. 3         Console.Write("Enter third number: ");         z = Convert.ToInt32(Console.ReadLine());         Console.WriteLine("The sum is: " + Sum(x, y, z));         Console.WriteLine("The product is: " + Product(x, y, z));          // Ex. 4         Console.WriteLine("The maximum number is: " + MyMax(x, y, z));         Console.WriteLine("The minimum number is: " + MyMin(x, y, z));          // Ex.3         Console.WriteLine("The difference is: " + Subtract(x, y, z));     }      // Ex. 3      // Sum calculates the sum of two int's     public static int Sum(int a, int b, int c)     {         int sumTotal;         sumTotal = a + b + c;         return sumTotal;     }      // Ex. 3      // Product calculates the product of two int's     public static int Product(int a, int b, int c)     {         int productTotal;         productTotal = a * b * c;         return productTotal;     }      // Ex. 3      // Subtract calculates the difference between two int's     public static int Subtract(int a, int b, int c)     {         int difference;         difference = a - b - c;         return difference;     }      //Ex. 4      // Find max value of three parameters     public static int MyMax(int a, int b, int c)     {         int max;         max = Math.Max(a, Math.Max(b, c));         return max;     }      // Ex. 4      // Find min value of three parameters     public static int MyMin(int a, int b, int c)     {         int min;         min = Math.Min(a, Math.Min(b, c));         return min;     } } 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net