Archive for April, 2007

Disney web site - 8 Object-Based Programming Objectives To understand encapsulation

Tuesday, April 24th, 2007

8 Object-Based Programming Objectives To understand encapsulation and data hiding. To understand the notions of data abstraction and abstract data types (ADTs). To create Java ADTs, namely, classes. To be able to create, use and destroy objects. To be able to control access to object instance variables and methods. To appreciate the value of object orientation. To understand the use of the this reference. To understand class variables and class methods. My object all sublime I shall achieve in time. W. S. Gilbert Is it a world to hide virtues in? William Shakespeare, Twelfth Night Your public servants serve you right. Adlai Stevenson But what, to serve our private ends, Forbids the cheating of our friends? Charles Churchill This above all: to thine own self be true. William Shakespeare, Hamlet Have no friends not equal to yourself. Confucius Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Chapter 7 Arrays 377 and should print a (Adelphia web hosting)

Tuesday, April 24th, 2007

Chapter 7 Arrays 377 and should print a full computer dump in the format we discussed previously. This will help the user locate the error in the program. 7.44 (Modifications to the Simpletron Simulator) In Exercise 7.43, you wrote a software simulation of a computer that executes programs written in Simpletron Machine Language (SML). In this exercise, we propose several modifications and enhancements to the Simpletron Simulator. In Exercise 19.26 and Exercise 19.27, we propose building a compiler that converts programs written in a high-level programming language (a variation of Basic) to Simpletron Machine Language. Some of the following modifications and enhancements may be required to execute the programs produced by the compiler: a) Extend the Simpletron Simulator s memory to contain 1000 memory locations to enable the Simpletron to handle larger programs. b) Allow the simulator to perform modulus calculations. This requires an additional Simpletron Machine Language instruction. c) Allow the simulator to perform exponentiation calculations. This requires an additional Simpletron Machine Language instruction. d) Modify the simulator to use hexadecimal values rather than integer values to represent Simpletron Machine Language instructions. e) Modify the simulator to allow output of a newline. This requires an additional Simpletron Machine Language instruction. f) Modify the simulator to process floating-point values in addition to integer values. g) Modify the simulator to handle string input. [Hint: Each Simpletron word can be divided into two groups, each holding a two-digit integer. Each two-digit integer represents the ASCII decimal equivalent of a character. Add a machine-language instruction that will input a string and store the string beginning at a specific Simpletron memory location. The first half of the word at that location will be a count of the number of characters in the string (i.e., the length of the string). Each succeeding half-word contains one ASCII character expressed as two decimal digits. The machine-language instruction converts each character into its ASCII equivalent and assigns it to a half-word. ] h) Modify the simulator to handle output of strings stored in the format of part g). [Hint: Add a machine-language instruction that will print a string beginning at a certain Simpletron memory location. The first half of the word at that location is a count of the number of characters in the string (i.e., the length of the string). Each succeeding half-word contains one ASCII character expressed as two decimal digits. The machine-language instruction checks the length and prints the string by translating each two-digit number into its equivalent character.] 7.45 The Fibonacci series 0,1, 1, 2, 3, 5, 8, 13,21, begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. a) Write a nonrecursive method fibonacci(n)that calculates the nth Fibonacci num ber. Incorporate this method into an applet that enables the user to enter the value of n. b) Determine the largest Fibonacci number that can be printed on your system. c) Modify the program of part a) to use double instead of int to calculate and return Fi bonacci numbers and use this modified program to repeat part b). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

376 Arrays Chapter 7 REGISTERS: accumulator +0000 instructionCounter (Web site management)

Tuesday, April 24th, 2007

376 Arrays Chapter 7 REGISTERS: accumulator +0000 instructionCounter 00 instructionRegister +0000 operationCode 00 operand 00 MEMORY: 0 1 2 3 4 5 6 7 8 9 0 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 10 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 20 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 30 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 40 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 50 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 60 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 70 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 80 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 90 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 Fig. 7.37 A sample dump. This completes the simulated execution of the first instruction. When the user clicks the Executenextinstructionbutton, the entire process (i.e., the instruction execution cycle) begins again with the fetch of the next instruction to be executed. Now let us consider how the branching instructions the transfers of control are simulated. All we need to do is adjust the value in the instruction counter appropriately. Therefore, the unconditional branch instruction (40) is simulated within the switchas instructionCounter = operand; The conditional branch if accumulator is zero instruction is simulated as if ( accumulator == 0 ) instructionCounter = operand; At this point, you should implement your Simpletron simulator and run each of the SML programs you wrote in Exercise 7.42. You may embellish SML with additional features and provide for these in your simulator. Your simulator should check for various types of errors. During the program loading phase, for example, each number the user types into the Simpletron s memorymust be in the range -9999to +9999. Your simulator should test that each number entered is in this range, and, if not, keep prompting the user to reenter the number until the user enters a correct number. During the execution phase, your simulator should check for various serious errors, such as attempts to divide by zero, attempts to execute invalid operation codes, accumulator overflows (i.e., arithmetic operations resulting in values larger than +9999 or smaller than -9999) and the like. Such serious errors are called fatal errors. When a fatal error is detected, your simulator should print an error message such as: *** Attempt to divide by zero *** *** Simpletron execution abnormally terminated *** Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Web site domain - Chapter 7 Arrays 375 Codeand pick off the

Monday, April 23rd, 2007

Chapter 7 Arrays 375 Codeand pick off the right two digits and place them in operand. Each of the preceding registers should have a corresponding JTextField in which its current value can be displayed at all times. When Simpletron begins execution, the special registers are all initialized to 0. Now, let us walk through execution of the first SML instruction, +1009in memory location 00. This is called an instruction execution cycle. The instructionCountertells us the location of the next instruction to be performed. We fetch the contents of that location from memoryby using the Java statement instructionRegister = memory[ instructionCounter ]; The operation code and the operand are extracted from the instruction register by the statements operationCode = instructionRegister / 100; operand = instructionRegister % 100; Now the Simpletron must determine that the operation code is actually a read (versus a write, a load, etc.). A switchdifferentiates among the twelve operations of SML. In the switch structure, the behavior of various SML instructions is simulated as shown in Fig. 7.36. We discuss branch instructions shortly and leave the others to the reader. When the SML program completes execution, the name and contents of each register as well as the complete contents of memory should be displayed. Such a printout is often called a computer dump (and, no, a computer dump is not a place where old computers go). To help you program your dump method, a sample dump format is shown in Fig. 7.37. Note that a dump after executing a Simpletron program would show the actual values of instructions and data values at the moment execution terminated. The sample dump assumes the output will be sent to the display screen with a series of System.out.print and System.out.println method calls. However, we encourage you to experiment with a version that can be displayed on the applet using a JTextArea or an array of JTextFieldobjects. Let us proceed with the execution of our program s first instruction, namely the +1009in location 00. As we have indicated, the switch statement simulates this by prompting the user to enter a value into the input dialog, reading the value, converting the value to an integer and storing it in memory location memory[operand]. Since your Simpletron is event driven, it waits for the user to type a value into the input JTextField and press the Enter key. The value is then read into location 09. At this point, simulation of the first instruction is completed. All that remains is to prepare the Simpletron to execute the next instruction. Since the instruction just performed was not a transfer of control, we need merely increment the instruction counter register as follows: ++instructionCounter; Instruction Description read: Display an input dialog with the prompt Enter an integer. Convert the input value to an integer and store it in location memory[ operand ]. load: accumulator = memory[ operand ]; add: accumulator += memory[ operand ]; Fig. 7.3636 Behavior of several SML instructions in the Simpletron. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Web hosting e commerce - 374 Arrays Chapter 7 7.43 (A Computer Simulator)

Monday, April 23rd, 2007

374 Arrays Chapter 7 7.43 (A Computer Simulator) It may at first seem outrageous, but in this problem you are going to build your own computer. No, you will not be soldering components together. Rather, you will use the powerful technique of software-based simulation to create an object-oriented software model of the Simpletron. You will not be disappointed. Your Simpletron simulator will turn the computer you are using into a Simpletron, and you will actually be able to run, test and debug the SML programs you wrote in Exercise 7.42. Your Simpletron will be an event-driven applet you will click a button to execute each SML instruction and you will be able to see the instruction in action. When you run your Simpletron simulator, it should begin by displaying: *** Welcome to Simpletron! *** *** Please enter your program one instruction *** *** (or data word) at a time into the input *** *** text field. I will display the location *** *** number and a question mark (?). You then *** *** type the word for that location. Press the *** *** Done button to stop entering your program. *** The program should display an inputJTextField in which the user will type each instruction one at a time and a Done button for the user to click when the complete SML program has been entered. Simulate the memory of the Simpletron with a single-subscripted array memory that has 100 elements. Now assume that the simulator is running and let us examine the dialog as we enter the program of Fig. 7.35 (Exercise 7.42): 00 ? +1009 01 ? +1010 02 ? +2009 03 ? +3110 04 ? +4107 05 ? +1109 06 ? +4300 07 ? +1110 08 ? +4300 09 ? +0000 10 ? +0000 Your program should use a JTextFieldto display the memory location followed by a question mark. Each of the values to the right of a question mark is typed by the user into the input JTextField. When the Done button is clicked, the program should display: *** Program loading completed *** *** Program execution begins *** The SML program has now been placed (or loaded) in array memory. The Simpletron should provide an Executenextinstruction button the user can click to execute each instruction in your SML program. Execution begins with the instruction in location 00 and, like Java, continues sequentially, unless directed to some other part of the program by a transfer of control. Use the variable accumulator to represent the accumulator register. Use the variable instructionCounter to keep track of the location in memory that contains the instruction being performed. Use the variable operationCodeto indicate the operation currently being performed (i.e., the left two digits of the instruction word). Use the variable operand to indicate the memory location on which the current instruction operates. Thus, operand is the rightmost two digits of the instruction currently being performed. Do not execute instructions directly from memory. Rather, transfer the next instruction to be performed from memory to a variable called instructionRegister. Then pick off the left two digits and place them in operation Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Apache web server for windows - Chapter 7 Arrays 373 Location Number Instruction 00

Monday, April 23rd, 2007

Chapter 7 Arrays 373 Location Number Instruction 00 +1007 (Read A) 01 +1008 (Read B) 02 +2007 (Load A) 03 +3008 (Add B) 04 +2109 (Store C) 05 +1109 (Write C) 06 +4300 (Halt) 07 +0000 (Variable A) 08 +0000 (Variable B) 09 +0000 (Result C) Fig. 7.3434 SML program that reads two integers and computes their sum. Fig. The second SML program (Fig. 7.35) reads two numbers from the keyboard and determines and prints the larger value. Note the use of the instruction +4107as a conditional transfer of control, much the same as Java s ifstatement. Now write SML programs to accomplish each of the following tasks: a) Use a sentinel-controlled loop to read 10 positive numbers. Compute and print their sum. b) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute and print their average. c) Read a series of numbers and determine and print the largest number. The first number read indicates how many numbers should be processed. Location Number Instruction 00 +1009 (Read A) 01 +1010 (Read B) 02 +2009 (Load A) 03 +3110 (Subtract B) 04 +4107 (Branch negative to 07) 05 +1109 (Write A) 06 +4300 (Halt) 07 +1110 (Write B) 08 +4300 (Halt) 09 +0000 (Variable A) 10 +0000 (Variable B) Fig. 7.3535 SML program that reads two integers and determines which is larger. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

372 Arrays Chapter 7 Operation code Meaning Input/output

Monday, April 23rd, 2007

372 Arrays Chapter 7 Operation code Meaning Input/output operations: final int READ = 10; Read a word from the keyboard into a specific location in memory. final int WRITE = 11; Write a word from a specific location in memory to the screen. Load/store operations: final int LOAD = 20; Load a word from a specific location in memory into the accumulator. final int STORE = 21; Store a word from the accumulator into a specific location in memory. Arithmetic operations: final int ADD = 30; Add a word from a specific location in memory to the word in the accumulator (leave result in the accumulator). final int SUBTRACT = 31; Subtract a word from a specific location in memory from the word in the accumulator (leave result in the accumulator). final int DIVIDE = 32; Divide a word from a specific location in memory into the word in the accumulator (leave result in the accumulator). final int MULTIPLY = 33; Multiply a word from a specific location in memory by the word in the accumulator (leave result in the accumulator). Transfer of control operations: final int BRANCH = 40; Branch to a specific location in memory. final int BRANCHNEG = 41; Branch to a specific location in memory if the accumulator is negative. final int BRANCHZERO = 42; Branch to a specific location in memory if the accumulator is 0. final int HALT = 43; Halt the program has completed its task. Fig. 7.33 Simpletron Machine Language (SML) operation codes . The first SML program (Fig. 7.34) reads two numbers from the keyboard and computes and prints their sum. The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to 0). Then, instruction +1008 reads the next number into location 08. The load instruction, +2007, puts the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator. The store instruction, +2109, places the result back into memory location 09 from which the write instruction, +1109, takes the number and prints it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Chapter 7 Arrays 371 (Com web hosting) Animal Move type Percentage

Sunday, April 22nd, 2007

Chapter 7 Arrays 371 Animal Move type Percentage of the time Actual move Tortoise Fast plod 50% 3 squares to the right Slip 20% 6 squares to the left Slow plod 30% 1 square to the right Hare Sleep 20% No move at all Big hop 20% 9 squares to the right Big slip 10% 12 squares to the left Small hop 30% 1 square to the right Small slip 20% 2 squares to the left Fig. 7.3232 Rules for adjusting the positions of the tortoise and the hare. Fig. SPECIAL SECTION: BUILDING YOUR OWN COMPUTER In the next several problems, we take a temporary diversion away from the world of high-level language programming. We peel open a computer and look at its internal structure. We introduce machine-language programming and write several machine-language programs. To make this an especially valuable experience, we then build a computer (through the technique of software-based simulation) on which you can execute your machine-language programs! 7.42 (Machine-Language Programming) Let us create a computer we will call the Simpletron. As its name implies, it is a simple machine, but as we will soon see, a powerful one as well. The Simpletron runs programs written in the only language it directly understands, that is, Simpletron Machine Language or SML for short. The Simpletron contains an accumulator a special register in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number such as +3364, -1293, +0007, -0001, etc. The Simpletron is equipped with a 100-word memory and these words are referenced by their location numbers 00, 01, , 99. Before running an SML program, we must load or place the program into memory. The first instruction (or statement) of every SML program is always placed in location 00. The simulator will start executing at this location. Each instruction written in SML occupies one word of the Simpletron s memory (and hence instructions are signed four-digit decimal numbers). We shall assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Simpletron s memory may contain either an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the operation code specifying the operation to be performed. SML operation codes are summarized in Fig. 7.33. The last two digits of an SML instruction are the operand the address of the memory location containing the word to which the operation applies. Let s consider several simple SML programs. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

370 Arrays Chapter 7 from the wall, eventually (Web hosting services)

Sunday, April 22nd, 2007

370 Arrays Chapter 7 from the wall, eventually you will arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you are guaranteed to get out of the maze if you follow the algorithm. Write recursive method mazeTraverse to walk through the maze. The method should receive as arguments a 12-by-12 character array representing the maze and the starting location of the maze. As mazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The method should display the maze after each move so the user can watch as the maze is solved. 7.39 (Generating Mazes Randomly) Write a method mazeGenerator that takes as an argument a double-subscripted 12-by-12 character array and randomly produces a maze. The method should also provide the starting and ending locations of the maze. Try your method mazeTraverse from Exercise 7.38, using several randomly generated mazes. 7.40 (Mazes of Any Size) Generalize methods mazeTraverse and mazeGenerator of Exercise 7.38 and Exercise 7.39 to process mazes of any width and height. 7.41 (Simulation: The Tortoise and the Hare) In this problem, you will recreate one of the truly great moments in history, namely the classic race of the tortoise and the hare. You will use random number generation to develop a simulation of this memorable event. Our contenders begin the race at square 1 of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. There is a clock that ticks once per second. With each tick of the clock, your applet should adjust the position of the animals according to the rules in Fig. 7.32 Use variables to keep track of the positions of the animals (i.e., position numbers are 1 70). Start each animal at position 1 (i.e., the starting gate ). If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in the preceding table by producing a random integer, i, in the range 1 = i = 10. For the tortoise, perform a fast plod when 1 = i = 5, a slip when 6 = i = 7 or a slow plod when 8 = i = 10. Use a similar technique to move the hare. Begin the race by printing BANG !!!!! AND THEY’RE OFF !!!!! Then, for each tick of the clock (i.e., each repetition of a loop), print a 70-position line showing the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally, the contenders will land on the same square. In this case, the tortoise bites the hare, and your program should print OUCH!!! beginning at that position. All print positions other than the T, the H or the OUCH!!!(in case of a tie) should be blank. After each line is printed, test for whether either animal has reached or passed square 70. If so, print the winner and terminate the simulation. If the tortoise wins, print TORTOISE WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the turtle (the underdog ) or you may want to print It’satie. If neither animal wins, perform the loop again to simulate the next tick of the clock. When you are ready to run your program, assemble a group of fans to watch the race. You ll be amazed at how involved your audience gets! Later in the book, we introduce a number of Java capabilities, such as graphics, images, animation, sound and multithreading. As you study those features, you might enjoy enhancing your tortoise and hare contest simulation. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

Web design - Chapter 7 Arrays 369 a) Starting from the

Sunday, April 22nd, 2007

Chapter 7 Arrays 369 a) Starting from the rightmost element of the array, compare each element to 37 until an element less than 37 is found, then swap 37 and that element. The first element less than 37 is 12, so 37 and 12 are swapped. The new array is 12 2 6 4 89 8 10 37 68 45 Element 12 is in italic to indicate that it was just swapped with 37. b) Starting from the left of the array, but beginning with the element after 12, compare each element to 37 until an element greater than 37 is found, then swap 37 and that element. The first element greater than 37 is 89, so 37 and 89 are swapped. The new array is 12 2 6 4 37 8 10 89 68 45 c) Starting from the right, but beginning with the element before 89, compare each element to 37 until an element less than 37 is found, then swap 37 and that element. The first element less than 37 is 10, so 37 and 10 are swapped. The new array is 12 2 6 4 10 8 37 89 68 45 d) Starting from the left, but beginning with the element after 10, compare each element to 37 until an element greater than 37 is found, then swap 37 and that element. There are no more elements greater than 37, so when we compare 37 to itself we know that 37 has been placed in its final location of the sorted array. Once the partition has been applied on the previous array, there are two unsorted subarrays. The sub- array with values less than 37 contains 12, 2, 6, 4, 10 and 8. The subarray with values greater than 37 contains 89, 68 and 45. The sort continues with both subarrays being partitioned in the same manner as the original array. Based on the preceding discussion, write recursive method quickSort to sort a single-subscripted integer array. The method should receive as arguments an integer array, a starting subscript and an ending subscript. Method partition should be called by quickSort to perform the partitioning step. 7.38 (Maze Traversal) The following grid of #s and dots (.) is a double-subscripted array representation of a maze. # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . # # # . # . . # # # # . # . # . # . # # . . # . # . # . # . # # # . # . # . # . # . # # . . . . . . . . # . # # # # # # # . # # # . # # . . . . . . # . . . # # # # # # # # # # # # # In the preceding double-subscripted array, the #s represent the walls of the maze, and the dots represent squares in the possible paths through the maze. Moves can be made only to a location in the array that contains a dot. There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services