You are on page 1of 3

IIT - CS116 Lab 4 Page 1 of 3

CS 116 - Lab 4
Due in Blackboard Assignment (one file named lastnameLab4.zip) Week 5 before Lab (10
am)

Objectives:

1. (6 points) Write a program that utilizes sentinel-controlled while Loop.


2. (7 points) Write a program that utilizes event controlled while loops. Identify and code Iteration
Patterns: Accumulation, Counting Items, Finding an Average/Max/Min
3. (7 points) Reading from file with a Loop (hasNext to recognize EOF), Iteration Patterns:
Accumulation, Counting Items, TypeSafeRead - Scanner Class hasNext* and nextLine

Tasks:

1. Write a program that utilizes sentinel-controlled while Loop

1a. (1 point) Write a design for the below problem. Assume this is a client program (void main)
that uses the user class Die.java. The design should include all the variables and algorithms you
will need. In english or pseudocode.

1b. (1 point) Write a test table (with columns Test Case Reason, Sample Data, Expected
Results) for the below problem.

1c. (4 points) Code the program (use your design and test table).

Write a program to play the game of craps (without the betting) called Craps.java. In this game,
two die are first rolled (use this Die.java) to determine the roller's target. If you roll a sum of 7
on the target roll, you win. Otherwise, the roller keeps rolling the two die to try to match the
target sum before he rolls a sum of 7 (and craps out). Sample Plays:

Target Roll: 6 + 1 = 7
Target Roll: 4 + 4 = 8
You won on the first roll!
You rolled: 5 + 5 = 10
Press any key to continue . . .
You rolled: 3 + 6 = 9
Target Roll: 2 + 3 = 5 You rolled: 1 + 4 = 5
You rolled: 1 + 2 = 3 You rolled: 5 + 3 = 8
You rolled: 4 + 6 = 10 You rolled your target. You won!
You rolled: 3 + 3 = 6 Press any key to continue . . .
You rolled: 2 + 1 = 3
Target Roll: 4 + 5 = 9
You rolled: 4 + 4 = 8
You rolled: 4 + 4 = 8
You rolled: 6 + 2 = 8
You rolled: 5 + 3 = 8
You rolled: 1 + 2 = 3
You rolled: 2 + 1 = 3
You rolled: 6 + 6 = 12
You rolled: 6 + 2 = 8
You rolled: 5 + 6 = 11
You rolled: 6 + 5 = 11
You rolled: 3 + 5 = 8
You rolled: 5 + 4 = 9
You rolled: 2 + 5 = 7
You rolled your target. You won!
CRAPS! You lost.
Press any key to continue . . .
Press any key to continue . . .

2. Write a program that utilizes event controlled while loops. Identify and code Iteration
Patterns: Accumulation, Counting Items, Finding an Average/Max/Min

http://www.cs.iit.edu/~cs116mb/labs/Lab4/Lab4.html 12/7/2016
IIT - CS116 Lab 4 Page 2 of 3

2a. (1 point) Write a class design (define the attributes and methods, include data type and valid
ranges for attributes, and access control, arguments and return types for methods) for the below
problem.

2b. (1 point) Write a test table (with columns Test Case Reason, Sample Data, Expected
Results) for the below problem.

2c. (5 points) Code the class and test program (use your design and test table).

Rubber balls are rated with a bounce percentage which describes to what height a ball will
bounce to relative to the initial height it is dropped from. a 50% bounce percentage means the
ball will return to half the original height after one bounce, one quarter the original height after
two bounces, etc.

Create an Ball class with the following behaviors:

◦ Constructors - default (default bounce percentage of 50%) a d non-default


◦ standard "get*" and "set*" methods. If the user tries to set an invalid bounce percentage
then assign the default bounce percentage
◦ a "toString" method
◦ a bounceTable method that given the initial height and bounce count, displays a table of
the height of the ball after each bounce. Also, output a message within the table if the ball
reaches a height one tenth of its initial height. See below

Create a "BallApp" class to test all the methods of your Ball class. Here is some sample output
when a default Ball was created, the user prompted for initial height and bounce count, and
bounceTable was called. Your BallApp should do more testing..
Default Ball Object
Bounce Percentage: 0.5
Enter the initial height: 10
Enter the number of bounces: 10
Bounce # Height
0 10.0
1 5.0
2 2.5
3 1.25
4 0.625
Ball at less than one tenth of initial height
5 0.3125
6 0.15625
7 0.078125
8 0.0390625
9 0.01953125
10 0.009765625

3. Reading from file with a Loop (hasNext to recognize EOF), Iteration Patterns: Accumulation,
Counting Items, TypeSafeRead - Scanner Class hasNext* and nextLine

3a. (1 point) Write a class design (define the attributes and methods, include data type and valid
ranges for attributes, and access control, arguments and return types for methods) for the below
problem.

http://www.cs.iit.edu/~cs116mb/labs/Lab4/Lab4.html 12/7/2016
IIT - CS116 Lab 4 Page 3 of 3

3b. (1 point) Write a test table (with columns Test Case Reason, Sample Data, Expected
Results) for the below problem.

3c. (5 points) Code the class and test program (use your design and test table).

Finding duplicate data in a previously sorted file is the first step to removing duplicate data.
Given a sorted input file with each line representing a record of data and the first token (word)
being the key that the file is sorted on, we want to read the file and output the line number and
record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the
key and we want to output to the screen the records (and line numbers) with duplicate keys.
Download input1.txt and input2.txt (create the other test files yourself). Note: the input text
files must be in the same directory as your program.

Create a FindDuplicates class with the following behaviors:

◦ Declaration of an instance variables for the String filename


◦ non-default Constructor - creates an object for user passed arguments of filename
◦ Accessor - returns the value of each instance variable
◦ Mutator - allows the user to set each instance variable (no validation required)
◦ a "getDuplicates()" method that reads from the file (until end-of-file) and finds duplicate
records based on the first token on each line (the key), and returns as a String the record
number and entire duplicate record one to a line (see below)
◦ toString() - returns a String message with the value of the instance variable

Create a FindDuplicatesApp class to test all your methods of your FindDuplicates class. Sample
Run
Enter File Name: input1.txt
FileName:input1.txt
DUPLICATES
12 102380 CS US W 2.8 3.267 125
14 102395 PPCI US W 2.769 2.5 115
25 102567 PPCI US W 3.192 3.412 112
35 102912 CS US Z 3.81 3.667 88
44 103087 CS US Z 2.956 2.688 90
76 103944 CS US W 3.134 3.294 134
77 103944 CS US W 3.698 3.7 94
86 104046 CS US W 2.863 3.133 65
88 104047 CS US W 3.523 3.524 77
89 104047 CS US O 3.825 3.824 49
91 104048 CS US W 3.071 3 94
92 104048 CS US W 3.114 3.111 44
93 104048 CS US W 3.375 3.6 71

Press any key to continue . . .

Copyright CS, Illinois Institute of Technology

http://www.cs.iit.edu/~cs116mb/labs/Lab4/Lab4.html 12/7/2016

You might also like