CMSI 185: Homework Assignment #2
General Details
Due Date: October 19, 2017 [2017-10-19, Thursday of week 08]
The following guidelines are expected for all homework submissions:
- All homework must be typed. Homework which is not typed will be returned ungraded and will be
subject to the late homework guidelines as set out on the syllabus page. PLEASE DO NOT scribble
something on lined paper and rip it out of your spiral notebook — hanging chads went out
with the presidential election in 2000.
- I don't care too much about what font you use or how large your margins are; however, you might
want to check out a monospaced font for typing code, as it will be easy to see the indentations.
- Speaking of indenting, PLEASE DON'T USE TABS TO INDENT YOUR CODE. Tabs can often get interpreted
differently by different computers and applications, and could make code that is nicely formatted
on your computer look "all over the map" on my computer or printer. USE SPACES INSTEAD.
You can set up almost every modern text editor to insert spaces whenever you press the TAB key, or
you can simply pound the spacebar.
- Work with a partner. This mimics an industry code development model called "pair programming"
which is part of the Extreme Programming software development method. Feel free to collaborate in
your pairs as much as you want, preferably doing the entire assignment together.
- DO NOT share your work between groups. Doing so will count as plagiarism. If you wish to discuss
solutions with another group over coffee in the Lair, that's fine as long as it is kept at the
conceptual level, but each group needs to turn in its own version of the solutions.
- You only need to turn in one copy per group.
- If you'd like to use the jsfiddle website to do your programs,
that is great. Simply save the program in jsfiddle as a shared fiddle, then copy and embed a link
to the fiddle into your homework where the answer goes for that problem. I'll use the link to go
to YOUR fiddle and run the code. To save your fiddles you'll need to sign up for an account…
Problems From Chapter 2
- Ch 2, #30: Check the following poorly
formatted script with JSLint by copying it and pasting it into
the JSLint text area, then clicking on the "JSLint" button:
var x=0;var y=1;var sequence=[];
while(y<1000){sequence.push(y);z=x+y;x=y;y= z;}
alert (sequence);
Use JSLint's suggestions to clean up the script. (Also, feel free to experiement with JSLint's
checkbox options to see what happens.) Submit only your "cleaned-up" version.
- Ch 2, #37: Although this text focuses on
JavaScript as a language for scripting web browsers, JavaScript is used to write server-side code
as well. Read about server-side JavaScript online and try to install the
node.js
application (available at http://nodejs.org). Do not worry about
writing anything in node for now; instead, experiment with node's shell. How is this shell
similar to, and different from, the shell from squarefree.com
discussed in this
chapter?
- pg 31, #2 : Explain the difference between the
following output strategies: (1) alert, (2) document.write, and (3) modifying the values of HTML
elements.
- pg 34, #1 : Rewrite the expression:
!x && y || z + 5 * 4 >= 3 == y && z
in its fully parenthesized form.
- pg 34, #2 : Explain, in English, the meaning
of
x < y || x < z
- pg 39, #2 : Is
first-guess
a legal
variable name in JavaScript? Provide reasons for your answer.
- pg 39, #3 : What does the script
var size = 6; alert( Size );
do? (Hint: Pay attention
to the case of the letters in the names.) Explain.
Problems From Chapter 3
- Ch 3, #6 : Execute the following script in
the scriptrunner window:
var celsius = prompt( "Enter a temperature in \u00b0C" );
var fahrenheit = 1.8 * celsius + 32;
alert( celsius + "\u00b0C = " + fahrenheit + "\u00b0F" );
giving inputs of 0
, 37
, -40
, and 100
. Then
execute with inputs such as dog
, 2e600
, 3ffc
, and
Infinity
. For each execution, state whether your observed result made sense to
you; if not, state why the result differed from your expectation.
- Ch 3, #8 : Evaluate the following expressions:
(a) 5 / 0
,
(b) 0 / 0
,
(c) Infinity + Infinity
,
(d) Infinity - Infinity
,
(e) Infinity * Infinity
,
(f) Infinity / Infinity
,
(g) Math.sqrt( Infinity )
.
Do these results make sense? Why or why not?
- Ch 3, #12: Repackage the die-rolling web page
from this chapter so that the HTML document and the script are in separate files. Make the script
output values for two dice, and make the output display the proper dice glyph from the HTML set.
Be sure to use the style property to make the dice large enough to see easily.
Algorithm Exercises
Exercise 1: Implement a web page that performs the Russian
Peasant Multiplication algorithm. You must have two input boxes for the two numbers, and a button
to click for performing the operation. You must also have the output show on the footer of the web
page (like we did in class for the document properties list), and you must make the output show all
the operation's steps in columns. If you are programming in jsFiddle, you may use the tab character
to separate the columns, or alternatively you might want to get fancy and use a table to show your
output.
Exercise 2: Implement a web page that contains a representation of a
stack. The stack must have ten slots. Show the stack vertically on the page with the top of the
stack at the top. Include a text input and a button to cause the input value to be pushed onto the
stack; also include a button to cause a pop operation to remove the top element. You must be sure
to alert if the stack is empty when popping or full when pushing.
Python Programming Problems
Read chapters one through six in the Python book softcopy (it sounds like a lot, but the chapters are
quite short — it's only 35 pages). Feel free to try some or all of the exercises at the ends
of the chapters, several of which appear below as part of this homework assignment.
- Python book Ch 2, #2.7 Modify listing 2.1 to
create your own Mad Lib program. You may want to look ahead to Chapter 8 to use the sep=
or end= options of the print() statement to better control your output.
- Python book Ch 2, #2.12 Modify Listing 1.1 to
write a program sphere.py that calculates and prints the volume and surface area of a
sphere given its radius. Look up the formulae if you do not remember them. Run your program with
different values of the radius to find the point at which the volume equals the surface area.
- Python book Ch 3, #3.13 Write a program called
temp.py that asks the user for a temperature in degrees Fahrenheit, and then calculates
and prints the corresponding temperature in degrees Celsius. Use a function to perform the
conversion.
- Python book Ch 3, #3.18 Write a program called
compound.py to calculate the new balance of a savings account if interest is compounded
n times per year. Ask the user for the principal, interest rate, number of years, and
number of compounding periods per year, and display the new balance. (Look up the formula on
page 18 of the book — it is provided in the problem text.) Use appropriate functions.
Use your program to determine the difference over 20 years on a $1000 balance between compounding
yearly (compounding period of one) and compounding monthly (compounding period of 12) at an
interest rate of 8%.
- Python book Ch 4, #4.9 Write a program called
temptable.py that prints a table of Fahrenheit to Celsius conversions for temperatures
between 30 degrees F and 100 degrees F at 10 degree intervals. Use the conversion function you
developed for your program temp.py above.