Laboratory 1:
Expressions, Statements, and Interactions
Overview
This lab will allow you to get some practice writing simple expressions
and statements in Java. It will also give you an opportunity to experiment
with interactions among entities and how these generate a variety of basic
behaviors. Portions of this lab assignment are also designed to familiarize
you with the 6.096 lab environment and to begin to build a backkground
in thinking the 6.096 way.
Be sure to read through the 6.096 general
information handout, and in particular the collaboration policy. For
this assignment, you may discuss the project in as much detail as you like
with your classmates, but you should do the writeup on your own. You may
also get comments from other students on all portions of your writeup before
turning it in, if you wish. Please include the names of anyone with whom
you collaborate, in any way, on this assignment, and indicte the nature
of the collaboration. [Failure to include this information is a violation
of the collaboration policy.]
This assignment emphasizes the following topics
-
Java expressions and statements
-
Use of the 6.096 laboratory
-
Thinking in terms of interactions
You should read through this entire assignment and complete the Lab
preparation sectionbefore you come to lab. Some portions of
the PostLab writeup also require your thinking about them before and during
the laboratory portion of the assignment.
This assignment is due at the beginning of class on Monday,
15 September.
Contents
Pre-Lab
This week's pre-lab has three parts. Future labs will have more extensive
explicit lab preparation but less additional preparatory work.
A. Written Exercise
Pretend that you are corresponding with a Martian pen pal. (Alternately,
pretend you're corresponding with your 6.096 professor, which can often
feel the same.) Pick something that you encounter in every day life and
describe/explain it to your pen pal. You may choose (for example) an artifact,
an institution, a process or activity. Include both descriptions of how
it behaves/how you use it, of how it fits into context, and also of how
it works.
Remember that Martians (and Professors) know very little about life
on Earth, so you should make your description fairly detailed and specific.
The description should run approximately two paragraphs, and in no circumstances
should it exceed one page. (Martians have notoriously short attention spans.)
Bring this writeup with you to lab. While you may revise it after lab
(and before the problem set is due), the course assistant will want to
see this writeup as a part of lab check-in.
B. Finger exercises
A writeup of the finger exercises shouldalways be turned in along
with your completed laboratory. This week, you should also bring them to
check-in.
1. In Java, every expression has a type. Assume that the following declarations
apply:
int i, j, c;
double d;
short s;
long l;
float f;
boolean b;
For each expression below, if it is syntactically legal Java, indicate
its type (not its value). If it is not syntactically valid,
indicate why.
-
6
-
24L
-
+3.5
-
3.5f
-
2e-16
-
-25b
-
i
-
i+3
-
i+3.0
-
i+s
-
l+d
-
f+s
-
i / 0
-
4 * 3.2
-
i = 0
-
i == 0
-
'c'
-
"An expression in double-quotes"
-
"An expression in double-quotes" + "another one"
-
"6" + 3
-
!b
-
!i
-
b || true
-
i += s
-
s += i
-
i += f
-
l = i = s
-
i = l += s
-
l++
-
(long) s
-
s
-
(short) l
-
l
2. Assume that the following lines of code are executed in the order shown.Each
numbered line is a comment; on that line, give the value of each of the
variables indicated at that point in the execution.
C. Lab Preparation
This week's lab work has two goals. One is to teach you about Java and
to get you started designing programs. The other is to acquaint you with
the particular laboratory facilities that we'll be using this term. Your
lab preparation focuses exclusively on the first of these goals. We will
provide you with additional materials about the 6.096 lab when you arrive
in lab. (Those materials make much more sense when you're sitting at a
lab machine.) There is no preparation for the second -- lab-familiarity
-- goal.
The spirograph application that you'll be playing with this week is
a simple drawing application. There is a blank screen with a dot on it.
When the dot moves, it leaves behind a trail sort-of like the children's
drawing toy Spirograph.
The motion of the dot is controlled by two entities, one for each axis
(horizontal and vertical).These entities can potentially sense certain
properties of the dot -- e.g., its position or acceleration -- relative
to their own axis. Each of these entities follows a particular control
rule that tells it how to behave. The control rule is automatically invoked
by the application system; your job is simply to write down appropriate
control rules.
The form of a control rule is a sequence of Java statements ending in
a
(where double is some Java expression with type double).
The value returned by your control rule will be used as the new position
of the dot. (Motion will be smoother if you move the dot only a small amount
at a time.)When the spirograph window first opens, the screen will be approximately
400x400; the origin (0,0) is in the center of the screen. You should avoid
using these numbers explicitly, though; the name maxPos will be
provided (i.e., pre-declared) for you and the coordinates of your screen
will run from -maxPos to +maxPos.
Your job, in lab, is to write a series of behaviors that cause the spirograph
to display certain kinds of pictures. We will suggest a few to begin, but
we hope that you will find the environment interesting enough to try a
few of your own. You should read through the exercises below
and come up with preliminary designs for the code that will solve them.
There are also several places where you are asked to predict what your
code will do. Be sure to write up your predictions as well as your designs.
Bring these notes with you to lab. [There is far more in the lab
section than you should expect to do in lab. Do not worry about designing
solutions to all of them!]
The spirograph application has many advanced features that you will
use. For example, you can move the dot around (with your mouse) so that
it begins from a different position, or (using an advanced features dialog)
you can give the dot initial velocity. Some of these features are described
below; some are mentioned in the lab handout; and others are left for you
to discover for yourself. One specific feature involves a distinction that
you will need to make in lab: You can declare two different kinds of names
in your code. One is a temporary name that can be used during a single
application of your rule. These names can be declared anywhere in your
code. They are called variables. The other kind of
name sticks around from one use of your rule to another. These names must
be declared in a special box, separate from your rule code, but can be
used freely in your rule code. These names are called fields.
(You can also use name that have been pre-defined for you, like maxPos;
these are called parameters.)
Things to Try
This section walks you through a series of exercises of increasing complexity.
In future labs, you will have increasing responsiblity for designing the
progression from simpler cases to more complex ones. It is always a good
idea to build and test a simple version before going on to
add many features. Testing should be thorough, and designing good test
suites (sets of test cases) is a significant skill. Each time that you
add a feature, you should test your code again.
There is more listed here than you can reasonably get through in one
three-hour lab.
Static Positioning
-
Write Horizontal and Vertical rules that will place the ball in position
(10, 20). Try other coordinates as well, including negative ones.
-
Use the Horizontal rule for both rules. What do you expect would
happen?
-
Use the Horizontal and Vertical rules separately again. Use the mouse to
manually move the ball to another position (see Advanced Environment Options).
Do this several times. What happens? Explain. [In lab: Can
you "fix" this behavior?]
Implementing Velocity
We have pre-defined the name pos to hold the current position
of the dot (along the relevant dimension). Each time your rule is
used, pos will have the value at that time. (What value
will pos have if you assign to it?) Using this name, solve the
following problems.
-
Write a pair of rules to make the ball move horizontally from left to right.
Can you control how fast the ball moves (i.e., velocity) by changing your
code?
-
Use the horizontal rule for both rules. What do you expect would
happen? Explain. (When you get to lab, try it out and see if you're
right.)
-
What happens if the horizontal and vertical rules have different effective
velocities? (How do you make this happen?)
-
Use the mouse to move the dot to a different position. What happens?
Explain.
[Note: although the spirograph application seems to indicate that
there are velocity names, these names are not used in position-control
mode.]
Implementing Acceleration
-
Now that you can implement velocity using position controls, implement
acceleration. Note that you cannot use myVel and otherVel, since
these are always 0 in position-control mode. (Hint: use fields.)
Write your code so you can change the initial velocity and acceleration
by changing the code.
-
Can you make the dot go in a parabolic path? (Hint: what accelerations
does it need?)
You should try to prepare the lab up to this point. You should also
read through the exercises below, and if you are familiar with programming,
you should prepare at last some of them.
Wraparound and other boundary conditions
Try running the code you have so far in wrap-around mode and no-wrap-around
mode (using the advanced features), and observe its behavior.
-
Modify your code to make it emulate the behavior of wrap-around mode while
using no-wrap-around mode.
-
Can you make the dot bounce when it hits the end?
Other cool stuff
-
Implement a function plotter. Write code to plot the following:
-
y = x^2;
-
y = sin( x );
-
y = 1/x;
You may want to look at the Math library (see the on-line API
here
if you get serious....)
Using Velocity and Acceleration Controls
Although you can implement velocity and acceleration using position
controls alone, Spirograph is capable of doing this for you, and makes
it easier for you to play around with the effects of different code.
In the case of acceleration controls, you can think of the ball as a robot
with independent horizontal and vertical motors, and your rules as the
controls for its motors.
-
Play around with the velocity and acceleration-control mode. Play
around with bounce and no-bounce modes too.
-
Try the different position rules you wrote above (in particular, the static
positioning, velocity, and acceleration)
-
Write code that will draw a parabola.
Challenge: Write code that will draw a circle (given an appropriate
initial position and velocity). (Hint: remember a = v^2/r
from Physics.)
Laboratory
What to Bring to Lab
You should bring the letter to your pen pal, your finger exercises, and
a plan of action for the laboratory (including some thoughts on how to
solve the various problems described below). You should have read the entire
problem set before you arrive. Your notes from lab will form the basis
for your post-lab writeup.
Getting Started
Instructions on how to start and use the Spirograph problem set will be
available in lab and on-line. In addition, the lab handouts will include
information about some simple exercises designed to familiarize you with
the lab machines and the Java environment that we will be using. You should
allow time for these in lab.
During the lab session, you should work through the lab set-up exercises
and as much of the spirograph as you have time for. Details of the
spirograph are described in the Things to Try section, above; a supplementary
handout (describing how to run the spirograph) will be available in
lab.
Before you leave
Before you leave lab, you will need to have your code checked off by a
course staff member. You should allow time for an adequate demonstration
and discussion of what you have done. Please do not wait until the last
minute to be checked off.
Post-Lab, AKA What
To Turn In
Your completed assignment should include:
-
your pre-lab assignment, including the written
and finger exercises.
-
a brief description of your expectations before coming to lab. (This may
be a version of your notes for check-in.)
-
a discussion of how you spent your time in lab.
-
your observations concerning
-
what actually happened.
-
how this compares with what you expected prior to lab.
-
any interesting behaviors you may have developed.
-
a brief description of the check-off interaction, the name of the staff
member who checked off your laboratory
work, and answers to any specific issues s/he may have asked you to address.
-
the names and roles of any collaborators in any parts of the project.
also note the following:
-
How long did this lab take you?
-
Did you work with other students? If so, how helpful was it?
-
Did you have problems writing code for this lab?
-
Did you have problems running/using this lab?
Lab assignments are due on Mondays at the beginning of class. They
may, of course, be turned in earlier.
This course is a part of Lynn
Andrea Stein's Rethinking
CS101 project at the MIT AI Lab
and the Department of Electrical Engineering
and Computer Science at the Massachusetts
Institute of Technology.
Questions or comments:
<cs101-webmaster@ai.mit.edu>
Last modified: Thu Jul 10 13:01:20 1997