|
FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG1: Programmierung I
Wintersemester 02/03
|
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 lab environment
and to begin to build a backkground in thinking the interactive programming way.
Be sure to read through the 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 VG 212 laboratory
- Thinking in terms of interactions
You
should read through this entire assignment and complete the Lab
preparation section before 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 Wednesday at 9.45 the
week of your next lab. For example, this means that if you have your lab
on October 9, then the writeup is due the next time you have a lab scheduled,
that is, October 23.
Contents
Pre-Lab
This week's pre-lab
has three parts. Future labs may 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 Programming 1 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. You can write in either
German or English, Martians can read either. 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 under 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), I will want to see this
writeup as a part of lab check-in.
B. Finger
exercises
A writeup of the finger exercises should always 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
The
goal of this week's lab is to teach you about Java and to get you started designing
programs. 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
or Etch-a-Sketch.
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 names
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 four-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. 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.
-
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 at the beginning of your
next lab class. They may, of course, be turned in earlier.
This course is an adaption for the
Fachhochschule für Technik und Wirtschaft
, Berlin
by
Prof. Dr. Debora
Weber-Wulff
of a part of
Prof. Dr. Lynn Andrea
Stein's
Rethinking CS101 project produced while she was at the MIT
AI Lab
belonging to the
Department
of Electrical Engineering and Computer
Science (EECS) at the
Massachusetts
Institute of Technology. The copyright for
all materials belongs to Lynn Andrea Stein, this adaptation is used
by permission. All rights reserved.
A textbook is in preparation by
Morgan Kaufmann Publishers
Questions or comments:
<weberwu@fhtw-berlin.de>