8 Pages

asmt7

Course: CS 101, Fall 2009
School: Vassar
Rating:
 
 
 
 
 

Word Count: 2093

Document Preview

Fall CMPU-101-02, 2008 Asmt 7 Due: Monday, Nov. 17 @ 1:30 p.m. For this assignment, you will design a more complete Yahtzee game using object-oriented programming. A big difference will be that you will take care of scoring. In addition, you will provide a toString method, as described in the text. A QUICK REVIEW OF THE RULES OF THIS MINI-VERSION OF YAHTZEE. A game consists of six turns. Each turn is...

Register Now

Unformatted Document Excerpt

Coursehero >> New York >> Vassar >> CS 101

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Fall CMPU-101-02, 2008 Asmt 7 Due: Monday, Nov. 17 @ 1:30 p.m. For this assignment, you will design a more complete Yahtzee game using object-oriented programming. A big difference will be that you will take care of scoring. In addition, you will provide a toString method, as described in the text. A QUICK REVIEW OF THE RULES OF THIS MINI-VERSION OF YAHTZEE. A game consists of six turns. Each turn is begun by rolling all of the dice. The player then has two more chances to roll some or all of the dice. Each time, the player chooses zero or more dice to re-roll, and then rolls them. After completing the dice-rolling phase, the player must score the hand. A Yahtzee score sheet contains six slots, numbered 1 through 6. Each of the slots represents a scoring category. Initially, all the slots are empty. After each turn, as described above, the player must choose a category for scoring the hand. Once a category receives a score, it cannot be changed or used again. Thus, after six turns, the score sheet will be completely filled out and the game is over. The score for the game is simply the sum of the scores in the six categories. The choice of category determines how the score for a given hand will be computed. For example, suppose the dice are: 2 2 4 5 2. Scoring this hand in category 2 yields a score of 6 points (i.e., 3 * 2, since there are three 2s in the hand). However, scoring it in category 4 yields a score of 4 points (i.e., 4 * 1, since there is only one 4 in the hand). However, scoring the hand in category 6 yields a score of 0 points (i.e., 6 * 0, since there are no 6s in the hand). Here's how a game might proceed: Initially, score sheet is empty: _ _ _ _ _ _. First turn: Initial dice are: 2 2 4 1 3. Re-roll the last three dice: 2 2 5 2 6. Re-roll the 5 and 6: 2 2 3 2 5. Choose category 2; the score is 6 points (i.e., 2 * 3). Score sheet now looks like this: _ 6 _ _ _ _. Second turn: Initial dice are: 1 2 4 3 6. Re-roll first four: 2 5 6 1 6. Re-roll 2,5,1: 3 4 6 2 6. Choose category 6; the score is 12 points (i.e., 6 * 2). Score sheet now looks like this: _ 6 _ _ _ 12. Etc. NOTE For this assignment you will define a class called Yahtzee that implements the functionality for a game of Yahtzee as described above. Your class will include the following object-oriented methods: sumScores -- sums the scores in all the categories rollDice -- rolls the indicated dice scoreHand -- computes score for the hand using the indicated category; enters the score into the appropriate slot in the score sheet resetGame -- resets the game in preparation for a new game resetDice -- resets the dice in preparation for a new turn toString -- returns a String representation of the current state of the game (e.g., the dice, the number of rolls left, the current score sheet). In addition, you will define a static method, validKeeperString, that returns true if the input String has five characters, each of which is either 'r' or 'k'. Finally, you will define a constructor method (that simply calls the resetGame method). To see how your methods should behave, have a look at the sample interactions file, asmt6-inters.txt, provided in the assignment directory. Below, you will be guided through the process of defining these methods. Initially, they won't print out as much information as you see in the sample Interactions Window session; however, by the time you finish, they should generate results that are essentially the same as what you see in the sample session. GETTING STARTED Define a new class, called Yahtzee. Be sure to include comments that clearly partition the body of your class into the following categories: ** the object-oriented data structure ** the constructor(s) ** the object-oriented methods ** the class-oriented methods OBJECT-ORIENTED DATA STRUCTURE The object-oriented data structure should contain the following fields: ** an array of six ints representing the score sheet NOTE: Be sure to initialize the array so that each slot is set to -1. We will use the value -1 to indicate that a category is available for scoring. (Remember, a slot might receive a score of zero during the game; so initializing the array with zeros would not be good.) ** an array of five ints representing the dice ** an int called numTurnsLeft that represents the number of turns the player has left in the game ** an int called numRollsLeft that represents the number of rolls the player has left in the current turn ** a boolean called currentHandScored that keeps track of whether or not the player has entered a score for the current hand in the score sheet CONSTRUCTOR METHOD You need supply only one constructor method. It should not take any arguments. It should simply call the "resetGame" method, described below. (To enable your code to compile successfully, you should comment out the call to resetGame until you have actually defined it. That way you can test your code, even though it's not complete.) STATIC METHOD validKeeperString -- This method should take a single String as its only input. It should return boolean true if that String contains exactly five characters, each of which is either 'k' or 'r' (representing "keep" or "re-roll", respectively). NOTE: You can test this method in the Interactions Window even if you haven't written any other methods yet. > Yahtzee.validKeeperString("abc") false > Yahtzee.validKeeperString("rkrrk") true OBJECT-ORIENTED METHODS rollDice ... This method should take a single String as its only input. It should first call the validKeeperString method to determine whether the given String is valid. If not, then it should print out a warning message. Next, this method should check whether the player has any rolls left. If not, it should print out a warning message. On the other hand, if the keeper string is valid and the player has at least one roll left, this method should: ** Print out a message saying that the dice are being rolled! ** Re-roll the dice indicated by the keeper string. ** Decrement the number of rolls left. ==> For now, the return type of this method can be void. Later on, we'll make some changes. NOTE: You can test this method in the Interactions Window, as follows. > y = new Yahtzee() > y.numRollsLeft = 2; > y.dice { 0, 0, 0, 0, 0 } > y.rollDice("rrrrr") Rolling... > y.dice { 3, 2, 6, 6, 5 } > y.rollDice("rrkkr") > y.dice { 4, 3, 6, 6, 1 } > y.rollDice("rrkkr") Hey! No more rolls left!! resetDice -- This method should do the following, in order: ** set the number of rolls left to 3; ** roll all of the dice; ** set the currentHandScored field to false. Its return type should be void. NOTE: You can test this method in the Interactions Window, as follows. > y = new Yahtzee() > y.dice { 0, 0, 0, 0, 0 } > y.resetDice() > y.dice { 2, 1, 3, 4, 5 } > y.numRollsLeft 2 Notice that the value of numRollsLeft has been decremented by the act of rolling all the dice. resetGame -- This method should reset the game in preparation for a new game of Yahtzee. In particular, it should: ** Print out a message saying that the game is being reset ** Set each of the slots in the score sheet to -1 (indicating that they are available for use) ** Set the number of turns left to 6 ** Call the resetDice method. The return type of this method should be void. NOTE: You can test this method in the Interactions Window, as illustrated below. > y = new Yahtzee() > y.resetGame() Resetting game... Rolling... > y.scores { -1, -1, -1, -1, -1, -1 } > y.numTurnsLeft 6 > y.numRollsLeft 2 > y.dice { 4, 1, 4, 1, 5 } =====> After you have written this method, change the body of the constructor method so that it calls the resetGame method! sumScores -- This method should add all of the LEGITIMATE scores in the score sheet. In particular, it should sum only those entries in the scores array that are positive or zero; it should IGNORE the entries that are -1. This method should return the computed sum (an INT). NOTE: You can test this method in the Interactions Window as illustrated below. > y = new Yahtzee() Resetting game... Rolling... > y.scores { -1, -1, -1, -1, -1, -1 } > y.sumScores() 0 Notice that the answer is zero, not -6. toString -- Before defining this method, check out the following Interactions Window session: > String stree = "hi" > stree = stree + ":bye" "hi:bye" > stree = stree + ":" + 84 + ":" "hi:bye:84:" > stree = stree + "so long!!" "hi:bye:84:so long!!" Notice that the stree variable initially referred to the String "hi", but then we successively concatenated other Strings onto it. At the end, stree refers to the String "hi:bye:84:so long!!". If we want to, we can ask println to print out stree: > System.out.println(stree) hi:bye:84:so long!! Okay, back to the toString method.... The definition of this method should begin with the keyword public, as described in the text. It should not take any inputs, and it should return a String. That String should be constructed by concatenating various bits of information just like in the preceding example. However, keep in mind that you should RETURN the String, not print it out!! Here's an example of what should happen when you call this method explicitly: > y.toString() "Dice: 4 6 5 1 5 ; numRollsLeft = 2 ; Scores: _ _ _ _ _ _ ; Total Score: 0" Notice that the return value is (a reference to) a String obejct. Once you have defined this method, you can make drJava call it automatically, as follows: > y = new Yahtzee() Resetting game... Rolling... > y Dice: 4 6 5 1 5 ; numRollsLeft = 2 ; Scores: _ _ _ _ _ _ ; Total Score: 0 Notice that now, because you have defined a toString method, drJava uses it to describe the Yahtzee object that y refers to. (Before, drJava would just have displayed a reference such as Yahtzee@1a204e.) scoreHand -- This method should take an integer input that represents one of the scoring categories. It should check several things before computing the score: ** The category should be an integer from 1 to 6 ** The corresponding slot in the scores array should be -1 indicating that it is available. ** The number of turns left should be positive, indicating that there is at least one blank category. ** The currentHandScored field should be false, indicating that the current hand has not yet been scored. (You only want to score each hand once.) If all these...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Vassar - CS - 295
; -; CMPU-295, Spring 2009; Code used in class; Apr. 1; -; Propositional Logic in Scheme; We'll represent sentences in PROPOSITIONAL LOGIC; using SYMBOLS for propositional letters and; LISTS of the form (NOT S), (S1 AND S2), (
Vassar - CS - 295
; -; CMPU-295, Spring 2009; Code used in class; Apr. 6, 2009; -; This file contains the stuff on Propositional Logic; introduced on Apr. 1, as well as the additional stuff; we did in class on Apr. 6; -; Recall:; We represent s
Vassar - CS - 245
; =; CMPU-245, Spring 2009; Code used in class; =; Implementing Arithmetic in the Lambda Calculus; First, a note about Scheme and "currying" of inputs.; FUNC takes two inputs at once.; (e.g., (func 3 4) )(define func (lambda (
Purdue - MA - 315
MA 315, Spring 2009 Homework 11 Due Tuesday, April 14, 2009 Prove the following. 1. Let f : X Y be a function. Assume A and B are subsets of Y . Prove or disprove: f -1 (A B) = f -1 (A) f -1 (B).2. Let f : X Y . For each element b Y , let Yb =
Auburn - DIGDATA - 1903
149REPORT OF THE PRESIDENTto theTrustees of the Alabama Polytechnic Institute, June 8, 1903 The friends of the college and of education have cause for gratification at the continued growth and prosperity of the institution, a prosperity the
Idaho - CH - 310
Auburn - AG - 1987
Conservation Tillage: Today and Tomorrow Southern Region No-till ConferenceProceedingsJuly, 1987 College Station, Texas The Texas Agricultural Experiment Station, Neville P. Clarke,Director, The Texas A&M University System, College Statio
Auburn - AG - 1990
1990 Southern Region Conservation Tillage Conference Steering Committee J. S. Bacheler, Department of Entomology, NCSU C. G. Bowers, Department of Biological and Agricultural Engineering, NCSU B. G. Brock, Soil Conservation Service M. G. Co
Auburn - AG - 1985
169 Effects of Tillage on Quality of Runoff WaterP. L. Baldwin, W. W. Frye, and R. L. BlevinsDepartment of Agronomy, University of Kentucky, Lexington, KY 40546The i s s u e of water q u a l i t y i s o f g r e a t importance t o a g r i c u l
Auburn - AG - 1987
PrefaceWe recognize the importance of tillage in modern agriculture especially in respect to ameliorating biological, chemical, and physical soil impediments to crop growth. Modern tillage practices have contributed to the unmatched productivity of
Allan Hancock College - CS - 9311
namespecialtiesssnPersonaddressqualificationPrimary PhysicianoDoctor TreatsPatientPharmacistManagesWorksIn when tradenamePrescribesDrugSoldInPharmacyyearsExpwhenquantityformulaprice
Allan Hancock College - CS - 9311
SQL: QueriesexampleDB-1SQL Query LanguageAn SQL query consists of a sequence of clauses: SELECT FROM WHERE GROUP BY HAVING projectionList relations/joins condition groupingAttributes groupConditionWHERE, GROUP BY, HAVING clauses are optional.
Allan Hancock College - CS - 9311
EII DB Day 2007Database Group @ UNSW, Australia 2 October 2007Sponsered by ARC Research Network in Enterprise Information Infrastructure (EII)Database Group School of Computer Science and Engineering University of New South Wales Sydney 2052, NS
Vassar - CS - 240
Reducing One Undecidable Problem to Another We say a probem A is reduced to problem B is the decidability of A follows from the decidability of B Then, if we know A is undecidable, we can conclude that B is also undecidable The typical case is to
Allan Hancock College - CS - 9311
Data Modification in SQLSQL provides mechanisms for modifying data (tuples) in tables: INSERT . add a new tuple into a tableSQL: Updates DELETE . remove tuples from a table (via condition) UPDATE . modify values in exiting tuples (via conditio
Allan Hancock College - CS - 9311
RDBMS Architecture What is an RDBMS?A relational database management system (RDBMS) is software designed to support large-scale data-intensive applications allowing high-level description of data (domains, constraints) with high-level access to t
Allan Hancock College - CS - 9311
Data Modication in SQLSQL provides mechanisms for modifying data (tuples) in tables: INSERT . add a new tuple into a tableSQL: Updates DELETE . remove tuples from a table (via condition) UPDATE . modify values in exiting tuples (via condition)
Allan Hancock College - CS - 9311
Future of DatabaseCore "database" goals: deal with very large amounts of data (terabytes, petabyes, .) very-high-level languages (deal with big data in uniform ways) query optimisation(evaluation too slow useless)Future of Database(cont.)
Allan Hancock College - CS - 9311
RDBMS Architecture What is an RDBMS?A relational database management system (RDBMS) is software designed to support large-scale data-intensive applications allowing high-level description of data (domains, constraints) with high-level access to t
Allan Hancock College - CS - 9311
Data ModellingData modelling: an important early stage of database application development (aka "database engineering"). 1. requirements analysis 2. data modelling(identify data and operations)(high-level, abstract) (detailed, relational/tables)
Purdue - CS - 251
z y o &X' VB y 8' c I d 2 02 2 z y {# o &X' VB VB cvrfHgs I j I d ctp n rp n j d 3# o s s q p 2 nl j VB I ' Tv' cvg& Tv{v8 v j fC o d y ' } c d c H U fu & vy Qo U c P d d y # 8 n c& fd BI r QY vgd 2 0~Tv' c D s y 8 ' 2 ' c y 8'2 y'
Purdue - CS - 514
CS514 : Numerical AnalysisHomework 4 Solution Problem 1 The attached Matlab computes f(0) and f(0) using divided differences and origin shifts. The values were found to be f(0) = 0.6788 f(0) = -0.2876 Problem 2 We approximate the step function f(t
Purdue - MA - 490
MA49000 HOMEWORKASSIGNMENT 8: SELECTED PROBLEM SOLUTIONSProblem 4, Ch. 5. If g is a smooth function on R, define the formal power series(1)u(x, t) =n=0g (n) (t)x2n . (2n)!(a) Check formally that u solves the heat equation. (b) For a >
Vassar - CS - 335
Using Design Patterns to Elaborate upon Design ModelsMoving from Analysis to DesignExamine Design models and initial code to:Improve cohesion Reduce coupling Enhance ReusabilityGoF Design Patterns should be used to rewrite code to promote these
RPI - MB - 2400
RPI - MATH - 2400
RPI - MATH - 2400
Purdue - STAT - 416
Name Student ID # SectionSOLUTIONSTAT/MATH 416 Spring 09 Practice Midterm #2 April 9, 2009You are not allowed to use books or notes. Calculators approved for the actuarial exams are permitted. The last page contains a table for the cumulative di
Purdue - STAT - 416
STAT 416 Spring 09 Section 001 Homework 10 Due: April 16, 2009, 9:00amApril 8, 2009Please explain how you arrived at your solution, don't just write down the final number. Reading: Chapter 6.5,6.7, 7.1-7.2 Exercises: problems 6.51, 6.52, 6.54, 6.
RPI - KU - 4800
RPI - KU - 4800
RPI - KU - 4800
RPI - KU - 4800
RPI - KU - 4800
RPI - ECSE - 35201
35201 SAMPLE FINAL EXAM 5/6/97 This exam has two parts. There are 8 problems weighted as shown. Put your answers in the spaces provided. Include units where appropriate. SHOW YOUR WORK! Please print your name on EVERY page, including any attachments.
RPI - ECSE - 35201
35201 FINAL EXAM Take-Home Part (Problems 1-4) In-Class Part (Problems 5-8)SAMPLE EXAMPROB. 1 (15 points) i 10 mA 0.4 F 20 k 5 k t=0 15 k i(0-) = 8 mA, i(0+) = 2 mA, Iss = 5 mA, = 4 ms PROB. 2 (10 points) Given H ( s) = , plot and label the pole
TCNJ - CSC - 220
Csc220 p. 195 2.Self-check answerspart 2a. a = (b + a b) 3rd 1st 2nd b. (c = (a + b) | flag 2nd 1st 3rd c. (a != 7) & (c >= 6) | flag 1st 3rd 2nd 4th d. !(b <= 12) & (a %2 = 0) 4th 1st 5th 2nd 3rd e. !(a > 5) | (c < (a + b) 5th 1st 4th 3rd 2nd
Purdue - STAT - 598
rnorm.ar = function (n) {# generating a sample of size n from normal N(0, 1) using the # acceptance-rejection method. x = rep(0, n) R = sqrt(exp(1)/2/pi) for(i in 1:n){ while(TRUE){ # Step 1. Y = -log(runif(1)
RPI - P - 0591
ANNEXWPP: A Dynamic decision making taskIn WPP the decision maker plays the role of an operator of a Water Purification Plant, whose goal is to distribute water to different locations on time. To do the task, the decision maker activates/de-activa
Auburn - DIGDATA - 1867
72ON MOTION, RESOLVED, That a committee of three be appointed to make an appealto the public in behalf of the College and also to present to the Montgomery District meeting in September the present status of the College and its claims upon public
UNLV - ECG - 495
1. open up simplescalar.xla file2. press simpledata toolbar button in excel3. choose directory in which all benchmark output ".txt" files are located4. choose which values you want the program to grab (separated by commas).Example: "sim_num_i
UNLV - CS - 370
Operating Systems CS 370 Spring 2006 Project #1 Due: Thursday, February 2, 2006 DOS to Linux Command Shell Interpreter Total points: 100Teaching Assistants: Meghna Sharma and Ai Yamazaki Office Hours in TBE B-361: MW - 10:30 - 11:30 AM and 2 - 3 PM
UNLV - CS - 472
CS472 Software Product Design ISpring 2008 Topic PresentationObjectives: Students will practice oral communication skills Students will practice written communication skills Students will specialize in a software engineering topic and learn addit
RPI - COMM - 6820
Maureen Duffy <duffy@rpi.edu> COMM 6820: Foundations of HCI Usability Assignment #2 Interface Metaphors AN EVALUATION OF WINAMP'S STEREO COMPONENT METAPHOR INTRODUCTION Winamp 3 is a free media player downloadable from winamp.com. It was originally
TCNJ - SPED - 506
VISION STATEMENT:CommunicationWe are committed to insuring that our technology effectively supports and encourages communication between teachers, students, parents, and administrators to establish a greater sense of community and ownership in the
Centenary LA - REL - 320
Virtual Tour of Ancie Myce nt naePhotographs by Margare t-AnneGillis, BarrieCe ntral Colle giate , Barrie Ontario , and Elizabe Ellison, th Elm wood S chool Ottawa, Ontario CANADA1Myce , se n fromthevalle surrounding it. nae e yPhoto: Elizabe E
TCNJ - PHY - 466
TCNJ - PHY - 466
APPENDIX A1. Trigonometric DistancesThe distance of an object may be found by measuring an angle called parallax. Parallax is defined to be the angular displacement of an object when viewed from two different positions that are located along a line
TCNJ - PHY - 416
CHAPTER 44.1 Work We define work to be the interchange of energy between a system and it surroundings under the action of a force. In general: = where is the angle between the force vector and the direction of the displacement ds. In thermodynamics
TCNJ - AST - 161
ASTR161 TEST 2, March 4, 2009Part I. Multiple Choice01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. B E M V J N D C E H V A B V J D B B A D B A C C D A A D D E B C B E B A28. 29. 30. 31
TCNJ - PHY - 416
D06-2, 6-3, 6-4a, 6-17Section 6-9. Properties of a solid or liquid under hydrostatic pressProblem 6-22 in S&S was done as an example in class. Do 6-22D.
TCNJ - PHY - 466
CHAPTER 1 THE OBSERVATIONAL PROPERTIES OF STARS1-1. PhotometryPhotometry involves the measurement of the properties of light or electromagnetic radiation in general. More specifically, it deals with measuring the brightness of a light source, wheth
TCNJ - PHY - 466
CHAPTER 4B4-4 OpacityWe now turn to the matter of determining what contributes to the optical depth of a medium, such as the layers of a star. The theory here can be quite complex and a thorough exposition would take us deep into quantum mechanics.
Auburn - MECH - 4240
Gang Saw GangMohammad Jeelani Richard Thrift Barton Pate Michael Knoblett Jeremy Patterson Mark Herndon Justin Owens Joseph Perry Mike HudgensProblemTo design a reliable machine to cut various sizes of scrap wood into survey stakes of specific di
Auburn - ELEC - 6740
ELEC 6740: Electronics Manufacturing Chapter 12: Soldering of Surface Mount ComponentsR. Wayne Johnson Alumni Professor ECE Department, Auburn University 334-844-1880 johnson@eng.auburn.eduSubjects! ! ! ! ! ! !Introduction Wave Soldering Vapor
Auburn - MECH - 7300
MECH 7300: Fracture Mechanics (Spring 2002) Instructor: Hareesh V. Tippur, Professor of Mechanical Engineering Office: Ross 335 Phone: 844-3327 e-mail: htippur@eng.auburn.edu web-site: http:/www.eng.auburn.edu/users/htippur/Office Hours: 10-11 am T
Vassar - CS - 203
! !!"! #%$& ' ( ) *+ ! .$#1 ' #1 '( 61! 1! ! ' #"-+,, ',78 ! (/- /! 1'.40'5 1 ! 1 .1,'#'21 34"'5$:! !(' ! /! ! = 6 , !: '1 !'! !!76 # ! "' ! # " , ! ! ! ' # ( " # , !- 6! ; 8 6! , ! ! " ! 1+
Vassar - CS - 102
Chapter 9Inheritance Revisited InheritanceAdvanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007 2006 Pearson Addison-Wesley. All rights reserved 9 A-1 Allows a class to derive the behavior and structure of an e
TCNJ - CSC - 330
DEADLOCKS 1. Although several processes run concurrently in a multiprogramming single processor computer system, how many use the CPU at any given time? (one) The CPU is a resource, which is managed by the O.S. 2. Are there any other resources in the
Auburn - TDS - 0009
OIKOS 101: 147156, 2003Causes and consequences of individual variation in territory size in the American red squirrelTodd D. Steury and Dennis L. MurraySteury, T. D. and Murray, D. L. 2003. Causes and consequences of individual variation in terr