A Balanced Introduction to Computer Science, 3/EDavid Reed
|
||
Features Contents Code Errata Student Instructor Feedback 2/E |
Additional Practice Exercises (with Solutions)Ch2 | Ch4 | Ch5 | Ch7 | Ch9 | Ch11 | Ch13 | Ch15 | Ch17Chapter 2The next four exercises refer to the following HTML document:
Exercise 2ADraw a picture of the Web page that would be generated by the above HTML document. Be specific with respect to formatting and page layout. Exercise 2BWhat is the purpose of the tags that begin Exercise 2C
In what directory must the image file
Exercise 2DWhat would happen if the user were to click on the link at the bottom of this page? What if the user's computer were not connected to the Internet? Chapter 4The next four exercises refer to the following HTML document:
Exercise 4ADraw a picture of the Web page that would be generated by the above HTML document. Be specific with respect to formatting and page layout. Exercise 4BDescribe the changes that would occur within the page when one of the buttons is clicked. Exercise 4CSuppose the developer of the page had made a typing error, misspelling the name Exercise 4DModify the page so that there is an image above the buttons. Initially, this image should display the question mark icon used in the book ( Chapter 5The next two exercises refer to the following JavaScript statements, which access a student's grade from a text box and display a scaled grade in a page division. Exercise 5AThe first statement determines the amount by which the grade will be scaled. What is the possible range of scale amounts? That is, what are the smallest and largest possible scale amounts? Can the scale amount be a fraction, or will it always be a whole integer? Exercise 5BThis code contains a logic error that will result in scaled grades well outside the specified range. For example, it is possible for the user to enter a grade of The next four exercises refer to the following HTML document, which could be used by a person to select their favorite animal. You may assume that the following four images are stored in the same folder as this page:
Exercise 5CDraw a picture of the Web page that would be displayed when this HTML document is loaded. Your drawing should clearly show the layout of the page elements, along with any text, labels, and default values, as they would initially appear in the page. Exercise 5DSuppose the user entered a type of animal in the text box then clicked on the button. What would happen in the page? Be specific. Exercise 5EAugment the above page so that, in addition to its current behavior, the image in the page is updated to display the chosen animal. For example, if the user entered Exercise 5FWhat would happen in your modified page if the user entered an animal for which there was no corresponding image? For example, if the user entered Chapter 7The next three exercises refer to the following HTML document, which could be used by a person to calculate the hypotenuse of a right triangle. Given the lengths of the two sides a and b of a right triangle, the length of the third side c (the hypotenuse) is: .
Exercise 7ADraw a picture of the Web page that would be displayed when this HTML document is loaded. Your drawing should clearly show the layout of the page elements, along with any text, labels, and default values, as they would initially appear in the page. Exercise 7BWhat is the purpose of the two lines of text in the HEAD of the page that begin with double slashes ( Exercise 7CComplete the definition of the The next two exercises refer to the following HTML document, which could be used by a person to calculate the "body mass index" of a patient. The Center for Disease Control (CDC) defines body mass index (BMI) using the following formula: BMI = (weight in kilograms) / (height in meters)2.
Exercise 7DDraw a picture of the Web page that would be displayed when this HTML document is loaded. Your drawing should clearly show the layout of the page elements, along with any text, labels, and default values, as they would initially appear in the page. Exercise 7EComplete the definition of the Chapter 9The next four exercises refer to the following HTML document, which could be used by a person to test whether they have ESP. You may assume that the following images are stored in the same folder as this page:
Exercise 9ADraw a picture of the Web page that would be displayed when this HTML document is loaded. Your drawing should clearly show the layout of the page elements, along with any text, labels, and default values, as they would initially appear in the page. Exercise 9BWhat (if anything) changes in the page when the user clicks on the button? Is the behavior the same each click? Explain your answer. Exercise 9CModify the page so that it selects its random number from the range 1-9 (instead of 1-3). You may assume that image files Exercise 9DAugment the above page so that, in addition to its current behavior, a message also appears in Chapter 11Exercise 11AConsider the following function that takes two numbers as inputs: Exercise 11BDesign and implement a Web page with three text boxes and a button. When the user clicks the button, the page should display a message identifying whether any of the box contents are identical. The three possible message are 'All values are unique.', 'Two of the three match.', or 'All three are the same.' Exercise 11CDesign and implement a Web page that contains a single button. When the user clicks the button, the page should simulate flipping two coins and display the results, e.g., Exercise 11DAdd a counter to your page from Exercise 11C that keeps track of the number of flip matches. Initially, this counter should be 0, and it should be incremented each time matching flips are obtained. Chapter 13Exercise 13AConsider the following function that takes a single number as input: Exercise 13BDesign and implement a Web page with two text boxes (which are to contain positive integers) and a button. When the user clicks the button, the page should display all of the integers in the range 1 to 100 that are divisible by the number in the first box, but not divisible by the number in the second box. For example, if the boxes contained Exercise 13C
Modify your page from Exercise 13B so that it also displays a count of how many numbers were displayed.
For example,
Chapter 15Exercise 15AConsider the following function that takes a single string as input: Exercise 15B
Consider the page from Exercise 15A. What would happen if the statement
Exercise 15CDesign and implement a page that contains a text area and button. When the user clicks on the button, any text in the text area is analyzed and statistics displayed in the page. The page should display the number of (non-space) characters in the text area, the number of sentences, and the average number of (non-space) characters per sentence. For simplicity, we will consider any occurrence of a period, question mark, or exclamation mark to denote the end of a sentence. Chapter 17Exercise 17AConsider the following array assignment: Exercise 17B
Modify your text statistics page from Exercise 15C so that it also displays the number of words
in the text area and the average number of words per sentence. In order to accomplish this, you will need
to split the text into individual words using the
Solutions to Practice ExercisesExercise 2AExercise 2BThese are comment tags. They are there to document the page, in this case by displaying the file name and a short description. Comments are ignored by the browser, so removing them would not change the appearance of the page in the browser. Exercise 2CSince the SRC is assigned a relative address (just the file name Exercise 2DWhen the user clicks on the link, the browser will take them to the Wikipedia site for Omaha (whose address is
Exercise 4AExercise 4BWhen a button is clicked, each Exercise 4CThe page would still load the same, displaying the buttons and text as before.
When the button is clicked, however, it would cause a JavaScript error, with the resulting
error message (in the Error Console) complaining that the specified element is "null" or
"not found.". The current contents of the
Exercise 4DThe paragraph within the BODY should be changed as follows: Exercise 5AThe scale amount will be an integer ranging from 0 to 4. The smallest value obtainable from Exercise 5BWhenever you access the VALUE attribute of a text box, the box contents are treated as a string. Thus, if the user entered Exercise 5CExercise 5DThe quote in the page would be replaced by a message that included the animal name. For example, if the user entered Exercise 5EThe IMG element within the BODY should be changed as follows: Exercise 5FBrowsers differ in how they would handle this error. Safari generates an error message, stating that the "resource failed to load", and the image is replaced by the broken image icon. Firefox simply ignores the error, leaving the image in the page unchanged. Exercise 7AExercise 7BThese are JavaScript comments. They are there to document the behavior of the function. Any text preceded by double-slashes is ignored by the JavaScript interpreter, so removing these lines would not change the behavior of the page. Exercise 7C
Exercise 7DExercise 7E
Exercise 9AExercise 9BA random integer between 1 and 3 is selected, and the image ( Exercise 9CSimply change the Exercise 9DAdd the following assignment at the end of the function: Exercise 11A
Exercise 11B
Exercise 11C
Exercise 11D
|
tag)
Number of matches: 0
// Add at end of IF statement (before the right curly-brace) document.getElementById('matchSpan').innerHTML = parseFloat(document.getElementById('matchSpan').innerHTML)+1;
Loopy(0) → ''
Loopy(1) → '1'
Loopy(4) → '100'
Loopy(11) → '1011'
While Demo Page Display all integers between 1 and 100 that are divisible by: but not divisible by .