Instruction 14 and homework

fullfattruckMobile - Wireless

Dec 10, 2013 (3 years and 6 months ago)

65 views

Instruction 14

and homework


1
4
.1 Ban
k

Replace the
CommandReader

from homework 4 with an activity that replaces the command
-
line
interface with a graphical user interface. Use a vertical
LinearLayout

to contain all the elements.
Use a
horizontal
LinearLayout

for each function of the command
-
line interface, where the
commands use an
EditText

for each element of user input, a
TextView

for output and a
B
utton

t
o execute the command. When a
B
utton

is pressed, the corresponding function in your
Bank

c
lass
must be executed.

Add an additional method to transfer money from one account to another.


Homework:

Reaction Test

You are going to build a program to experiment with reaction times.

The user is presented with a button with the text "wait". After some

time (the length varies), the button
shows the text "press".

The user has to press the button as quickly as possible, and then the button shows "wait" again.

The program runs five of these tests. At the fifth test, the program shows a really large button
with large
green text instead of a small one with black text. This will startle the user and has in many cases the
effect that the reaction time is a little bit shorter than the previous ones. An explanation is that in the
first reaction you make a conscio
us reaction, whereas in the startled reaction you react directly with the
brain stem. You can try to train yourself to use this type of reaction and be faster in sports and games.
Even after a few experiments with this program, you may observe this effect.

Make an app with
Button
s to get reaction from the user, and
TextView

to show the reaction times
and the average reaction time. The reaction time is the time in milliseconds between the moment the
test "press" is shown and
the moment the user clicks the
B
utton
. One experiment consists of five
tests. Each test consists of:

1.

showing "wait" in the
B
utton

2.

showing "press" in the
B
utton

after a random period of so
-
called delay time;

3.

showing the reaction time and starting a new test, except after the fifth test;

4.

a
fter the fifth test, the program is inactive and just showing the reaction times and their
average, waiting for the start
B
utton

to be pressed

Experiment with the delay time. When testing your program, it may be practical to use a short period as
a delay t
ime. In the final version, the delay should be longer and have a relatively large variation, to
make the reaction test harder. Think of a minimum of five seconds.

Hints



Create an
A
ctivity

with
a

Button

and a
TextView
. The
OnClick()

method must keep
track w
here we are in the process, so you need to introduce some useful variables for the
current test number etc.



The current time can be obtained via
System.
currentTimeMillis
()
.

This method
returns a
long

(a variant of
int

that can store larger values). So subt
racting two values gives
you the elapsed time.



Each time after a button press, the app must decide itself when (in the near future) to change
from "wait" tot "press" and to start the measurement. This can be done by starting a countdown
timer, and when thi
s timer expires: the change is realized. For such a countdown timer, you
have to do the following:

o

create a new class which inherits from class
CountDownTimer

o

add a constructor that not only takes the duration arguments but also an argument of
the class yo
u created for the reaction test, so you can call its methods

o

override the
OnFinish()

method, where you add the statements for the button text
transition



Use a different (random) delay at each test. The method call
Math.random()

gives a random
value between 0 (inclusive) and 1 (exclusive). By adding and multiplying, a random value
between any two numbers can be obtained. It returns a
double

value.



Pimp your button with methods
SetX()
,
SetY()
,
SetWidth()
,
SetHeight()
,
SetTextColor(
)
,
SetBackgroundColor()
.


Alternative homework: the Stroop effect

(extra points)


Make an app that resembles the experiment of
http://faculty.washington.edu/chudler/java/ready.html
.

For

the random waiting period before showing the first screen, use a class, say
DelayTimer
, that
extends
CountDownTimer
. Add a constructor that takes 3 arguments: the duration, the interval and
your class that implements the experiment, which is called
MyStroop

here. Do not forget to call the
super’s constructor. In the
onFinish
()

method of

DelayT
imer
, call a method
showNextScreen
()

of
MyStroop
. Choose the interval period larger than the countdown period

(why?)
.
Choose the interval at random with t
he met
hod call
Math.random()
which

gives a random
value between 0 (inclusive) and 1 (exclusive).

Multiply by 5000 and convert to an
int

or round it with
Math.round()
.


In the class
MyStroop
, use two arrays with appropriate indices to hold the colors that you want

to use
and the names of the colors.

Use the constants of the android.graphics.Color class.
Initialization with,
e.g.,
int[] colors = {Color.RED,…,Color.YELLOW};

resp.

String[] words = {“red”,…,”yellow”};

is a convenient way to initialize these arrays.

Use a two
-
dimensional integer arra
y of which the first dimension has size 2 and the second one has size
n*m if the
GridLayout

to display the colors has size n*m. Initialize this array with


int[][] indices = {{k
1
,k
2
,….k
n*m
}{l
1
,l
2
,….l
n*m
}};

The indices k
i

are the indices of the color names and colors to use in the first screen and the l
i

are the
indices of the colors to use in the second screen (make sure these are different from the corresponding
k
i

indices!).

In the class
MyStroop
, make a method
showStar
t
()

that shows a single
Button

in a
LinearLayout

to start the experiment

(with the method
setContentView
)
. Make a method
showReady
()

to start the
DelayTimer

and
to display a
TextView

with text “
G
et ready” in a
LinearLayout
. Make a method
showScreen
()

that
shows a
LinearLayout

(vertical!) with 1) a
GridLayout

to display n*m
TextView
s with text colors and strings to display indirectly given by the
indices

array,

2) a
B
utton

with text “finish”, 3) a
TextView

to display the durations and 4) a
Button

to continue with the experiment, visible only in the first screen. Record the times at which the
screen is displayed and when the finish
Button

is pressed with
System.
currentTimeMillis
()
.
Implement the
onClick

method to handle the various events.