The following guidelines are expected for all homework submissions:
var i = 0; while( i < 10 ) { continue; i += 1; } alert( "All done" );
n
is divisible by 2, you know immediately that n
is compositen
for
statement
that alerts 10, then alerts 9, and so on, until a final alert of 0.for
statement.next
property were not null
but
rather a reference to the first object in the chain?j
to iterate through the columns. What would the
script have produced if the programmer had accidentally used the variable i
for both
the outer and inner loop indices? [Hint: be careful about array bounds.]average( [4, 5, 7, 2] )
should
return 4.5."Rat-a-tat-tat"
and the character "t"
, your function
should return 5
.balanceAfter
function from this chapter gives troublesome results for negative numbers
of compounding periods. Ask Wolfram Alpha to graph the function
1000 * (1 + 0.05/n)10n
and explain what you are seeing for negative
values of n
.sum
function from this chapter when given an argument that is not an array? Is this
worrisome? Discuss whether or not the function should be rewritten to check for a non-array
argument.isPrime
function in a shell on the following arguments: undefined
, null
,
false
, true
, "4"
, " 2 "
,
" 2 "
, and [19]
.Give each element a<div> <div> <div> </div> </div> </div>
click
handler that identifies, through an alert
dialog, the clicked-on element and whether or not the event is in the capturing or bubbling phase.
Open the web page and, before clicking on various elements, predict the sequence of
alert
messages that you will see. Open the same page and click around using different
web browsers as well, and determine which browsers have similar or different event behavior. [Hint:
read carefully the section in your text book about bubbling and capturing so you understand what is
being asked of your program.]These programming problems are designed to give you practice at creating algorithmic solutions to some straightforward problems. The intent is not to overload you with tons of homework; rather, it is to help you practice the concepts you have learned in class about breaking the problem down into parts, figuring out the algorithm for each part, writing the whole thing down, doing a desk check, and coding it up and testing it with various inputs. To that end, You may select any two of these three problems. If you would like to do all three, feel free to do so, of course. I'll give you one extra credit point for your correct solution.
Exercise 1: Expand the tic-tac-toe game from your text book to do a four-by-four game instead of three-by-three. (Note: this sounds a lot harder than it is...) The code begins on page 254 of your book.
Exercise 2 [part one of two parts]:Ch 7, #22: Write a function that takes in a string and returns the reversal of the string. For example:
reversal( "" ) // returns "" (the empty string) reversal( "a" ) // returns "a" reversal( "string" ) // returns "gnirts"
Exercise 2 [part two of two parts]Ch 7, #23: Next, build a web interface
to the string reversal function. Your web page should have a single input box and a button labeled
Reverse
. Clicking the button should cause the text in the input box to be replaced with the
reverse of its current contents. (Bonus: see how few lines of code you can use to accomplish this task.)
Exercise 3: Given a total monetary amount and set of coin denominations,
determine the smallest number of coins required to make the total amount. For example, given the total
amount of $1.23 and the denominations 50, 25, 10, 5, and 1, the answer should be two-50, two-10 and
three-one coins. If some denomination is provided which is unusable, such as a negative value, the
program should simply say "IMPOSSIBLE" for that denomination. The program should be contained in a
web page, in which the user inputs an arbitrary amount of money (expressed as a non-negative, whole
number of cents) in one text box, followed by a sequence of coin denominations (distinct positive
integers) in the second text box. The page must then display in its footer area the optimal way of
making change for the given amount using the given set of denominations. If there is no way to make
the change, the program must output the message IMPOSSIBLE
.
The following problems are from the book "Python for Everyone" by Cay Horstmann and Rance D. Necaise, ISBN 978-1-118-62613-9.
Exercise 4: (PYTHON PROGRAMMING — text exsample 3.2, page 131) Two circles may intersect at a single point, at two points, or at an unlimited number of points when the two circles are coincident (on top of each other). If the circles do NOT intersect, one circle may be contained entirely within the other, or they may be completely separate. Your task is to write a program in Python that obtains the parameters for two circles and reports whether they intersect. For each circle, the inputs should be the X and Y coordinate (on a Cartesian grid) of the center, and the radius of the circle. The output should be one and only one of the possible configurations listed above, as well as the distance between the two center points. (Hint: the distance can be determined by using the Pythagorean Theorem.)
Exercise 5: (PYTHON PROGRAMMING — Business P3.35) Calculating the tip when you go to a restaurant is not difficult, but your restaurant wants to suggest a tip according to the service diners receive. Write a program in Python that calculates a tip according to the diner's satisfaction as follows:
Exercise 6: (PYTHON PROGRAMMING — Science P3.40) Sound level
L in units of decibel (dB) is calculated by
L = 20 log
1 | Threshold of pain | 130 dB |
2 | Possible hearing damamge | 120 dB |
3 | Jackhammer at 1 meter distance | 100 dB |
4 | Traffic on a busy roadway at 10 meters distance | 90 dB |
5 | Normal conversation | 60 dB |
6 | Calm library | 30 dB |
7 | Light leaf rustling sound | 0 dB |
Write a Python program that reads a value and a unit, either dB or Pa, and then prints the closest description from the table above.
Exercise 7: (PYTHON PROGRAMMING — Science P3.44) A mass,
m of 2 kilograms is attached to the end of a rope of length
r = 3 meters. The mass
is whirled around at high speed. The rope can withstand a maximum tension T of 60 Newtons
before it will snap. Write a program that accepts a rotation speed V and determines whether
such a speed will cause the rope to break. Use the formula T = mV² / r to calculate.