CS 1401, Exam #3
Date: Thursday, April 6, 2006
Class Section (9:0010:20, 10:3011:50):
______________________
Name (please write legibly, ideally in block
letters):
____________________________________________________________________
Question 1. Universal Building Materials Inc. (UBM), a building product manufacturing company, offers several models of countertops. Materials used include marble, granite, steel, and aluminum. Countertops are sold in bulk as 4 ft. X 4 ft., 4 ft. X 6 ft., or 4 ft. X 8 ft. sheets, in various designs, or in tiles that can be 4 X 4 squares or 4 X 8 rectangles (4 means four inches). Countertop designs may be contemporary, classic, Italian, and commercial. Commercial countertops have a heavy-duty design that is guaranteed to withstand mechanical and chemical abrasion, and up to 500-lb shocks and 50 lb. per sq. in. pressure. Contemporary and classic models are straight-edged, but Italian models are beveled and may have a straight or curved edge with round corners. A standard six-month warranty is offered for all countertops, but additional warranty can be purchased in one-year increments for up to 5 years. You are developing a custom software application for UBM that will be used by sales and distribution personnel.
(a) Draw an inheritance hierarchy for your application design.
(b) Write a CRC card for the class DesignCountertop (It is not necessary to list responsibilities.)
Question 2. The Cyber-Craps game from the Lucky Draw Casino is presented below. Cyber-Craps simulates rolls of dice to let the user play the game of craps. This release of the game lets the user decide how much to bet and shows the outcome of a new roll every time the user clicks on the Roll the Dice button. A future release of the game will include computation of payoffs based on bet amount and roll of dice outcomes. The figure labeled as Before shows the application GUI before the user does any interaction with the application. The figure labeled as After shows the GUI after the user enters a bet amount and clicks the Roll the Dice button at least once.

The next pages include the Java source code for Cyber-Craps with a few missing lines.
(a) Which layout manager was used to create the application window? Write the code needed to create and initialize the layout manager in the space below. Write an A in the application source code where this code should be placed.
(b) The code required to create and initialize one of the GUI components is missing from the application source code. Write the missing code in the following space and write a B in the application source code where this code should be placed.
(c) Write code to display in the GUI the component that you defined in (b) above. Write a C in the application source code in the place where your code should be included to place the missing component in the right location of the GUI.
(d) Write the code that is needed to allow users to terminate the application from the GUI. Write a D in the source code in the place where your code should be included.
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class LuckyDraw { JFrame ldFrame; Container ldPane; JLabel ldTitle; JLabel ldPrompt; JTextField ldBet; JLabel ldOutput; ActionHandler buttonAction; private class ActionHandler implements ActionListener { Random rand = new Random(); public void actionPerformed(ActionEvent
event) { String betStr
= ldBet.getText(); int
aDie; int
bDie; String outputStr="You
got "; aDie
= 1 + rand.nextInt(6); bDie
= 1 + rand.nextInt(6); outputStr
= outputStr + aDie +
" and " + bDie; ldOutput.setText(outputStr); } }
Source code for the Cyber-Craps application (top part of
source).
public LuckyDraw() { // Prepare
action handlers buttonAction
= new ActionHandler(); // Prepare the
text labels String titleStr
= "<html><center>Lucky Draw<br>
+ Casino<br><br>Cyber-Craps<br></center></html>"; ldTitle
= new JLabel(titleStr, JLabel.CENTER); ldPrompt
= new JLabel("Your Bet:", JLabel.CENTER); ldBet
= new JTextField(4); ldOutput
= new JLabel("", JLabel.CENTER); // Prepare the
main frame and content pane ldFrame
= new JFrame(); ldFrame.setSize(90,220); ldFrame.setTitle("LD"); ldPane
= ldFrame.getContentPane(); // Add items
to the contentpane and display ldPane.add(ldTitle); ldPane.add(ldPrompt); ldPane.add(ldBet); ldPane.add(ldOutput); ldFrame.setVisible(true); } public static void main(String[] args) { LuckyDraw
ld = new LuckyDraw(); } }
Source code for the Cyber-Craps application (bottom part of
source).
Question 3. The Lucky Draw Casino (LDC) is developing a gaming application architecture where one of the base classes is the Game class shown below. The purpose of LDC games is to allow players to play games where they can place bets, play, and receive payoffs. The performance of every player is tracked to determine the expertise level of the player. Expertise level in every available game is used to determine winning odds, accept bets, and compute payoffs when the player wins.
public class Game { float bet; int
expertise; float odds; public
Game(float aBet, int anExpertise, float anOdds) { bet = aBet; expertise
= anExpertise; odds = anOdds; } public void setBet(float aBet) {
bet = aBet; } public void setExpertise(int anExpertise) {
expertise = anExpertise; }
public void setOdds(float anOdds) {
odds = anOdds; } public float computePayoff() { return
bet * odds/expertise; } }
Write code to define the class BlackJack as a subclass of the Game class. For this subclass, include a class definition header, any instance variables needed, and a constructor that takes three arguments. Override any superclass method that must be implemented in a different way within the subclass. LDC gives Black-Jack players an initial expertise level of 1, .95 winning odds, and allows an initial maximum bet of $300. The highest expertise level for Black-Jack players is 7 and the maximum bet that can be taken is $10,000.
Question 4. The Lucky Draw Casino keeps a file with the names of players and the total amount played by every player at the casino. Write a piece of code that reads the file and prints on the screen the list of players that have played at least $50,000. Players in this list will receive a free weekend at the casino in August. The LDC marketing department expects 80% of those receiving the complimentary weekend to visit the casino in August. The name of the player data file is ldcPlayers.txt. For every player, the file has a line with the player name and address followed by a line with the corresponding played amount rounded to dollars. The first six lines of the data file look as follows:
Pete Spender,
89000
Sasha Cash,
200000
Bill C. Frills, 234 Side Ln.,
1500
Question 5. Modify your answer to Question 4 to print the names of players that have an associated played amount that is corrupt and causes NumberFormatException exceptions. Only write the code segment that needs to be modified and label with Q5 the place in your answer to Question 4 where the modified code should be placed.
Extra Credit. Modify the Cyber-Craps application in Question 2 to add a line that says You won X dollars! where X is equal to 10% of the bet. This line should be displayed every time one die shows a number that is twice the number shown by the other die. Write your code below and label the places in the Cyber-Craps source code where your code changes should be inserted. Use EC1, EC2, EC3 and so on for your labels.