|
CSCI E-50a, Harvard Extension School
Monday Section Home Page
|
TF: David Roazen (droazen at fas dot harvard dot edu)
Section meeting time/place: Mondays 8:00-9:00 PM, Science Center Room
B-10
Office hours: Immediately following section, or by appointment
On this page I'll be posting the exercises we do in the Monday section for
CSCI E-50a. Please check this page regularly, as it will be updated with new
exercises after each section meeting. Please feel free to email
me if you have questions about any of the exercises we do in section.
To download an exercise to your local machine, simply right click (or control
click, on a Mac) on it and select "save link as..." in your web browser. To
download an exercise to your Harvard FAS account, use the wget command
while logged in to nice.harvard.edu, and give it the full URL of the file you
want to download. The syntax is:
wget 'url_of_file_to_download'
(note the single quotes around the URL). For example, to download the Hello
World exercise, you would type the following at the Unix prompt:
wget
'http://www.people.fas.harvard.edu/~droazen/e50a/unit2/HelloWorld.java'
Contents:
Announcements
- All unit 2 grades have been returned by email.
- I've posted guidelines for Java coding style in the documents section
below.
Helpful Documents:
Section Exercises for Unit 1:
- dancerInterrupted.sb, a program
that produces a simple animation of a man dancing, and allows the user to stop
and start the dance by pressing the spacebar. Demonstrates how to use a
variable to keep track of the state a program is in, and provides practice
with loops, if statements, and responding to keypresses.
- taxBreak.sb, a program that prompts the
user for the number of pools and yachts he/she owns, and then uses that
information to determine whether the user is eligible for a tax break.
Practice with user input, variables, boolean expressions, and if statements.
- multTable.sb, a program that causes a
sprite to recite the multiplication table from "1 times 1" up to "5 times 5".
Practice with variables, loops, and boolean expressions, and in particular the
concept of a nested loop (a loop inside of another loop).
- sumRange.sb, a program that prompts the
user for two integers, and then computes the sum of the integers in that
range, inclusive. More practice with user input, variables, and loops.
- dogYears.sb, a program that converts dog
years to human years using the following formula: the first 2 years of a
dog's life count as 10.5 human years, and all subsequent years count as 4
human years. Practice writing complex arithmetic expressions in Scratch, and
error-checking user input to make sure that it makes sense before using it in a
calculation.
- nestedTriangles.sb, a program that
draws a series of nested right triangles. Practice drawing plane shapes in
Scratch using the "pen" feature.
Section Exercises for Unit 2:
- HelloWorld.java, the classic Hello
World program! Prints the message "Hello, world!" to the screen.
- PrintPuzzles.java, practice with
print()/println() statements, escape sequences, String concatenation, and
printing the values inside of variables.
- CheckerBoard.java, a class that
prints a checkerboard pattern to the screen. Demonstrates how to use
stepwise refinement to break a large task up into smaller subtasks, and
then break those subtasks up into still smaller subtasks, until we reach the
smallest possible subtasks that can't be broken up any further. Each subtask
we identify becomes a method in our class.
- CheckerBoardWithFinalVariables.java,
an improved version of the CheckerBoard class that uses named constants (ie.,
final variables) to store the values that don't change during program
execution. This makes it easy to modify these values in the future, and
improves the readability of the code by replacing literal values like 4 and 6
with descriptive names like SQUARE_SIZE and BOARD_HEIGHT.
- IntegerDivision.java, a class
that demonstrates the difference between integer division, in which the
remainder is discarded, and floating-point division, in which the remainder is
retained.
- OperatorPractice.java, a class
that provides practice with operator precedence and associativity.
- PerfectSquares.java, a program
that prints the first 10 perfect squares. Practice writing simple for loops.
- Powers.java, a program that uses
nested for loops to produce output in which the 1st line contains the
integers 1 through 10 raised to the 1st power, the 2nd line contains the
integers 1 through 10 raised to the 2nd power, the 3rd line contains the
integers 1 through 10 raised to the 3rd power, and so on. Practice with
for loops, named constants, and using methods from the Math class.
- Summation.java, a program that uses
nested for loops to print the sum of the integers from 1 to 1, then from 1 to
2, then from 1 to 3, and so on up to the sum of the integers from 1 to 10.
Practice with a type of nested for loop in which the number of times the inner
loop loops depends on the current value of the index variable of the outer
loop.
- OddEven.java, a program that prompts the
user for an integer, then prints out whether that int is even or odd. Practice
with if/else statements and user input using Scanner objects.
- NerdSurvey.java, a program that asks
the user how many computers he/she owns, and then prints an appropriate
reaction message. Practice with if/else if/else statements and user input
using Scanner.
- Prob6.java, a corrected version of the
class from problem 6 on the practice midterm.
- Prime.java, a class that prints out all of
the prime numbers between 101 and 201, inclusive. Solution to problem 7 on the
practice midterm. See PrimeWithBooleanVariable.java below for an alternate
version of this class.
- SmallestEven.java, a class that
reads in 10 integers from the keyboard and prints out the smallest even
integer entered, or an error message if no even integers were entered at all.
More practice with user input using Scanner and if statements. See
SmallestEvenWithBooleanVariable.java below for an alternate version of this
class.
Section Exercises for Unit 3:
- PrimeWithBooleanVariable.java,
an alternate version of Prime.java from the unit 2 section above that uses the
unit 3 concept of a boolean variable to keep track of whether or not the
current number is prime.
- SmallestEvenWithBooleanVariable.java,
an alternate version of SmallestEven.java from the unit 2 section above that
uses the unit 3 concept of a boolean variable to keep track of whether or not
an even number has been encountered.
- NaturalizationSurvey.java, a
program that asks the user a series of true/false questions, and then
determines whether the user is eligible to become a U.S. citizen based on
his/her answers. The questions, bizarre though they may be, are mostly taken
verbatim from the U.S. Citizenship and Immigration Services "N-400
Application for Naturalization" form. Provides practice with boolean
expressions and boolean variables.
- WhileVSDoWhile.java, a class
designed to highlight the difference between a while loop, which performs its
boolean test before executing the loop body and which may potentially
execute zero times, and a do-while loop, which performs its boolean test
after executing the loop body and which will always execute at least
one time.
- ForToWhile.java, a class that shows
how to re-write a simple for loop using a while loop, so that the while loop
behaves identically to the original for loop in all cases.
- DiceGame.java, a program that plays a
simple game involving a pair of six-sided dice. The object of the game is to
roll doubles before rolling a 7. Practice with random number generation using
Math.random(), do-while loops, and boolean variables.
- BiasedCoinToss.java, a program
that simulates tosses of a biased coin -- that is, a coin in which the
probability of getting heads is not the same as the probability of getting
tails. Shows how to use Math.random() to cause something to happen with a
particular probability. Also shows how to use a do-while loop to repeatedly
prompt a user until he/she enters input that is legal.
- GlobalVariable.java, a class that
shows how to declare a global variable (a variable that can be accessed and
modified by any method in a class). Global variables are usually not the
preferred choice for sharing data between multiple methods, but until we learn
about parameters and return values it's the only method we've got!
- RandomInRangeBoneheadedVersion.java,
a silly, overly repetitious program written all in main() that generates a
series of random numbers within different ranges. Shows the need for reusable
methods to avoid this sort of boneheaded repetition!
- RandomInRangeWithGlobalVariables.java,
a second version of the RandomInRange program that creates a separate method
to handle the random number generation, and uses global variables to pass
information back and forth between that method and main(). An improvement over
the original, but still not ideal...
- RandomInRangeWithParametersAndReturns.java,
a third version of the RandomInRange program that also creates a separate
method to handle the random number generation, but uses parameter-passing and
return values to pass information back and forth between that method and
main(). Definitely the best of the three versions of the program from the
standpoint of conciseness and modularity!
- PrintVertical.java, a program that
prompts the user for a sentence, and then calls upon a method named
printVertical() to print that sentence "vertically", with one character per
line. The program continues until the user types "quit". Practice with
Strings, parameter passing, and while/do-while loops.
- VoidVSNonVoid.java, a class that
highlights the differences between the ways void and non-void methods can
legally be called.
- PredicateMethods.java, a class
containing a number of examples of predicate methods -- that is,
methods that return a boolean value of true or false: is20thCenturyYear(),
isInFirstHalfOfAlphabet(), defeatsInRockPaperScissors(), and charIsInString()
Section Exercises for Unit 4: