Assignments Java Chapter 4 by jcc96858


									                                       Assignments Java Chapter 4

   Notes on sections 4.0, 4.1, 4.2, 4.3, 4.5
   Multiple choice p. 237-240 #1-10
   True/false p. 240 #1-10
   Short answer p. 241-243 #1-8 (quiz), #9-17 (quiz), #18-22 (quiz)
   Key Concepts p. 236
   Self Review Questions p. 236-237 #1-12 (answers on p. 245-246)

#0 – Together make a class and its tester. Try different variations.

#1 – Use the given (revised) Die class. Do not change it. Name your new class DiceChoice. Write an
application that rolls a die and displays the result. Let the user pick the number of sides on the die. Result
should say
You rolled a 3 on a 6 sided die.

#2 –Make a BoxCars class that rolls two six-sided die objects 1000 times counting the number of box cars
(double sixes) that occur. At the end, print the number of box cars. Use a constant for 1000.

#3 – Write a class called Card that represents a standard playing card. Each card has a suit, face value, and a
point value. A Card should have a String for the suit, a String for the face, and an integer for the point value.
An ace is worth 1, a two through ten is worth that number, and a jack/queen/king is worth 10.

Make one private member function that picks the random suit, another private member function that picks the
random face, and a third private member function that figures out the corresponding point value. Call these set
functions from the constructor. Make a toString public member function that makes a String containing card
information in the following form: “King of Hearts has a value of 10”. Use Math class to generate the random

Make a driver class named ManyCards. It contains a loop that creates and prints 20 (nice constant) cards.
#4 – Make a class named Rectangle that has only 2 decimal data members called length and width. Make a
default constructor that makes the length and width both be 1. Make a 2 parameter constructor that accepts 2
doubles. Fill the data members by calling the set methods for each. Make the 2 set methods that fill the data
member with the incoming value if that value is above zero. If it isn’t above zero, fill the data member with the
default of 1. Make 2 get methods. Make a calcArea method that returns the area of the rectangle. Make a
calcPerimeter method that returns the perimeter of the rectangle. The area and perimeter are not stored in any
class variables.

Make a new class called RectangleTester and write code to test every Rectangle class method. Hardcode in
valid and invalid set values and constructor values.

#5 – Two files are provided to you. The Elevator class file has a description of everything that needs to be
done for the class. This is where you will write code. The ElevatorTester class file does the complete testing
of all your code. Do not change this.

#6 – Write a new class called Pig that will be using the Die class. In this game, the user competes against the
computer. On each turn, the player rolls a pair of dice and adds up his points. Whoever gets 100 points or more
first, wins. If a player rolls a 1, he loses all points for that round and the dice go to the other player. If a player
rolls two 1s, the player loses all points earned so far in the game and loses control of the dice. The player may
voluntarily turn over the dice after each roll. So the player must decide to either roll again (be a pig) and risk
losing points, or give up the dice, possibly letting the other player win. The player stops rolling when he wishes
to not be a pig or has the 100 points needed to win the game. The computer stops rolling dice when has 20 or
more points in round or has the 100 points needed to win the game.

Display the player’s total points and the computer’s total points before each round. Give the round total after
each roll. Have constants for 20 and 100. Try to make the code for the user’s turn be as similar as possible to
the code for the computer’s turn.

To top