Ch3 PTUDTT
Ch3 PTUDTT
Chapter Summary
Instance Variables
Constructor
Public Methods
int play( int If the play is legal and the board cell is not
row, int taken, plays and returns the old value of turn
column )
Class Description
Constructor
Public Methods
inner classes.
Class Description
OuterClassName.InnerClassName
number: 20
A a = new A( );
A.C c = a.new C( );
3.7 Layout Parameters: TicTacToe,
Version 3
We want to improve Version 2 by showing the
current state of the game at the bottom of the
screen, as shown in FIGURE 3.9.
GridLayout,BOTTOM, GridLayout.LEFT,
GridLayout.RIGHT, GridLayout.CENTER.
// vertical span
2 );
// horizontal span
2, 4 );
GridLayout.LayoutParams lp
columnSpec );
import android.widget.GridLayout.Spec;
in addition to:
import android.widget.GridLayout;
1 );
0, 3 );
GridLayout.LayoutParams lp
columnSpec );
method.
At lines 59–62, we center the text in status , set its
background color to green, set its text font size,
and set its text content based on the state of
tttGame , respectively.
status
▸ status , a TextView
AlertDialog.Builder( this );
alert.setPositiveButton( ”YES”, pd );
alert.setNegativeButton( ”NO”, pd );
view shows
View.OnClickListener {
}
}
Write an App
22. Write an app that displays one label and one
button. Every time the user clicks on the
button, the label toggles between being visible
and not being visible. Do not use XML.
23. Write an app that has three labels and one
button. The three labels represent a traffic
light. When the app starts, only the label with
the red background shows. When the user
clicks on the button, only the label with the
yellow background shows. When the user
clicks again on the button, only the label with
the green background shows. When the user
clicks again on the button, only the label with
the red background shows . . . and the cycle
continues. Do not use XML.
24. Write an app that displays one label and one
button. The label represents the current color
of a traffic light and can be red, yellow, or
green. When the user clicks on the button, the
label cycles to the next color and simulates
running the traffic light. Do not use XML.
25. Write an app that displays one label and one
button. Every time the user clicks on the
button, the label moves down by 10 pixels.
When the label reaches the bottom of the
screen, it no longer moves when the user
clicks on the button. Do not use XML.
26. Write an app that displays two labels and one
button. The first label should display a
randomly generated integer between 50 and
100. When the user clicks on the button, the
second label moves to a position whose y-
coordinate is the integer displayed in the first
label. Do not use XML.
27. Write an app that has one text field, one label,
and one button. The user can enter his or her
email in the text field. When the user clicks on
the button, the app checks if the email entered
contains the @ character and a dot
somewhere after the @ character. If it does,
the label displays VALID, otherwise it displays
INVALID in the label. The text field and the
label should have their own separate style,
each with a minimum of four attributes.
Include a Model. Do not use XML.
28. Write an app that has one text field and one
label. The user can enter his or her password.
The label displays, as the user types, WEAK
or STRONG. For the purpose of this app, we
define a weak password as having eight or
fewer characters. A strong password is
defined as having nine or more characters.
Include a Model. Do not use XML.
29. Write an app that displays a chessboard on a
grid with black and white pieces. You can
represent each piece by a letter or two, for
example, P for pawn, Q for queen, R for rook,
K for knight, B for bishop, and KG for king.
When the user clicks on a knight, color in
green the cells where the knight can move.
Include a Model. Do not use XML.
30. Write an app that displays four labels and a
button. The first two labels represent two
cards in a simplified blackjack game and are
filled with two randomly generated integers
with values between 1 and 11 inclusive. The
total of the two is displayed inside the fourth
label. When the user clicks on the button, if
the current total shown inside the fourth label
is 15 or less, the third label is filled with a
randomly generated number between 1 and
11 inclusive and the total inside the fourth
label is updated to equal the sum of the three
numbers in labels one, two, and three. If the
current total shown inside the fourth label is
greater than 15, nothing happens. Include a
Model. Do not use XML.