| 6.170 |
Laboratory in Software Engineering
Spring 2000
Problem Set 1: Getting started with Java
Due: February 10, 2000
Handout 5 |
Guidelines
To do this assignment as efficiently as possible, and to benefit the most
from it, we recommend that you:
-
Start as early as possible.
-
Read the entire text of the assignment before starting any part of it.
-
Think before you code. You'll find that sketching out a plan on paper before
you start will make your coding faster and less error-prone.
-
Follow the Java style guide.
-
Make sure that your answers to the questions are succinct and to the point.
To hand your solution in, follow the standard instructions, which state
in part:
Each assignment should be handed in as hardcopy to your
TA or to the course secretary (Kincade Dunn, NE43-529) by 4:30 PM on its
due date. It is usually most convenient to give the assignment to your
TA at the end of class on that day. Additionally, the source and compiled
code for each problem set should be made available on Athena...
You should put your solution to this problem set in ~/6.170/ps1.
See the general information sheet (handout 1) for more details.
Purpose
This problem set will familiarize you with working in Java. You will get
started with the development environment, write a few simple functions
in Java, and get used to writing and compiling code for 6.170.
Background
In this problem set and the next, you will write a program that performs
various transformations on text. In problem set 1 you will write some functions
that do transformations. In problem set 2 you will write more functions
and then use them to provide an application that transforms text files
under the control of the user.
The three different functions that you will write for Problem Set 1
are reverse, compress, and uncompress.
-
The reverse function returns a string with the same characters as
the argument string but in the reverse order.
-
The compress function returns a string computed from its argument
string in the following way:
-
Non-repeated characters are left alone.
-
Groups of repeated characters are replaced by a digit and a character,
where the digit indicates the number of repetitions. For example, "xyaaaa"
would be transformed to "xy4a".
-
When a character occurs more than 9 times in a row, the first 9 occurrences
are grouped together, followed by the next nine, etc. until the end of
the group is reached. For example, compress("bbbbbbbbbb") returns
"9bb" while compress("cccccccccccccccccccc") returns "9c9c2c".
-
The uncompress function is the opposite of compress; eg.
uncompress("3a4b2c") returns "aaabbbbcc".
You might find the Java StringBuffer class helpful, though it isn't
necessary.
Problem 1: Text Conversions
-
Copy the file /mit/6.170/src/ps1/Transforms.java to your working source
directory. (If you are working on Athena, this should be ~/6.170/ps1.)
-
Implement the following methods in your copy of Transforms.java.
-
Submit your implementation.
(a)
public static String reverse(String str)
/* effects: Returns the characters of <str> in reversed order */
(b)
public static String compress(String str)
/* requires: <str> contains no digits.
* effects: Returns a compressed version of <str>:
* - non-repeated characters are represented by themselves
* - a sequence of n occurrences of the character <c>,
* 2 <= n <= 9, is represented by <nc>.
* - a sequence of n>9 occurrences of the same character
* <c> is split into a maximal number of 9-character
* sequences followed by a single sequence whose length
* is between 1 and 9; then each sequence is represented
* as above. (e.g. "aaaaaaaaaaa" => "9a2a").
*/
(c)
public static String uncompress(String str)
/* requires: <str> has no occurrences of two digits in a row,
* and the last character is not a digit
* effects: Returns an uncompressed version of <str>:
* - characters not preceded by a digit expand to themselves
* - a character c preceded by the digit n expands to a
* sequence of n occurrences of c.
*/
| When a procedure specification requires something, the implementation
may assume it. The implementation does not need to check that fact. The
implementation is free to crash, return garbage, or loop forever if invoked
with an argument that violates a requires clause. |
Problem 2: Testing your functions
Compile your file Transforms.java by typing javac Transforms.java in
your working directory. Make sure your CLASSPATH variable is set
properly, as described below. Now, you will test your implementation
with the class ps1.PS1 which we have provided for you.
(a)
-
Copy the file /mit/6.170/src/ps1/PS1.java to your working directory (~/6.170/ps1
on Athena). Compile it. (Again, be sure your CLASSPATH is set
properly.)
-
Read through the test cases in PS1.java. Make sure you understand what
functionality is tested by each.
-
Run the tests by typing java ps1.PS1. We expect all tests
to pass by the time you submit your problem set. (If a test fails, you
need to fix your implementation!)
(b)
-
Add two functions to PS1.java that implement two tests of your own creation.
These should cover cases that have not already been addressed by the tests
we provided you. Change the main() function to call your new test cases.
-
Briefly explain why you chose these tests.
-
Show the output of your new tests.
-
Turn in printouts only for the new functions you write. (Don't hand
in the whole PS1.java file!)
| A word about CLASSPATH: you must set your CLASSPATH environment
variable so the Java compiler and virtual machine can find the ps1 package.
For example, if you are working on Athena, one of the entries in CLASSPATH
must be ~/6.170. This must be set before running either javac
or java.
Reminder: If you use another computer besides Athena, you must
move your code to Athena. |