write comments for two java programs
CS 181 Homework-5 Dr. Chetan Jaiswal Spring 2018
Total: 55 points + Extra Credit (Optional 10 points)
Q1. Consider a two-by-three integer array t. (14 points)
a) Write a statement that declares and creates t.
b) How many rows does t have?
c) How many columns does t have?
d) How many elements does t have?
e) Write access expressions for all the elements in row 1 of t.
f) Write access expressions for all the elements in column 2 of t.
g) Write a single statement that sets the element of t in row 0 and column 1 to zero.
h) Write individual statements to initialize each element of t to zero.
i) Write a nested for statement that initializes each element of t to zero.
j) Write a nested for statement that inputs the values for the elements of t from the
user.
k) Write a series of statements that determines and displays the smallest value in t.
l) Write a single printf statement that displays the elements of the first row of t.
m) Write a statement that totals the elements of the third column of t. Do not use
repetition.
n) Write a series of statements that displays the contents of t in tabular format. List
the column indices as headings across the top, and list the row indices at the left
of each row.
Q2. (Game of Craps) Write an application that runs 1,000,000 games of craps (Fig. 6.8 of your
textbook) and answer the following questions: (10 points)
a) How many games are won on the first roll, second roll, …, twentieth roll and after
the twentieth roll?
b) How many games are lost on the first roll, second roll, …, twentieth roll and after
the twentieth roll?
c) What are the chances of winning at craps? [Note: You should discover that craps
is one of the fairest casino games. What do you suppose this means?]
d) What is the average length of a game of craps?
e) Do the chances of winning improve with the length of the game?
Q3. (Simulation: The Tortoise and the Hare) (10 points) In this problem, you’ll re-create the
classic race of the tortoise and the hare. You’ll use random-number generation to develop a
simulation of this memorable event. Our contenders begin the race at square 1 of 70 squares.
Each square represents a possible position along the race course. The finish line is at square 70.
The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce.
The course weaves its way up the side of a slippery mountain, so occasionally the contenders
lose ground. A clock ticks once per second. With each tick of the clock, your application should
adjust the position of the animals according to the rules in Fig. Use variables to keep track of the
positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (the
“starting gate”). If an animal slips left before square 1, move it back to square 1.
Generate the percentages in Fig by producing a random integer i in the range 1 ≤ i ≤10. For the
tortoise, perform a “fast plod” when 1 ≤ i ≤ 5, a “slip” when 6 ≤ i ≤ 7 or a “slow plod” when 8 ≤ i
≤10. Use a similar technique to move the hare.
Begin the race by displaying
BANG !!!!!
AND THEY'RE OFF !!!!!
Then, for each tick of the clock (i.e., each repetition of a loop or sleep timer-timing out every
second), display a 70-position line showing the letter T in the position of the tortoise and the
letter H in the position of the hare. Occasionally, the contenders will land on the same square. In
this case, the tortoise bites the hare, and your application should display OUCH!!! beginning at
that position. All output positions other than the T, the H or the OUCH!!! (in case of a tie) should
be blank. After each line is displayed, test for whether either animal has reached or passed square
70. If so, display the winner and terminate the simulation. If the tortoise wins, display
TORTOISE WINS!!! YAY!!! If the hare wins, display HARE WINS. YUCH. If both animals
win on the same tick of the clock, you may want to favor the tortoise (the “underdog”), or you
may want to display It's a tie. If neither animal wins, perform the loop again to simulate the next
tick of the clock. When you’re ready to run your application, assemble a group of fans to watch
the race. You’ll be amazed at how involved your audience gets!
Q4. (10 points Extra Credit, attempt only if you want extra credit)
a. Create a class TicTacToe that will enable you to write a program to play Tic-Tac-Toe. The
class contains a private 3-by-3 two-dimensional integer array where 1 represents X, 0 represents
available/empty, -1 represents O. The constructor should initialize the board elements to empty.
Allow two human players. Wherever the first player moves, place a 1 in the specified square, and
place a -1 wherever the second player moves. Each move must be to an empty square. After each
move, determine whether the game has been won and whether it’s a draw. If it is a win then print
which player won and if it’s a draw then reinitialize the array to empty and restart the game: this
could be done using a sentinel controlled loop. You are provided with a class
<TicTacToePrinter> which contains a method <printBoard(int array[][])> that takes an argument
of your array and prints the board. Use this method to print the board after every move.
b. Feeling ambitious, modify your program so that the computer makes the moves for one of the
players. Also, allow the player to specify whether he or she wants to go first or second. Use an
enumeration to represent the value in each cell of the array. The enumeration’s constants should
be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor
should initialize the board elements to EMPTY. Wherever the first player moves, place an X in
the specified square, and place an O wherever the second player moves. Each move must be to
an empty square. After each move, determine whether the game has been won and whether it’s a
draw. If it is a win then print which player won and if it’s a draw then reinitialize the array to
empty and restart the game: this could be done using a sentinel controlled loop. You are provided
with a class <TicTacToePrinter> which contains a method <printBoard(int array[][])> that takes
an argument of your array and prints the board. Use this method to print the board after every
move. If your original array is enum type then you would need to convert it into integer and then
use this method.
Q5. Write a method that reverses the order of elements in an ArrayList<E> without creating a
new ArrayList. You may only use the methods remove, add, and size. (For this problem you
could use ArrayList<Integer> or ArrayList<String>). Write a driver class and demonstrate the
working of your method. (5 points)
Q6. (6 points) Derive a class Lizard from Reptile (Reptile & Vertebrate is provided to you) with
private data members as int lizardLength, String lizardLocation. (lizardLocation values could be
tropical, dry, cold etc.). Add a 4 argument constructor in this class to initialize values of
brainSize, eggSize, lizardLength & lizardLocation. Override the toString method in this class
and make it return all the 4 data members as String. Create a main method inside this class and
create an object by initializing values for member data. Print this object using toString() method.
Output shall look like:
brain size = 0.9
egg size = 4.0
lizardLength = 10
lizardLocation = tropical
Q7. Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram,
Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. You are being
provided with a Point class, use it to represent the points in each shape. For e.g. the private
instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of
the Quadrilateral. Write a program that instantiates objects of your classes and outputs each
object’s area (except Quadrilateral). (10 points)
Quadrilateral:
Data: Point topLeft, topRight, bottomLeft, bottomRight;
Methods: Constructors, getters, setters, toString
All derived classes: Each derived class must have its own instance data like side for Square,
length & width for Rectange and so on.
Methods: Constructors, setters, getters, to String, get Area()
Instructions:
What to submit: 1 pdf file containing - All Source code + respective print screens
When to submit: by end of the day, May 6 2018.
Where to submit: Blackboard->Assignments
Format: PDF