Answers to Self-Review Exercises

  1. 7.1 a) method call. b) local variable. c) return. d) void. e) top. f) last-in, first-out (LIFO). g) return; or return expression; or encountering the closing right brace of a method. h) Random. i) activation record, stack frame. j) stack overflow. k) scope. l) overloading. m) method call. n) recursive. o) base. p) =>. q) value, reference.

  2. 7.2 a) class body. b) block that defines method RollDice’s body. c) class body. d) class body. e) block that defines method Main’s body.

  3. 7.3 Figure 7.21 demonstrates the Math class methods in Fig. 7.2:

    Fig. 7.21 Solution to Exercise 7.3.

    Alternate View
    
      1   // Exercise 7.3 Solution: MathTest.cs
      2   // Testing the Math class methods.
      3   using System;
      4
      5   class MathTest
      6   {
      7      static void Main()
      8      {
      9         Console.WriteLine($"Math.Abs(23.7) = {Math.Abs(23.7)}");
     10         Console.WriteLine($"Math.Abs(0.0) = {Math.Abs(0.0)}");
     11         Console.WriteLine($"Math.Abs(-23.7) = {Math.Abs(-23.7)}");
     12         Console.WriteLine($"Math.Ceiling(9.2) = {Math.Ceiling(9.2)}");
     13         Console.WriteLine($"Math.Ceiling(-9.8) = {Math.Ceiling(-9.8)}");
     14         Console.WriteLine($"Math.Cos(0.0) = {Math.Cos(0.0)}");
     15         Console.WriteLine($"Math.Exp(1.0) = {Math.Exp(1.0)}");
     16         Console.WriteLine($"Math.Exp(2.0) = {Math.Exp(2.0)}");
     17         Console.WriteLine($"Math.Floor(9.2) = {Math.Floor(9.2)}");
     18         Console.WriteLine($"Math.Floor(-9.8) = {Math.Floor(-9.8)}");
     19         Console.WriteLine($"Math.Log(Math.E) = {Math.Log(Math.E)}");
     20         Console.WriteLine($"Math.Log(Math.E * Math.E) = {Math.Log(Math.E * Math.E)}");
     21         Console.WriteLine($"Math.Max(2.3, 12.7) = {Math.Max(2.3, 12.7)}");
     22         Console.WriteLine($"Math.Max(-2.3, -12.7) = {Math.Max(-2.3, -12.7)}");
     23         Console.WriteLine($"Math.Min(2.3, 12.7) = {Math.Min(2.3, 12.7)}");
     24 Console.WriteLine($"Math.Min(-2.3, -12.7) = {Math.Min(-2.3, -12.7)}");
     25         Console.WriteLine($"Math.Pow(2.0, 7.0) = {Math.Pow(2.0, 7.0)}");
     26         Console.WriteLine($"Math.Pow(9.0, 0.5) = {Math.Pow(9.0, 0.5)}");
     27         Console.WriteLine($"Math.Sin(0.0) = {Math.Sin(0.0)}");
     28         Console.WriteLine($"Math.Sqrt(900.0) = {Math.Sqrt(900.0)}");
     29         Console.WriteLine($"Math.Tan(0.0) = {Math.Tan(0.0)}");
     30      }
     31   }
    
    
    Math.Abs(23.7) = 23.7
    Math.Abs(0.0) = 0
    Math.Abs(-23.7) = 23.7
    Math.Ceiling(9.2) = 10
    Math.Ceiling(-9.8) = -9
    Math.Cos(0.0) = 1
    Math.Exp(1.0) = 2.71828182845905
    Math.Exp(2.0) = 7.38905609893065
    Math.Floor(9.2) = 9
    Math.Floor(-9.8) = -10
    Math.Log(Math.E) = 1
    Math.Log(Math.E * Math.E) = 2
    Math.Max(2.3, 12.7) = 12.7
    Math.Max(-2.3, -12.7) = -2.3
    Math.Min(2.3, 12.7) = 2.3
    Math.Min(-2.3, -12.7) = -12.7
    Math.Pow(2.0, 7.0) = 128
    Math.Pow(9.0, 0.5) = 3
    Math.Sin(0.0) = 0
    Math.Sqrt(900.0) = 30
    Math.Tan(0.0) = 0
    
  4. 7.4 The answers to Self-Review Exercise 7.4 are as follows:

    1. double Hypotenuse(double side1, double side2)

    2. int Smallest(int x, int y, int z)

    3. void Instructions()

    4. double IntToDouble(int number)

  5. 7.5 The answers to Self-Review Exercise 7.5 are as follows:

    1. Error: Method H is declared within method G.

      Correction: Move the declaration of H outside the declaration of G.

    2. Error: The method is supposed to return an integer, but does not.

      Correction: Delete the method’s body statements and place the statement

      
      return x + y;
      

      in the method, or add the following statement at the end of the method body:

      
      return result;
      
    3. Error: The semicolon after the right parenthesis of the parameter list is incorrect, and the parameter a should not be redeclared in the method.

      Correction: Delete the semicolon after the right parenthesis of the parameter list, and delete the declaration float a;.

    4. Error: The method returns a value when it’s not supposed to.

      Correction: Change the return type from void to int.

  6. 7.6 Figure 7.22 contains the solution to Exercise 7.6.

    Fig. 7.22 Solution to Exercise 7.6.

    Alternate View
    
      1   // Exercise 7.6 Solution: Sphere.cs
      2   // Calculate the volume of a sphere.
      3   using System;
      4
      5   class Sphere
      6   {
      7      // obtain radius from user and display volume of sphere
      8      static void Main()
      9      {
     10      Console.Write("Enter radius of sphere: ");
     11      double radius = double.Parse(Console.ReadLine());
     12      Console.WriteLine("Volume is {SphereVolume(radius):F3}");
     13    }
     14
     15    // calculate and return sphere volume
     16    static double SphereVolume(double radius) =>
     17       (4.0 / 3.0) * Math.PI * Math.Pow(radius, 3);
     18   }
    
    
    Enter radius of sphere: 4
    Volume is 268.083
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset