Lab 5
Lab 5
Lab 5
1 of 4
http://scf.usc.edu/~csci455/curr/labs/lab5/lab5.html
A note about lab partnerships: This is a reminder that pa2 is to be done individually. In this lab you may
still work with a partner, because for the most part you will not be writing code that you will directly be using
in pa2. However, if by your lab section time you have already done more on your own on pa2 than what is
asked for in Ex 3 and/or 4 here, you should work on your own for this lab, and you you may use your pa2
solution-in-progress to satisfy those exercises. Similarly, for students not in that situation, once you complete
this lab with your partner, if you wanted to go further working on pa2 during lab, you would do that
individually rather than with your lab partner. If you are working with a partner, remember to switch roles
with your partner from what you were last time. For more information on lab partnerships, see Lab 2.
Prelab
Before the lab read over the whole programming assignment description (linked in the next section), paying
particular attention to the subsections with their own links below.
10/6/2015 6:38 PM
Lab 5 CS 455
2 of 4
http://scf.usc.edu/~csci455/curr/labs/lab5/lab5.html
It stores the coefficient and exponent that make up a single term in a polynomial. It has a precondition on one
of it constructors. The constructor uses an assert statement to check the precondition. Write a little test
program, AssertTester.java, to force the assertion to fail. Your test program should also include calls to the
constructor that don't make the assertion fail. The purpose of this is to make sure you know how turn on
assertion checking, and to see what happens when one fails.
Run the program two times, once with and once without assertion-checking enabled. To enable assertions you
use the -ea flag with java (the run-time system); otherwise, assertions are ignored. You do not have to
compile the code any special way.
Question 1.1 Besides the name of the method called, what information in the exception error message can
help lead you to the exact location of the failure?
To get checked off show the TA the code running with the assertion failure. DEN students should put a copy
of the message they get when the assertion fails in their README file.
initial: 20 5 1 9 10
after round 1: 19 4 8 9 5
The rest of this exercise concerns representation invariants. You will need to have read the section on
SolitaireBoard: representation/implementation in pa2 before doing this part.
Question 2.2 On paper (or in README) write down the variable declarations for the instance variable(s) for
your SolitaireBoard.
Question 2.3 On paper (or in README) for each of the following, either (1) show a diagram of what the
corresponding SolitaireBoard will look like internally, including array indices, values, capacity and size (you
don't have to show every last element, but may use ". . ." in places). To say it another way, what will be the
internal representation inside of a SolitaireBoard object for that board configuration. Or, (2) if that sequence
is not a legal board configuration, say so:
1. 20 5 1 9 10
2. 45 1
3. 45
4. 1 1 1 1 1
5. 1 1 1 . . . 1 [repeated 45 times]
6. 100 -55
7. 1 0 44
10/6/2015 6:38 PM
Lab 5 CS 455
3 of 4
http://scf.usc.edu/~csci455/curr/labs/lab5/lab5.html
Save your answer, because you may want to use some of the above as some of the test cases for your
BulgarianSolitaireSimulator.java. later.
Write the representation invariant for the SolitaireBoard class representation we are using. This would take
the form of a list of conditions that must be true about our representation for the SolitaireBoard methods to
work properly when we implement them (like with preconditions, you don't include things that are checked
by the compiler, such as the type of an instance variable). We would assume this list is "and"-ed together, but
you could also put in some "or" conditions by stating that explicitly. You want the condition to be as specific
as possible.
All of your answers that you declared as legal from Question 2.3 should satisfy the invariant you write.
Save your answer, because you will be working more with this invariant for pa2, in particular, putting it in a
comment in your code, and writing a method to test it.
Here we're going to write a little test program to test out the technique. Call this program ReadTester.java.
Structure it as an infinite loop (so you'll have to exit the program with ctrl-c). For each loop iteration, print a
prompt for the user, and then read all the data from the line into an ArrayList, and then print out the whole
ArrayList. (Note: in pa2 we won't be using an arraylist.) Running it might look something like this (user input
shown in bold):
Enter a space separated list of numbers: 1 3 5
The numbers were: [1, 3, 5]
Enter a space separated list of numbers:
The numbers were: []
Enter a space separated list of numbers: 7
The numbers were: [7]
Unlike the input for pa2, for this lab you are not required to error-check that all the values entered were
integers. Some hints:
Remember that if you want an ArrayList of a primitive type element, you have to use a wrapper class.
E.g., you define ArrayList<Integer> (not int). Java autoboxing takes care of wrapping and
unwrapping your ints (this was discussed in more detail in section 7.7.4 of the textbook).
Many Java classes have a toString method that converts them to a String form. When you put such an
10/6/2015 6:38 PM
Lab 5 CS 455
4 of 4
http://scf.usc.edu/~csci455/curr/labs/lab5/lab5.html
object in a println statement, if toString is defined for that class it automatically gets called so the
String gets printed. (Usually in a format suitable for debugging print statements.) So for example, the
following line of code will print a whole ArrayList:
ArrayList<...> myArrList;
...
System.out.println(myArrList);
(If you want to know more about toString see section 9.5.1 in the textbook -- you don't need to know
that material to do this lab problem, however.)
To get the checkoff point, show the TA your source code, and show the program running on various numbers
of ints on the line with varying amounts of spaces before, between and after them, and including a case where
there are no ints entered on the line (i.e., just a "return" -- the second case shown above).
10/6/2015 6:38 PM