Archive for July, 2007

212 Methods Chapter (Web server address) 6 (lines 85 93) sets the

Tuesday, July 31st, 2007

212 Methods Chapter 6 (lines 85 93) sets the player s point, displays the dice in pointGroupBox, enables roll- Button and disables playButton. Notice that for many of the cases, we cast the enumeration values to type int. Although each enumeration value is assigned an integer value on lines 26 32, each value is considered to be of enum type DiceNames, and therefore must be cast to int for use in the switch structure, which requires constant integral expressions. The rollButton_Click event handler s task is to roll the dice and determine if the player won by making the point value or lost by rolling 7. Line 40 calls method roll- Dice. Lines 42 54 in method rollButton_Click analyze the roll. Depending on the value of the roll, the buttons rollButton and playButton will become either disabled or enabled. This is done by setting the Enabled property to trueor false. 6.12 Duration of Variables The attributes of variables include name, type, size and value. Each variable in a program has additional attributes, including duration and scope. A variable s duration (also called its lifetime) is the period during which the variable exists in memory. Some variables exist briefly, some are created and destroyed repeatedly and others exist for the entire execution of a program. A variable s scope is where the variable s identifier (i.e., name) can be referenced in a program. Some variables can be referenced throughout a program, while others can be referenced from limited portions of a program. This section discusses the duration of variables. Section 6.13 discusses the scope of identifiers. Local variables in a method (i.e., parameters and variables declared in the method body) have automatic duration. Automatic duration variables are created when program control reaches their declaration; that is, they exist while the block in which they are declared is active, and they are destroyed when that block is exited. For the remainder of the text, we refer to variables of automatic duration as automatic variables, or local variables. The instance variables of a class are initialized by the compiler if the programmer does not provide initial values. Variables of most primitive data types are initialized to zero, bool variables are initialized to false and references are initialized to null. Unlike instance variables of a class, automatic variables must be initialized by the programmer before they can be used. Common Programming Error 6.13 Automatic variables must be initialized before their values are used in a method; otherwise, the compiler issues an error message. Variables of static duration exist from the time at which the class that defines them is loaded into memory. These variables then last until the program terminates. Their storage is allocated and initialized when their classes are loaded into memory. Static-duration variable names exist when their classes are loaded into memory, but this does not mean that these identifiers necessarily can be used throughout the program their scopes may be limited as we will see in the next section. 6.13 Scope Rules The scope (sometimes called declaration space) of an identifier for a variable, reference or method is the portion of the program in which the identifier can be accessed. A local vari
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Web hosting asp - Chapter 6 Methods 211 Fig. 6.12 Program to

Tuesday, July 31st, 2007

Chapter 6 Methods 211 Fig. 6.12 Program to simulate the game of craps. (Part 4 of 4.) Before its method definitions, the program includes several declarations, including an enumeration on lines 26 32. An enumeration is a value type that contains a set of constant values and is created using the keyword enum. This enumeration is a convenient way of referring to constant values used throughout the program. We have used the identifiers SNAKE_EYES, TREY, CRAPS, YO_LEVEN and BOX_CARS, to represent significant values in craps. Using these identifiers makes the program more readable. Additionally, if we need to change one of these values, we can modify the enumeration instead of changing the values where they are used throughout the program. This example introduces a few new GUI components. The first, called a GroupBox, displays the user s point. A GroupBoxis a container for other components and helps group components logically. Within the GroupBox, we add two PictureBoxes components that display images. These are added, as with other components, by clicking PictureBox in the ToolBox and dragging this component within the borders of the GroupBox. The playButton_Click event handler begins the game. Line 68 invokes method rollDice (defined on lines 108 124), which rolls the dice, displays the dice and returns their sum. Lines 70 95 use a switch structure to determine whether the player won, lost or established a point value. If the player won by rolling a 7 or 11, line 74 disables roll- Button to prevent the player from rolling the dice again. Line 75 displays a message to indicate that the user won. If the player lost by rolling SNAKE_EYES, TREYor BOX_CARS (i.e., 2, 3 or 12), line 82 displays a message to indicate that the user lost. Otherwise, the default case
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

210 Methods Chapter 6 99 // display an

Monday, July 30th, 2007

210 Methods Chapter 6 99 // display an image for the specified face 100 private void displayDie( PictureBox dieImage, int face ) 101 { 102 dieImage.Image = Image.FromFile( 103 Directory.GetCurrentDirectory() + 104 “\images\die” + face + “.gif” ); 105 } 106 107 // simulates rolling two dice 108 private int rollDice() 109 { 110 int die1, die2, dieSum; 111 Random randomNumber = new Random(); 112 113 die1 = randomNumber.Next( 1, 7 ); 114 die2 = randomNumber.Next( 1, 7 ); 115 116 displayDie( firstDieImage, die1 ); 117 displayDie( secondDieImage, die2 ); 118 119 myDie1 = die1; 120 myDie2 = die2; 121 dieSum = die1 + die2; 122 return dieSum; 123 124 } // end method rollDice 125 126 // main entry point for the application 127 [STAThread] 128 static void Main() 129 { 130 Application.Run( new CrapsGame() ); 131 } 132 133 } // end of class CrapsGame Fig. 6.12 Program to simulate the game of craps. (Part 3 of 4.)
We recommend high quality webhost to host and run your jsp application: christian web host services.

Web server extensions - Chapter 6 Methods 209 48 else 49 if(

Monday, July 30th, 2007

Chapter 6 Methods 209 48 else 49 if( sum == 7 ) 50 { 51 statusLabel.Text = “Sorry. You lose.”; 52 rollButton.Enabled = false; 53 playButton.Enabled = true; 54 } 55 56 } // end rollButton_Click 57 58 // simulate first roll and result of that roll 59 protected void playButton_Click( 60 object sender, System.EventArgs e ) 61 { 62 pointGroupBox.Text = “Point”; 63 statusLabel.Text = “”; 64 pointFirstDieImage.Image = null; 65 pointSecondDieImage.Image = null; 66 67 myPoint = 0; 68 int sum = rollDice(); 69 70 switch ( sum ) 71 { 72 case 7: 73 case ( int ) DiceNames.YO_LEVEN: 74 rollButton.Enabled = false; // disable Roll button 75 statusLabel.Text = “You Win!!!”; 76 break; 77 78 case ( int ) DiceNames.SNAKE_EYES: 79 case ( int ) DiceNames.TREY: 80 case ( int ) DiceNames.BOX_CARS: 81 rollButton.Enabled = false; 82 statusLabel.Text = “Sorry. You lose.”; 83 break; 84 85 default: 86 myPoint = sum; 87 pointGroupBox.Text = “Point is ” + sum; 88 statusLabel.Text = “Roll Again”; 89 displayDie( pointFirstDieImage, myDie1 ); 90 displayDie( pointSecondDieImage, myDie2 ); 91 playButton.Enabled = false; 92 rollButton.Enabled = true; 93 break; 94 95 } // end switch 96 97 } // end playButton_Click 98 Fig. 6.12 Program to simulate the game of craps. (Part 2 of 4.)
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

208 Methods Chapter 6 Notice that the player

Sunday, July 29th, 2007

208 Methods Chapter 6 Notice that the player rolls two dice on each roll. When executing the application, clicking the Play button begins the game and makes the first roll. The form displays the results of each roll. The screen captures show the execution of several games. 1 // Fig. 6.12: CrapsGame.cs 2 // Simulating the game of Craps. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 using System.IO; 10 11 public class CrapsGame : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Button rollButton; 14 private System.Windows.Forms.Button playButton; 15 16 int myPoint; // player’s point value 17 private System.Windows.Forms.PictureBox pointFirstDieImage; 18 private System.Windows.Forms.Label statusLabel; 19 private System.Windows.Forms.PictureBox firstDieImage; 20 private System.Windows.Forms.PictureBox pointSecondDieImage; 21 private System.Windows.Forms.PictureBox secondDieImage; 22 private System.Windows.Forms.GroupBox pointGroupBox; 23 int myDie1; // value of first die 24 int myDie2; // value of second die 25 26 public enum DiceNames 27 { 28 SNAKE_EYES = 2, 29 TREY = 3, 30 YO_LEVEN = 11, 31 BOX_CARS = 12, 32 } 33 34 // Visual Studio .NET generated code 35 36 // simulate next roll and result of that roll 37 protected void rollButton_Click( 38 object sender, System.EventArgs e ) 39 { 40 int sum = rollDice(); 41 42 if ( sum == myPoint ) 43 { 44 statusLabel.Text = “You Win!!!”; 45 rollButton.Enabled = false; 46 playButton.Enabled = true; 47 } Fig. 6.12 Program to simulate the game of craps. (Part 1 of 4.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Chapter 6 (How to cite a web site) Methods 207 106 107 case 6:

Sunday, July 29th, 2007

Chapter 6 Methods 207 106 107 case 6: 108 sixes++; 109 break; 110 111 } // end switch 112 113 } // end DisplayDie 114 115 // main entry point for the application 116 [STAThread] 117 static void Main() 118 { 119 Application.Run( new RollDie2() ); 120 } 121 122 } // end of class RollDie2 Fig. 6.11 Simulating rolling 12 six-sided dice. (Part 3 of 3.) As the program output demonstrates, over a large number of die rolls, each of the possible faces from 1 through 6 appears with approximately equal likelihood (i.e., about one- sixth of the time). After studying arrays in Chapter 7, Arrays, we will show how to replace the entire switch structure in this program with a single-line statement. 6.11 Example: Game of Chance One of the most popular games of chance is a dice game known as craps, played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. Each face contains 1, 2, 3, 4, 5 or 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3 or 12 on the first throw (called craps ), the player loses (i.e., the house wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes the player s point. To win, players must continue rolling the dice until they make their point (i.e., roll their point value). The player loses by rolling a 7 before making the point. Figure 6.12 simulates the game of craps with a simple graphical user interface.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

206 Methods Chapter 6 53 DisplayDie( (Domain and web hosting) dieLabel11 );

Sunday, July 29th, 2007

206 Methods Chapter 6 53 DisplayDie( dieLabel11 ); 54 DisplayDie( dieLabel12 ); 55 56 double total = ones + twos + threes + fours + fives + sixes; 57 58 // display the current frequency values 59 displayTextBox.Text = “FacettFrequencytPercentn1tt” + 60 ones + “tt” + 61 String.Format( “{0:F2}”, ones / total * 100 ) + 62 “%n2tt” + twos + “tt” + 63 String.Format( “{0:F2}”, twos / total * 100 ) + 64 “%n3tt” + threes + “tt” + 65 String.Format( “{0:F2}”, threes / total * 100 ) + 66 “%n4tt” + fours + “tt” + 67 String.Format( “{0:F2}”, fours / total * 100 ) + 68 “%n5tt” + fives + “tt” + 69 String.Format( “{0:F2}”, fives / total * 100 ) + 70 “%n6tt” + sixes + “tt” + 71 String.Format( “{0:F2}”, sixes / total * 100 ) + “%”; 72 73 } // end rollButton_Click 74 75 // display the current die, and modify frequency values 76 public void DisplayDie( Label dieLabel ) 77 { 78 int face = randomNumber.Next( 1, 7 ); 79 80 dieLabel.Image = Image.FromFile( 81 Directory.GetCurrentDirectory() + 82 “\images\die” + face + “.gif” ); 83 84 // add one to frequency of current face 85 switch ( face ) 86 { 87 case 1: 88 ones++; 89 break; 90 91 case 2: 92 twos++; 93 break; 94 95 case 3: 96 threes++; 97 break; 98 99 case 4: 100 fours++; 101 break; 102 103 case 5: 104 fives++; 105 break; Fig. 6.11 Simulating rolling 12 six-sided dice. (Part 2 of 3.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web servers - Chapter 6 Methods 205 // Fig. 6.11: RollDie2.cs

Saturday, July 28th, 2007

Chapter 6 Methods 205 // Fig. 6.11: RollDie2.cs // Rolling 12 dice with frequency chart. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; public class RollDie2 : System.Windows.Forms.Form { private System.Windows.Forms.Button rollButton; private System.Windows.Forms.RichTextBox displayTextBox; private System.Windows.Forms.Label dieLabel1; private System.Windows.Forms.Label dieLabel2; private System.Windows.Forms.Label dieLabel3; private System.Windows.Forms.Label dieLabel4; private System.Windows.Forms.Label dieLabel5; private System.Windows.Forms.Label dieLabel6; private System.Windows.Forms.Label dieLabel7; private System.Windows.Forms.Label dieLabel8; private System.Windows.Forms.Label dieLabel9; private System.Windows.Forms.Label dieLabel10; private System.Windows.Forms.Label dieLabel11; private System.Windows.Forms.Label dieLabel12; private Random randomNumber = new Random(); private int ones, twos, threes, fours, fives, sixes; // Visual Studio .NET generated code // simulates roll by calling DisplayDie for // each label and displaying the results protected void rollButton_Click( object sender, System.EventArgs e ) { // pass the labels to a method that will // randomly assign a face to each die DisplayDie( dieLabel1 ); DisplayDie( dieLabel2 ); DisplayDie( dieLabel3 ); DisplayDie( dieLabel4 ); DisplayDie( dieLabel5 ); DisplayDie( dieLabel6 ); DisplayDie( dieLabel7 ); DisplayDie( dieLabel8 ); DisplayDie( dieLabel9 ); DisplayDie( dieLabel10 ); Fig. 6.11 Simulating rolling 12 six-sided dice. (Part 1 of 3.)
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Free web hosting with ftp - 204 Methods Chapter 6 48 49 // main

Friday, July 27th, 2007

204 Methods Chapter 6 48 49 // main entry point for application 50 [STAThread] 51 static void Main() 52 { 53 Application.Run( new RollDie() ); 54 } 55 56 } // end class RollDie Fig. 6.10 Rolling dice in a Windows application (Part 2 of 2.). Method DisplayDie (lines 39 47) invokes Random method Next to simulate a roll of a die (line 41) and loads an image that corresponds to the value rolled (lines 44 46). Line 44 uses class Label s Imageproperty (introduced in Chapter 2) to display the die. Notice that we specify which image will be displayed by invoking method FromFile of class Image, which specifies the location of the file on disk that contains the image. Each click of the button displays four images that represent the four new values of the dice. Note that the user must click rollButton at least once to display the dice. Directory method GetCurrentDirectory (line 45) returns the path of the folder in which the program is executing. If you run the program from Visual Studio .NET, this will be the binDebug directory in the project s directory. The die images must be in this folder for the example to operate properly. These images are placed in the proper folders on the CD that accompanies this book. To show that class Random produces numbers with approximately equal likelihood, let us modify the program in Fig. 6.10 to keep some simple statistics. The Windows application of Fig. 6.11 provides a Roll button for rolling the dice and a TextBox that displays the frequencies for each value rolled. The program output shows the results of clicking Roll 10 times. When the user clicks the Roll button, the program invokes the rollButton_Click event handler on lines 38 73. This event handler invokes method DisplayDiefor each of the 12 dice that the program simulates (lines 43 54). Lines 56 71 then calculate the frequencies for each die and displays the results by appending the information to displayTextBox s Text property. Method displayDie(lines 76 113) simulates a die roll (line 78), loads the appropriate Imageand increments the frequency count for the rolled value.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Photo web hosting - Chapter 6 Methods 203 The Windows application of

Friday, July 27th, 2007

Chapter 6 Methods 203 The Windows application of Fig. 6.10 simulates rolls of four dice. The program enables the user to click a button that rolls four dice at a time and displays an image of each die in the window. The next example (Fig. 6.11) uses many of this example s features to demonstrate that the numbers generated by Next occur with approximately equal likelihood. 1 // Fig. 6.10: RollDie.cs 2 // Using random number generation to simulate dice rolling. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 using System.IO; // enables reading data from files 10 11 public class RollDie : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Button rollButton; 14 15 private System.Windows.Forms.Label dieLabel2; 16 private System.Windows.Forms.Label dieLabel1; 17 private System.Windows.Forms.Label dieLabel3; 18 private System.Windows.Forms.Label dieLabel4; 19 20 private Random randomNumber = new Random(); 21 22 // Visual Studio .NET generated code 23 24 // method called when rollButton clicked, 25 // passes labels to another method 26 protected void rollButton_Click( 27 object sender, System.EventArgs e ) 28 { 29 // pass the labels to a method that will 30 // randomly assign a face to each die 31 DisplayDie( dieLabel1 ); 32 DisplayDie( dieLabel2 ); 33 DisplayDie( dieLabel3 ); 34 DisplayDie( dieLabel4 ); 35 36 } // end rollButton_Click 37 38 // determines image to be displayed by current die 39 public void DisplayDie( Label dieLabel ) 40 { 41 int face = randomNumber.Next( 1, 7 ); 42 43 // displays image specified by filename 44 dieLabel.Image = Image.FromFile( 45 Directory.GetCurrentDirectory() + 46 “\images\die” + face + “.gif” ); 47 } Fig. 6.10 Rolling dice in a Windows application (Part 1 of 2.).
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.