CSE 142
Spring'04
Assignment 2
Psychology Experiment
Due Dates:
BubbleMaker and LogoMaker classes due electronically Thursday night, April
15 at 10pm; paperwork (including "Prelude to Act II") in class (lecture)
the next day. Should you print the receipts? The turn-in forms
or the receipts themselves will tell you.
Final programs (experiment and trial series classes) plus text file,
due electronically Wednesday night, April 21 at 10pm; paperwork
in class (section) the next day.
Note: Exam #1 is Friday, April 23!
When you look at a geometric figure, does the shape or color
influence
how large you judge the figure to be? Psychologists investigate
perceptual questions like this by running experiments in which people's
reactions are observed, recorded and analyzed. In this
project, you will use your new programming skills to set up experiments
of this nature. As a preliminary step, you will implement two
Java classes for practice.
Please read the entire assignment word
for word all the way through
before you begin. It is important to read the entire
assignment through to the end, even the parts that are not due until
much later. Compared with the previous assignments, you are not
given as much detail about how to proceed. Try to
understand as much as you can before beginning. However, you
might not understand everything right away, some things may seem vague
or
incomplete. Don't let that bother
you too much! Read and re-read. Ask questions. Try
things. As you
make progress things will eventually make sense.
Overture
The first part of this assignment is to investigate
some library classes and methods. There is nothing to be
turned-in from
this part.
javax.swing.JColorChooser.
The class JColorChooser in the Swing library of Java allows a user to
select a
Color. It has a static method
showDialog. It needs three parameters, which can all be
null (i.e., use the special keyword null), and returns a Color.
Using DrJava's interactions pane, try
calling this method (sending a message).
(Don't forget to include an import statement
before you try using JColorChooser).
What happens? Can you figure out how to save the returned Color
to use later? If
you don't
understand something, feel free to ask questions about it. Write
a short (2-5 line) sequence of statements you can execute in the
interactions pane of DrJava to demonstrate what is happening. For
example, let the user choose a color, and then display a Rectangle of
that color in a GWindow.
If you are curious about what the parameters mean, locate the
documentation for the class (follow links from
the menu bar of the CSE142 home page).Can you guess what might be
good values for parameters two and three? (Leave the first
parameter as null). Try things out. Experiment!
uwcse.io.Input.
This class in the uwcse library has several methods for getting values
from a user. Find the documentation. Try out all of the
following methods (at least): readDouble, readDoubleDialog, readChar,
readCharDialog. Take
note of other methods which might be useful. Write short
sequences of statements you can
execute in DrJava to demonstrate what is happening with readDouble and
readDoubleDialog. See if you
can get the user to input a value which then you redisplay in an output
message. If you type in a value of the wrong type, what
happens?
uwcse.graphics.TextShape.
This class is similar to
uwcse.graphics.Rectangle and other shapes you have used. Try it
out in the DrJava interactions pane. See if you can get your name
to show up in blue letters in a GWindow. Can you change the color
to yellow without creating a new TextShape object?
java.lang.Math.
This class is mentioned several times in the textbook. Find the
Java API documentation for it and browse a bit. Write a short
sequence of
statements in the DrJava interactions pane which calculates the
following: given the area of a circle, what is its radius? Given
the area of a uwcse Oval and its width, what is its height?
Act I
BubbleMaker.
This is the first part you actually have to turn in. Create a
class called BubbleMaker which knows how to
draw small circles (bubbles) on a GWindow. Each bubble is green
and has an area of about 2000 pixels.
Write a constructor for BubbleMaker which takes no arguments
and displays a blank GWindow on the screen.
Write a method for BubbleMaker, named paintBubble,
which takes two parameters, the desired x and y
coordinates of the bubble (as pixels, exactly as for the shapes of
uwcse.graphics) and adds a new bubble to the window.
Implement the BubbleMaker class and use the interactions
pane of DrJava to try it out. That is, create a BubbleMaker
object, and then call paintBubble (send a message to the BubbleMaker object)
several times in a row to create
bubbles at different locations on the window.
If you're not sure where to begin, work on BubbleMaker away from the
computer. Start by writing the empty skeleton of a class (named
BubbleMaker), on paper. If you can do that, add in specifications
for the constructor and the method. Then add implementation
code. Write as much or little as you can and then take your work
to a course staff member, who can help you progress to the next step.
You will be asked to turn in the BubbleMaker.java file. As
mentioned above, it must have constructors and methods exactly as
described -- you can't use other names or spellings, or change or add
parameters, etc.
LogoMaker.
This is another class that will be turned in. A "logo" for this
problem is a graphic
consisting of a letter (single character) centered (more or less)
inside a
triangle. The top edge of the triangle must be parallel
to the x-axis.
Stop reading now and draw a sample logo on paper, given the
preceding short description.
The LogoMaker class helps a user design a logo. The logo is displayed
in a GWindow. There is only one logo visible. Some of
its characteristics can be changed, in particular: the letter being
displayed, the size (area) of the triangle, the color of the triangle,
and
the color of the letter. Objects of type LogoMaker can display a
logo on a GWindow, and also allow the user to select colors, choose the
size of the triangle, and specify a letter. The location of the
logo on the window cannot be changed by the user (the LogoMaker puts it
somewhere appropriate).
LogoMaker must have the following methods and constructors.
[Here, as elsewhere in CSE142 and in programming in general, a list
like this implies certain things that must be followed exactly, and
others that might leave you some freedom. For example, the name
of a method is always assumed to be an exact, unchangeable requirement,
case sensitive. Other such absolutes include whether or not
something is a command or query, what the return type is, and what the
types of the parameters are. The order of the parameters is
generally implied as well. On the other hand, the
exact names of the parameters are rarely considered an absolute
requirement when given in this style.]
- a constructor (with no parameters) which
sets the defaults as: blue letter, yellow
triangle, size (area) 3000, letter 'X'.
- a constructor which takes as parameters the letter color, the
triangle color, the size (area), and the letter. The constructor
parameters should be in the order just mentioned.
- a command paintLogo, which paints the logo using the current
values for colors, size, etc. Any past logo is erased first.
The method has no parameters.
- a command changeLetterColor which lets the user select a color
for the letter, and which then repaints the logo (using the new letter color,
of course). The method has no parameters. Hint: use JColorChooser.
- a command changeTriangleColor which lets the user select the
color for the triangle, and which then repaints the logo. The method has no
parameters.
- a command changeArea which lets the user specify the size (as
pixels) of the triangle, and which then repaints the logo. The
method has no parameters. For example, if the user wants the area to be
10000, and the triangle is equilateral, then what would the length of
each side be? The user enters the size (area), and the program
has to figure out dimensions and coordinates.. Hint: use
one of the uwcse.io.Input classes.
Begin by writing down a Java specification for the class. That
is, write a class definition which has specifications for all the items
mentioned above. Add instance variables that you think you might
need. Then begin to implement the methods, starting with the
default constructor. If at any point you get stuck, or don't know
how to proceed, ask for help immediately. The file
LogoMaker.java will be handed in.
Prelude to Act II
You should not wait until Act I is complete before reading and thinking
about Act II! To show that you have read and understood Act II,
answer the follow questions (on paper). This will be turned in
with with the paperwork for Act I.
- Draw (freehand) a picture of the GWindow as it might look when a
trial is being presented to the subject. Use colored markers (or write
the color names in English). For that picture, what is a
correct user input (answer)? Your picture doesn't have to be at
all beautiful, as long as it captures the essential idea (i.e., it
demonstrates that you know what is being asked).
- Draw another such picture, different from the first, that could
come from the same test
series, and state what its correct input is.
- Suppose the smaller area is 10000, and the "difference" (as
defined in the instructions) is 15. If the smaller figure
is a circle, what is its radius? If the larger figure is a
square, what is the length of a side?
Act II
The experiment is to operate roughly as follows. Note that this
is not programming specifications! It describes the people in
the experiment and what they are doing:
- The psychologist (person running the experiment, i.e., you) sets
things up (chooses the colors, etc.).
- The subject (person taking the test) sees a window with two
geometric figures, side by side.
The figures have slightly different areas.
They may be different colors.
They may be different shapes.
The subject must decide which of the two
figures is larger.
- One trial is the presentation of the figures and the
decision by the subject of which is bigger.
- A trial series consists of the subject completing five
such trials, each of which appears different (different shapes and/or colors,
etc.). The experiment reports a summary of the results.
Implement the following classes and methods. You may notice that
the specification for the classes above is less
specific than for BubbleMaker and LogoMaker. You may find
that
additional methods or constructors are needed. You may decide
that
return values and/or parameters are needed for some of the
methods. You will almost certainly decide that instance variables
are needed in one or both classes.
class Experiment
- Experiment (constructor): The constructor takes no
parameters.
- chooseColors: The psychologist chooses two colors to be used
for the shapes in the trial series.
- setAreaDifference: The psychologist specifies how close in
area the two figures are to be. This is given as a number
(double), which is the percentage difference between the larger and
smaller figure. For example, if the difference is 10.0, it means
that larger figure should be exactly 10% larger in area than the
smaller figure.
- reportResults: Using System.out, the Experiment object
displays the results of the last set of trials, in particular:
-- how many trials there were
-- how many choices were correct
-- how many choices were incorrect
-- the percent of correct answers
-- the colors used
-- the percent difference in area
- run: When this method is called, a complete experiment is run,
that is, the psychologist is asked to choose parameters (colors,
area difference), a trial series is conducted, and the results are
reported. This is a command (no parameters).
class TrialSeries
- TrialSeries (constructor): The colors to be used and the
percent difference are given as parameters.
- runTrials: Runs the complete series of trials.
It makes a better experiment if the subject cannot guess the answers in
advance. For example: don't use the same shapes every time.
Don't always have the colors in the same order. Don't always have
the larger figure on one particular side, etc.
A single trial, as described
earlier, consists of presenting the subject with two figures, asking
the subject which is larger, getting the user's answer, and remembering
the total right and wrong so far. There should be a message on
the GWindow telling the user which trial is underway (1 through
5). The user should be told to enter the character 'L' or
'R'. Any response other than the correct character is considered
to be a wrong answer. Be sure to clear the screen before each
trial!
- getCorrectCount: Reports how many correct answers have been
recorded so far.
- getWrongCount: Reports how many incorrect answers have been
recorded so far.
When the classes are complete, the following lines of code, typed in
the interactions pane, must be enough to run a complete
experiment. When grading, no exceptions to this requirement can
be made.
Experiment drPysch = new
Experiment( );
drPysch.run( );
For this part of the project, you will turn in the two (separate)
files, Experiment.java and TrialSeries.java. You will not be
able
to turn in additional Java files, or Java files with different names.
Note on robustness: A "robust"
program is one that operates reasonably even in the presence of
errors. For example, if the psychologist typed in an unreasonable
value
for the area difference (the number was negative, too tiny, or too
huge, or a
string was entered instead of a number) the program would notice this
and let the user enter another value. For assignment 2, your
program does not have to be robust! You do not have to check for
bad data. If your program blows up because of an input error (as
opposed to a programming error), it's OK -- this time!
Part 3 – Postlude
In this part of the assignment you will write about the work you have
done. Create a new text document
where you can put your answers to the
following questions (You can use Notepad or some other text
editor.) This file will be turned in. You must number
each section of the file with the corresponding number of the parts
below.
- Run the experiment at least twice "for real". That is,
get a couple of people who are not in CSE142 and let them go through
the trials. Write down the results. Then write a short "lab
report" giving the results and the experimental parameters (colors,
difference, etc.). There is no special format for this
report. One short paragraph, or perhaps a table, should
suffice. It should include the date that the experiment was done
and the names of the subject (or just their initiala), in addition to
the experiment results.
- Write one paragraph summarizing your experience working on the
programming part of this
assignment. You might mention positive things, frustrations,
discoveries, or
anything else you wish to share. If there was some part of the
assignment you were
unable to complete, please describe your difficulties with it.
Suggested Self-Study Exercises
As you go through the course, you should work as many of the
Self-Study Exercises in the textbook as you can. The answers for
all of them are given in the book. Here is a list of the
problems we particularly recommend working.. If you
can't get the answers on your own, feel free to discuss them on the
Message Board, or come and talk to any of the course staff about
them. (By the way, you may also find it helpful to work the
other, non-Self-Study Exercises as well, even though the answers are
not in the book. Any of these that are not assigned as homework
are fine to discuss on the Message Board).
Note: Do not wait until the second week of this Assignment to do
these! Chapter 4 is material we are covering in class during the
first
week of the Assignment period.
4.3, 4.4 (at least a-g), 4.5 (at least a-d), 4.5, 4.6, 4.8, 4.9,
4.11, 4.12, 4.13
12.9, 12.10
Turn in your homework using the turn-in form. These forms
will be linked from the Assignments page before the deadlines.
Read the forms
carefully for further instructions or clarifications they may contain.
Playtime: Characters and Fonts
Just for fun! You don't even need to read this unless you run out
of other things to do.
Unicode code pages.
Figure out how to display a
string or character in a non-Latin character set (such as Chinese,
Arabic, Klingon, etc.). The Unicode character set used by Java is
documented on the Unicode web site.
Look for the "code charts" link then find the block of characters
you
want.
Most blocks have the names of languages, but some are not so obvious
(Chinese, Japanese, and Korean characters are scattered among several
CJK blocks; the primary one is called "CJK Unified
Ideographs"). Find the character you want and note
its numeric value (ddddd).
In Java, you can indicate a Unicode character
with the special notation \udddd. This goes inside single or
double quotes, depending on whether you are after a Java char or a Java
String. In DrJava you can use a TextShape in GWindow to display
the characters (it almost certainly won't work on
System.out). If you get a
square box instead of a character, it means the default font used by
TextShape doesn't know how to display the character. If you know
a font on your system which does support Unicode, you can create a Font
object and
use the TextShape setFont method before displaying the character.
Many Windows systems include Arial Unicode, which should work.