Tema 1: Quiz Game
1. a. Create a quiz game that displays, in turn, five questions about any topic of your
choice. All five questions should have the same three possible multiple-choice
answers. For example, you might ask trivia questions about U.S. states for which
the correct response is either California, Florida, or New York. After each
question is displayed, allow the user to choose one, two, or all three answers by
selecting JCheckBoxes. In other words, if the user is sure of an answer, he will
select just one box, but if he is uncertain, he might select two or three boxes.
When the user is ready to submit the answer(s), he clicks a button. If the user’s
answer to the question is correct and he has selected just one box, award 5
points. If the user is correct but has selected two boxes, award 2 points. If the
user has selected all three boxes, award 1 point. If the user has selected fewer
than three boxes but is incorrect, the user receives 0 points. A total of 25 points
is possible. If the user has accumulated more than 21 points at the end of the
quiz, display the message “Fantastic!”. If the user has accumulated more than 15
points, display the message “Very good”, and if the user has accumulated fewer
points, display “OK”. Save the file as HedgeYourBet.java.
Tema 2 : Last Man Standing
a. Create a game called Last Man Standing in which the objective is to select the
last remaining JCheckBox. The game contains 10 JCheckBoxes. The player can
choose one, two, or three boxes, and then click a JButton to indicate the turn is
complete. The computer then randomly selects one, two, or three JCheckBox
objects. When the last JCheckBox is selected, display a message indicating the
winner. Save the game as LastManStanding.java.
b. In the current version of the Last Man Standing game, the computer might
seem to make strategic mistakes because of its random selections. For example,
when only two JCheckBox objects are left, the computer might randomly
choose to check only one, allowing the player to check the last one and win.
Modify the game to make it as smart as possible, using a random value for the
number of the computer’s selections only when there is no superior alternative.
Save the improved game as SmarterLastManStanding.java.
Tema 3: Llotaria
In Chapter 5, you created a lottery game application. Create a similar game using
check boxes. For this game, generate six random numbers, each between 0 and 30,
inclusive. Allow the user to choose six check boxes to play the game. (Do not allow
the user to choose more than six boxes.) After the player has chosen six numbers,
display the randomly selected numbers, the player’s numbers, and the amount of
money the user has won, as follows:
Matching Numbers Award ($)
Three matches 100
Four matches 10,000
Five matches 50,000
Six matches 1,000,000
Zero, one, or two matches 0
Save the file as JLottery2.java.
Tema 4 : Sammy’s Seashore Rentals
In previous chapters, you have created a number of programs for Sammy’s Seashore
Rentals. Now, create an interactive GUI program that allows the user to enter a rental
time in hours into a text field; if the value entered is not numeric, set the rental price
to 0. Also allow the user to choose one equipment type to rent from a group of seven
choices. The rental fee is $40 per hour for a jet ski or pontoon boat; $20 per hour for a
rowboat, canoe, or kayak; and $7 per hour for a beach chair or umbrella. Let the user
add an equipment lesson for an extra $5. Display a message that indicates all the
details for the rental, including the total price. Save the program as
JSammysSeashore.java.
Tema 5 : Mine Field game
a. Create a Mine Field game in which the user attempts to click 10 panels of a grid
before hitting the “bomb.” Set up a JFrame using BorderLayout, use the NORTH
region for a congratulatory message, and use the CENTER region for the game. In
the CENTER region, create a four-by-five grid using GridLayout and populate the
grid with JPanels. Set the background color for all the JPanels to Color.BLUE.
Randomly choose one of the panels to be the bomb; the other 19 panels are
“safe.” Allow the player to click on grids. If the player chooses a safe panel, turn
the panel to Color.WHITE. If the player chooses the bomb panel, turn the panel to
Color.RED and turn all the remaining panels white. If the user successfully
chooses 10 safe panels before choosing the bomb, display a congratulatory
message in the NORTH JFrame region. Save the game as MineField.java.
b. Improve the Mine Field game by allowing the user to choose a difficulty level
before beginning. Place three buttons labeled “Easy”, “Intermediate”, and
“Difficult” in one region of the JFrame, and place the game grid and
congratulatory message in other regions. Require the user to select a difficulty
level before starting the game, and then disable the buttons. If the user chooses
“Easy”, the user must select only five safe panels to win the game. If the user
selects “Intermediate”, require 10 safe panels, as in the original game. If the user
selects “Difficult”, require 15 safe panels. Save the game as MineField2.java.
Tema 6 : Catch the mouse
a. Create a game that helps new mouse users improve their hand-eye coordination.
Within a JFrame, display an array of 48 JPanels in a GridLayout using eight rows
and six columns. Randomly display an X on one of the panels. When the user
clicks the correct panel (the one displaying the X), remove the X and display it on
a different panel. After the user has successfully “hit” the correct panel 10 times,
display a congratulatory message that includes the user’s percentage (hits divided
by clicks). Save the file as JCatchTheMouse.java.
b. Review how to use the GregorianCalendar class from Chapter 4, and then
revise the JCatchTheMouse game to conclude by displaying the number of
seconds it took the user to click all 10 Xs. When the application starts, create
a GregorianCalendar object and use the get(Calendar.SECOND) and
get(Calendar.MINUTE) methods with it to get the SECOND and MINUTE values
at the start of the game. When the user has clicked all 10 Xs, create a second
GregorianCalendar object and get the SECOND and MINUTE values at the end of
the game. If the user starts and ends a game during the same minute, then the
playing time is simply the difference between the two SECOND values. Make sure
your application times the game correctly even if the start and stop times do not
occur during the same MINUTE. Save the file as JCatchTheMouseTimed.java.
c. In the JCatchTheMouseTimed game described in Game Zone exercise 2b, the
timer does not work correctly if the user happens to play when the hour, day, or
year changes. Visit the Java Web site to find out how to use the
GregorianCalendar class method getTimeInMillis(), and then modify the
game to measure playing time accurately, no matter when the user plays the
game. Save the file as JCatchTheMouseTimed2.java.
If you were writing a professional timed game, you would test the timer’s accuracy regardless of when
the user decided to play. For example, if the user played over the midnight hour on New Year’s Eve, you
would either have to test the game then (which is impractical), or reset your system’s clock to simulate
New Year’s Eve. If you are writing the programs in this book on a school’s computer network, you might
be blocked by the administrator from changing the date and time. Even if you are working on your own
computer, do not attempt to change the date and time unless you understand the impact on other
installed applications. For example, your operating system might assume that an installed virus-protection
program is expired, or a financial program might indicate that automatically paid bills are overdue.
Tema 7 : Corner the King game
The game Corner the King is played on a checkerboard. To begin, a checker is
randomly placed in the bottom row. The player can move one or two squares to the
left or upward, and then the computer can move one or two squares left or up. The
first to reach the upper-left corner wins. Design a game in which the computer’s
moves are chosen randomly. When the game ends, display a message that indicates
the winner. Save the game as CornerTheKing.java.
Tema 8 : Target Practice
Create a target practice game that allows the user to click moving targets and
displays the number of hits in a 10-second period. Create a grid of at least 100
JPanels. Randomly display an X on five panels to indicate targets. As the user clicks
each X, change the label to indicate a hit. When all five Xs have been hit, randomly
display a new set of five targets. Continue with as many sets as the user can hit in 10
seconds. (Use www.oracle.com/technetwork/java/index.html to find how to use the
GregorianCalendar class method getTimeInMillis() to calculate the time
change.) When the time is up, display a count of the number of targets hit. Save the
file as JTargetPractice.java.
Tema 9 : Concentration Game
You set up the card game Concentration by placing pairs of cards face down in a
grid. The player turns up two cards at a time, exposing their values. If the cards
match, they are removed from the grid. If the cards do not match, they are turned
back over so their values are hidden again, and the player selects two more cards to
expose. Using the knowledge gained by the previously exposed cards, the player
attempts to remove all the pairs of cards from play. Create a Java version of this
game using a GridLayout that is four rows high and five columns wide. Randomly
assign two of the numbers 0 through 9 to each of 20 JPanels, and place each of the
20 JPanels in a cell of the grid. Initially, show only “backs” of cards by setting each
panel’s background to a solid color. When the user clicks a first card, change its
color and expose its value. After the user clicks a second card, change its color to the
same color as the first exposed card, expose the second card’s value, and keep both
cards exposed until the user’s mouse pointer exits the second card. If the two
exposed cards are different, hide the cards again. If the two turned cards match,
then “remove” the pair from play by setting their background colors to white. When
the user has matched all 20 cards into 10 pairs, display a congratulatory message.
Save the game as JConcentration.java.
Tema 10 : Mine Sweeper game
Create a Mine Sweeper game by setting up a grid of rows and columns in which
“bombs” are randomly hidden. You choose the size and difficulty of the game; for
example, you might choose to create a fairly simple game by displaying a four-byfive
grid that contains four bombs. If a player clicks a panel in the grid that contains
a bomb, then the player loses the game. If the clicked panel is not a bomb, display a
number that indicates how many adjacent panels contain a bomb. For example, if a
user clicks a panel containing a 0, the user knows it is safe to click any panel above,
below, beside, or diagonally adjacent to the cell, because those cells cannot possibly
contain a bomb. If the player loses by clicking a bomb, display all the numeric values
as well as the bomb positions. If the player succeeds in clicking all the panels except
those containing bombs, the player wins and you should display a congratulatory
message. Figure 15-44 shows the progression of a typical game. In the first screen,
the user has clicked a panel, and the display indicates that two adjacent cells contain
a bomb. In the second screen, the user has clicked a second panel, and the display
indicates that three adjacent cells contain bombs. In the last screen, the user has
clicked a bomb panel, and all the bomb positions are displayed. Save the game as
MineSweeper.java.
Tema 11 : Card Game Lucky seven
2. Create an application that plays a card game named Lucky Seven. In real life, the
game can be played with seven cards, each containing a number from 1 through 7,
that are shuffled and dealt number-side down. To start the game, a player turns
over any card. The exposed number on the card determines the position (reading
from left to right) of the next card that must be turned over. For example, if the
player turns over the first card and its number is 7, the next card turned must be
the seventh card (counting from left to right). If the player turns over a card whose
number denotes a position that was already turned, the player loses the game. If the
player succeeds in turning over all seven cards, the player wins.
Instead of cards, you will use seven buttons labeled 1 through 7 from left to right.
Randomly associate one of the seven values 1 through 7 with each button. (In other
words, the associated value might or might not be equivalent to the button’s
labeled value.) When the player clicks a button, reveal the associated hidden
value. If the value represents the position of a button already clicked, the player
loses. If the revealed number represents an available button, force the user to click
it; that is, do not take any action until the user clicks the correct button. After a
player clicks a button, remove the button from play. (After you remove a button,
you can call repaint() to ensure that the image of the button is removed.)
For example, a player might click Button 7, revealing a 4. Then the player clicks
Button 4, revealing a 2. Then the player clicks Button 2, revealing a 7. The player
loses because Button 7 was already used. Save the game as JLuckySeven.java.
Tema 12 : Secret Phrase Game
In Chapters 7 and 8, you created a game named Secret Phrase in which the user
guesses a randomly selected secret phrase by entering one letter at a time. Now
create a GUI application that plays the game, allowing users to choose a letter
by selecting one of 26 buttons. (Hint: Consider creating an array of buttons
rather than 26 individually named buttons.)
Disable a letter button once it has been guessed, and after the puzzle is
complete, disable all the letters. Figure 16-42 shows a typical execution (1) after
the user has guessed an A, which is in the phrase; (2) after the user has guessed
a D, which is not in the phrase; and (3) after the user has completed the puzzle.
Save the file as JSecretPhrase.java.
b. Make the JSecretPhrase game more like the traditional letter-guessing game
Hangman by drawing a “hanged” person piece by piece with each missed letter.
For example, when the user chooses a correct letter, place it in the appropriate
position or positions in the phrase, but the first time the user chooses a letter
that is not in the target phrase, draw a head for the “hanged” man. The second
time the user makes an incorrect guess, add a torso. Continue with arms and
legs. If the complete body is drawn before the user has guessed all the letters in
the phrase, display a message indicating that the player has lost the game. If the
user completes the phrase before all the body parts are drawn, display a
message that the player has won. Save the game as JSecretPhrase2.java.
Tema 13 : Tic Tac Toe game
a. In Chapter 9, you created a Tic Tac Toe game in which you used a 2D array of
characters to hold Xs and Os for a player and the computer. Now create a
JFrame that uses an array of nine JButtons to represent the Tic Tac Toe grid.
When the user clicks a JButton that has not already been taken, place an X on
the button and then allow the computer to place an O on a different button.
Announce the winner when either the computer or the player achieves three
marks in sequence, or announce that the game was a tie. Figure 16-41 shows a
typical game in progress and after the player has won. Save the game as
JTicTacToe.java.
b. Add a graphic that displays a large letter representing the winning player of the
game in Game Zone exercise 1a. Draw a large X, O, or, in case of a tie, an
overlapping X and O in different colors. Save the game as JTicTacToe2.java.
Tema 14: Krijimi i nje fjalori multigjuhesh duke perdorur GUI
Tema 15: Krijimi i nje makine llogaritese ne GUI
Tema 16: Lights Out Game
Create the game Lights Out using a BorderLayout. Place a five-by-five grid of panels
in one region, and reserve another region for a congratulatory message. Randomly set
each panel in the grid to a dark color or light color. The object of the game is to force
all the panels to be dark, thus turning the “lights out.” When the player clicks a panel,
turn all the panels in the same row and column, including the clicked panel, to the
opposite color. For example, if the user clicks the panel in the second row, third
column, then darken all the light-colored panels in the second row and third column,
and lighten all the dark-colored panels in that row and column. When all the panels in
the grid are dark, all the lights are out, so display a congratulatory message. Save the
game as LightsOut.java.
Tema 17: Krijimi i nje loje “Stop Gate” me GUI
The game StopGate is played on a checkerboard with a set of dominoes; each domino
is large enough to cover two checkerboard squares. One player places a domino
horizontally on the checkerboard, covering any two squares. The other player then
places a domino vertically to cover any other two squares. When a player has no more
moves available, that player loses. Create a computerized version of the game in which
the player places the horizontal pieces and the computer randomly selects a position
for the vertical pieces. (Game construction will be simpler if you allow the player to
select only the left square of a two-square area and assume that the domino covers
that position plus the position immediately to the right.) Use a different color for the
player’s dominoes and the computer’s dominoes. Display a message naming the
winner when no more moves are possible. Figure 15-45 shows a typical game after the
player (blue) and computer (black) have each made one move, and near the end of the
game when the player is about to win—the player has two moves remaining, but the
computer has none. Save the file as StopGate.java.
Tema 18: Krijimi i nje ore dixhitale (ose analoge) duke perdorur GUI
Tema 19: Krijimi i nje parkingu duke perdorur GUI
Implementoni nje program ne Java per te menaxhuar nje parking ne nje qender
tregtare. Supozoni se parking ka forme matrice, me permasa N X M, dhe se ne çdo
qelize te tij mund te parkohet nje makine.
Kur arrin nje makine per tu parkuar:
makina fillimisht hy ne radhen e parkimit;
nese ka vend te lire ne parking makina parkohet;
ne te kundert, makina pret ne radhe.
Programi duhet te ofroje opcionet me poshte:
1. Parko: Kur arrin nje kerkese e re per parkim:
a. Programi fillimisht e ve kerkesen per parkim ne radhe.
b. Me tej, programi kontrollon a ka vende boshe ne parking. Ne rast se ka te
pakten nje vend bosh, ofron mundesine te zgjedhin nje nga vendet e lira.
Mbas zgjedhjes se qelizes se lire, parkohet makina e pare ne radhe dhe
qeliza kalon ne gjendjen e zene. Ne te kundert, njoftohet se nuk ka vende
parkimi te lira per momentin.
2. Çparko: Ne kete rast programi kerkon te dhenat e qelizes nga e cila duhet
çparkuar. Me tej programi çparkon qelizen duke e lene ate te lire. Programi shruan
gjithashtu sa kohe ka pritur makina per tu parkuar e sa kohe ka ndenjur e parkuar.
3. Konsulto: Ne kete rast programi jep te dhena statistikore mbi parking:
a. perqindjen e qelizave te lira
b. perqindjen e qeliza te zena
c. numrin e makinave qe presin ne radhe
4. Vizualizo: Ne kete rast programi vizaton parking (me nderfaqje grafike)
Shtoni nje opsion per paraqitjen grafike te gjendjes aktuale te parkingut.
Tema 20: Krijimi i nje konvertuesi binar decimal duke perdorur GUI
Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent. (Hint: Use the modulus
and division operators to pick off the “binary” number’s digits one at a time from right to left. Just as in the decimal number system where the rightmost digit has a positional value of 1 and the next digit left has a positional value of 10, then 100, then 1000, etc., in
the binary number system, the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then
8, etc. Thus the decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1
+ 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13.)
Tema 21: Krijimi i nje “Screen saver” me figura gjeometrike duke perdorur GUI
Dostları ilə paylaş: |