Archive for August, 2007

Adelphia web hosting - 7 Arrays Objectives To introduce the array

Wednesday, August 22nd, 2007

7 Arrays Objectives To introduce the array data structure. To understand how arrays store, sort and search lists and tables of values. To understand how to declare an array, initialize an array and refer to individual elements of an array. To be able to pass arrays to methods. To understand basic sorting techniques. To be able to declare and manipulate multiple- subscript arrays. With sobs and tears he sorted out Those of the largest size Lewis Carroll Attempt the end, and never stand to doubt; Nothing s so hard, but search will find it out. Robert Herrick Now go, write it before them in a table, and note it in a book. Isaiah 30:8 Tis in my memory lock d, And you yourself shall keep the key of it. William Shakespeare
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Professional web hosting - Chapter 6 Methods 235 a) Move n

Tuesday, August 21st, 2007

Chapter 6 Methods 235 a) Move n 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the n 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area. The process ends when the last task involves moving n = 1 disk (i.e., the base case). This is accomplished by trivially moving the disk without the need for a temporary holding area. Write a program to solve the Towers of Hanoi problem. Allow the user to enter the number of disks in a TextBox. Use a recursive Tower method with four parameters: a) The number of disks to be moved b) The peg on which these disks are threaded initially c) The peg to which this stack of disks is to be moved d) The peg to be used as a temporary holding area Your program should display in a read-only TextBoxwith scrolling functionality the precise instructions for moving the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should print the following series of moves: 1 . 3 (This means move one disk from peg 1 to peg 3.) 1 . 2 3 . 2 1 . 3 2 . 1 2 . 3 1 . 3 6.16 The greatest common divisor of integers x and y is the largest integer that evenly divides both xand y. Write a recursive method Gcd that returns the greatest common divisor of xand y. The Gcdof x and y is defined recursively as follows: If yis equal to 0, then Gcd(x, y)is x; otherwise, Gcd(x,y)is Gcd(y,x%y), where %is the modulus operator.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

234 Methods Chapter 6 6.13 (Affordable web design) Write an application

Monday, August 20th, 2007

234 Methods Chapter 6 6.13 Write an application that simulates coin tossing. Let the program toss the coin each time the user presses the Toss button. Count the number of times each side of the coin appears. Display the results. The program should call a separate method Flip that takes no arguments and returns false for tails and true for heads. [Note: If the program realistically simulates the coin tossing, each side of the coin should appear approximately half of the time.] 6.14 Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Use the Next method from an object of type Random to produce two positive one-digit integers. It should display a question in the status bar, such as How much is 6 times 7? The student should then type the answer into a TextBox. Your program should check the student s answer. If it is correct, draw the string “Verygood!” in a read-only TextBox, then ask another multiplication question. If the answer is wrong, draw the string “No. Pleasetry again.” in the same read-only TextBox, then let the student try the same question again until the student finally gets it right. A separate method should be used to generate each new question. This method should be called once when the program begins execution and each time the user answers the question correctly. 6.15 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems and the Towers of Hanoi (Fig. 6.20) is one of the most famous. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts. Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would find ourselves hopelessly knotted up in managing the disks. However, if we attack the problem with recursion in mind, it becomes tractable. Moving n disks can be viewed in terms of moving only n 1 disks (and hence, the recursion) as follows: Fig. 6.20 The Towers of Hanoi for the case with four disks.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Chapter 6 Methods 233 e) (Web hosting packages) x = Math.Abs(

Sunday, August 19th, 2007

Chapter 6 Methods 233 e) x = Math.Abs( -6.4 ); f) x = Math.Ceiling( -6.4 ); g) x = Math.Ceiling( -Math.Abs( -8 + Math.Floor( -5.5 ) ) ); 6.8 A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and displays the parking charges for each customer who parked a car in this garage yesterday. You should enter in a TextBox the hours parked for each customer. The program should display the charge for the current customer. The program should use the method CalculateCharges to determine the charge for each customer. Use the techniques described in the chapter to read the double value from a TextBox. 6.9 Write a method IntegerPower( base,exponent ) that returns the value of base exponent For example, IntegerPower(3,4 ) = 3* 3*3 *3. Assume that exponent is a positive, nonzero integer, and base is an integer. Method IntegerPower should use for or while to control the calculation. Do not use any Math library methods. Incorporate this method into a Windows application that reads integer values from TextBoxes for base and exponent from the user and performs the calculation with the IntegerPower method. 6.10 Define a method Hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. The method should take two arguments of type doubleand return the hypotenuse as a double. Incorporate this method into a Windows application that reads integer values for side1 and side2 from TextBoxes and performs the calculation with the Hypotenuse method. Determine the length of the hypotenuse for each of the following triangles: Triangle Side 1 Side 2 1 3.0 4.0 2 5.0 12.0 3 8.0 15.0 6.11 Write a method SquareOfAsterisksthat displays a solid square of asterisks whose side is specified in integer parameter side. For example, if sideis 4, the method displays **** **** **** **** Incorporate this method into an application that reads an integer value for side from the user and performs the drawing with the SquareOfAsterisks method. This method should gather data from Textboxes and should print to a label. 6.12 Modify the method created in Exercise 6.11 to form the square out of whatever character is contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacteris #, this method should print ##### ##### ##### ##### #####
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

232 Methods Chapter 6 Math.Abs( (Web host 4 life) 23.7 ) =

Saturday, August 18th, 2007

232 Methods Chapter 6 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.71828 Math.Exp( 2.0 ) = 7.38906 Math.Floor( 9.2 ) = 9 Math.Floor( -9.8 ) = -10 Math.Log( 2.718282 ) = 1 Math.Log( 7.389056 ) = 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, 7 ) = 128 Math.Pow( 9, .5 ) = 3 Math.Sin( 0.0 ) = 0 Math.Sqrt( 25.0 ) = 5 Math.Tan( 0.0 ) = 0 6.5 a) double hypotenuse( double side1, double side2 ) b) int smallest( int x, int y, int z ) c) void instructions() d) float intToFloat( int number ) 6.6 a) Error: Method h is defined in method g. Correction: Move the definition of h out of the definition of g. b) Error: The method is supposed to return an integer, but does not. Correction: Delete variable result and place the following statement in the method: return x + y; or add the following statement at the end of the method body: return result; c) Error: The result of n + sum(n-1) is not returned by this recursive method, resulting in a syntax error. Correction: Rewrite the statement in the else clause as return n + sum( n - 1 ); d) Error: The semicolon after the right parenthesis that encloses the parameter list, and the redefining of the parameter ain the method definition are both incorrect. Correction: Delete the semicolon after the right parenthesis of the parameter list and delete the declaration float a;. e) Error: The method returns a value when it is not supposed to. Correction: Change the return type to int. EXERCISES 6.7 What is the value of x after each of the following statements is performed? a) x = Math.Abs( 7.5 ); b) x = Math.Floor( 7.5 ); c) x = Math.Abs( 0.0 ); d) x = Math.Ceiling( 0.0 );
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Photography web hosting - Chapter 6 Methods 231 6.3 a) Class scope.

Friday, August 17th, 2007

Chapter 6 Methods 231 6.3 a) Class scope. b) Block scope. c) Class scope. d) Class scope. e) Block scope. 6.4 The following code demonstrates the use of some Math library method calls: 1 // Exercise 6.4: MathTest.cs 2 // Testing the Math class methods 3 using System; 4 5 public class MathTest { 6 public static void Main( string[] args ) 7 { 8 Console.WriteLine( “Math.Abs( 23.7 ) = ” + 9 Math.Abs( 23.7 ) ); 10 Console.WriteLine( “Math.Abs( 0.0 ) = ” + 11 Math.Abs( 0.0 ) ); 12 Console.WriteLine( “Math.Abs( -23.7 ) = ” + 13 Math.Abs( -23.7 ) ); 14 Console.WriteLine( “Math.Ceiling( 9.2 ) = ” + 15 Math.Ceiling( 9.2 ) ); 16 Console.WriteLine( “Math.Ceiling( -9.8 ) = ” + 17 Math.Ceiling( -9.8 ) ); 18 Console.WriteLine( “Math.Cos( 0.0 ) = ” + 19 Math.Cos( 0.0 ) ); 20 Console.WriteLine( “Math.Exp( 1.0 ) = ” + 21 Math.Exp( 1.0 ) ); 22 Console.WriteLine( “Math.Exp( 2.0 ) = ” + 23 Math.Exp( 2.0 ) ); 24 Console.WriteLine( “Math.Floor( 9.2 ) = ” + 25 Math.Floor( 9.2 ) ); 26 Console.WriteLine( “Math.Floor( -9.8 ) = ” + 27 Math.Floor( -9.8 ) ); 28 Console.WriteLine( “Math.Log( 2.718282 ) = ” + 29 Math.Log( 2.718282 ) ); 30 Console.WriteLine( “Math.Log( 7.389056 ) = ” + 31 Math.Log( 7.389056 ) ); 32 Console.WriteLine( “Math.Max( 2.3, 12.7 ) = ” + 33 Math.Max( 2.3, 12.7 ) ); 34 Console.WriteLine( “Math.Max( -2.3, -12.7 ) = ” + 35 Math.Max( -2.3, -12.7 ) ); 36 Console.WriteLine( “Math.Min( 2.3, 12.7 ) = ” + 37 Math.Min( 2.3, 12.7 ) ); 38 Console.WriteLine( “Math.Min( -2.3, -12.7 ) = ” + 39 Math.Min( -2.3, -12.7 ) ); 40 Console.WriteLine( “Math.Pow( 2, 7 ) = ” + 41 Math.Pow( 2, 7 ) ); 42 Console.WriteLine( “Math.Pow( 9, .5 ) = ” + 43 Math.Pow( 9, .5 ) ); 44 Console.WriteLine( “Math.Sin( 0.0 ) = ” + 45 Math.Sin( 0.0 ) ); 46 Console.WriteLine( “Math.Sqrt( 25.0 ) = ” + 47 Math.Sqrt( 25.0 ) ); 48 Console.WriteLine( “Math.Tan( 0.0 ) = ” + 49 Math.Tan( 0.0 ) ); 50 } 51 }
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

230 Methods Chapter 6 6.4 Write an application

Thursday, August 16th, 2007

230 Methods Chapter 6 6.4 Write an application that tests whether the examples of the math library method calls shown in Fig. 6.2 actually produce the indicated results. 6.5 Give the method header for each of the following methods: a) Method hypotenuse, which takes two double-precision, floating-point arguments side1 and side2 and returns a double-precision, floating-point result. b) Method smallest, which takes three integers, x, y, z, and returns an integer. c) Method instructions, which does not take any arguments and does not return a val ue. [Note: Such methods commonly are used to display instructions to a user.] d) Method intToFloat, which takes an integer argument, number, and returns a floating- point result. 6.6 Find the error in each of the following program segments and explain how the error can be corrected: a) int g() { Console.WriteLine( “Inside method g” ); int h() { Console.WriteLine( “Inside method h” ); } } b) int sum(int x, int y ) { int result; result = x + y; } c) int sum(int n ) { if ( n == 0 ) return 0; else n + sum( n - 1 ); } d) void f( float a ); { float a; Console.WriteLine( a ); } e) void product() { int a = 6, b = 5, c = 4, result; result = a * b * c; Console.WriteLine( “Result is ” + result ); return result; } ANSWERS TO SELF-REVIEW EXERCISES 6.1 a) methods, classes. b) method call. c) local variable. d) return. e) void. f) scope. g) return;, return expression;, encountering the closing right brace of a method. h) Random.Next. i) automatic. j) recursive. k) base. l) overloading. m) block. n) repetition. o) selection. p) method. q) modules. r) name. s) base case. t) syntax. u) parameter list. v) return-value-type. w) event. 6.2 a) False. Math method Abs returns the absolute value of a number. b) True. c) True. d) False. Type char can be promoted to int, float, long and double. e) True. f) False. When a method recursively calls itself, it is known as the recursive call or recursion step. g) True. h) True. i) False. Infinite recursion will occur when a recursive method does not converge on the base case. j) False. Recursion repeatedly invokes the mechanism, and consequently, the overhead, of method calls. k) True.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Chapter 6 Methods 229 p) Recursion achieves repetition (Web hosting uk)

Wednesday, August 15th, 2007

Chapter 6 Methods 229 p) Recursion achieves repetition through repeated calls. q) The best way to develop and maintain a large program is to divide it into several smaller program , each of which is more manageable than the original program. r) It is possible to define methods with the same , but different parameter lists. s) Recursion terminates when a is reached. t) Placing a semicolon after the right parenthesis that encloses the parameter list of a meth od definition is a error. u) The is a comma-separated list containing the declarations of the parameters received by the called method. v) The is the data type of the result returned from a called method. w) A is a signal that is sent when some action takes place, such as a button being clicked or a value being changed. 6.2 State whether each of the following is true or false. If false, explain why. a) Mathmethod Absrounds its parameter to the smallest integer. x b) Mathmethod Exp is the exponential method, e. c) Variable type float can be promoted to type double. d) Variable type charcannot be promoted to type int. e) A recursive method is one that calls itself. f) When a method recursively calls itself, it is known as the base case. g) 0! is equal to 1. h) Forgetting to return a value from a recursive method when one is needed results in a syn tax error. i) Infinite recursion occurs when a method converges on the base case. j) A recursive implementation of the Fibonaccimethod is always efficient. k) Any problem that can be solved recursively also can be solved iteratively. 6.3 For the following program, state the scope (either class scope or block scope) of each of the following elements: a) The variable x. b) The variable y. c) The method cube. d) The method paint. e) The variable yPos. public class CubeTest { int x; public void paint() { int yPos = 25; for ( x = 1; x <= 10; x++ ) { Console.WriteLine( x ); yPos += 15; } } public int cube( int y ) { return y * y * y; } }
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

228 Methods Chapter 6 exponential explosion of calls (Geocities web hosting)

Tuesday, August 14th, 2007

228 Methods Chapter 6 exponential explosion of calls pass-by-reference factorial method pass-by-value Fibonacci series defined recursively principle of least privilege golden ratio programmer-defined method hierarchical structure promotions for primitive data types infinite loop Random class infinite recursion recursive evaluation instance variables of a class recursive method invoke a method ref parameter lifetime of an identifier return keyword local variable return-value type method scaling factor method body scope of an identifier method call sequence of random numbers method header shifting value method overloading side effect modularizing a program with methods signature monolithic program simulation named constant software reusability nested block static duration nested control structure termination test out parameter user-defined method overloaded method user interface event parameter list void return-value type SELF-REVIEW EXERCISES 6.1 Fill in the blanks in each of the following statements: a) Program modules in C# are called and . b) A method is invoked with a . c) A variable known only within the method in which it is defined is called a . d) The statement in a called method can be used to pass the value of an expression back to the calling method. e) The keyword is used in a method header to indicate that a method does not return a value. f) The of an identifier is the portion of the program in which the identifier can be used. g) The three ways to return control from a called method to a caller are , and . h) The method is used to produce random numbers. i) Variables declared in a block or in a method s parameter list are of duration. j) A method that calls itself either directly or indirectly is a method. k) A recursive method typically has two components: one that provides a means for the recursion to terminate by testing for a case, and one that expresses the problem as a recursive call for a slightly simpler problem than the original call. l) In C#, it is possible to have various methods with the same name that operate on different types or numbers of arguments. This is called method . m) Local variables declared at the beginning of a method have scope, as do method parameters, which are considered local variables of the method. n) Iteration is based on a control structure. It uses a structure. o) Recursion is based on a control structure. It uses a structure.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Chapter 6 Methods 227 (Php web hosting) A local variable

Monday, August 13th, 2007

Chapter 6 Methods 227 A local variable or reference declared in a block can be used only in that block or in blocks nested within that block. Members of a class have class scope and are visible in what is known as the declaration space of the class. Class scope enables a class s methods to access directly all members defined in that class or inherited into that class. (static members are an exception to this rule.) Any variable declared in the initialization portion of a for structure will be visible only within that for structure. A recursive method is one that calls itself either directly, or indirectly through another method. A recursive method knows how to solve only the simplest case(s), or base case(s). If the method is called with a base case, the method returns a result. If the method is called with a more complex problem, the method divides the problem into two conceptual pieces a piece that the method knows how to solve (base case) and a piece that the method does not know how to solve. To make recursion feasible, the portion of the problem that the method does not know how to solve must resemble the original problem, but be a slightly simpler or smaller version. Certain recursive methods can lead to an exponential explosion of method calls. Both iteration and recursion are based on a control structure. Iteration uses a repetition structure (such as for, whileor do/while); recursion uses a selection structure (such as if, if/else or switch). Both iteration and recursion involve repetition. Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated method calls. Iteration and recursion each involve a termination test. Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized. Both iteration and recursion can execute infinitely. An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case. A recursive approach normally is chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Several methods can have the same name, as long as these methods have different sets of parameters, in terms of number of parameters, types of the parameters and order of the parameters. This is called method overloading. Method overloading commonly is used to create several methods with the same name that perform similar tasks, but on different data types. TERMINOLOGY .(dot operator) coercion of arguments argument to a method call comma-separated list of arguments automatic duration complexity theory automatic initialization of a variable constant variable base case control structures in iteration Buttonclass control structures in recursion calling method divide-and-conquer approach cast operator duration of an identifier class event handling Click event exhausting memory
In case you need quality webspace to host and run your web applications, try our personal web hosting services.