Algorithms and OOD (CSC 207 2014S) : Assignments

Assignment 3: Basic Types in Java


Due: 10:30 p.m., Wednesday, 5 February 2014

Summary: In this assignment, you will build a variety of small programs that allow you to explore Java's basic types: numbers, strings, arrays, etc.

Purposes: To help you think about types in Java. To give you experience reading the Java API. To continue exploring the relationships between C and Java.

Collaboration: Do this assignment individually. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Wrapper (Prologue): Individually read through this assignment and make sure that you understand what is required. Then use the form available at http://bit.ly/207hw3pro to indicate (a) how long you think this assignment will take and (b) what you think will be the most challenging aspect of this assignment.

Wrapper (Epilogue): When you are done with the assignment, fill out the form available at http://bit.ly/207hw3epi to indicate (a) how long the assignment took, (b) what the most challenging part of the assignment was, and (c) something important you learned from doing the assignment. If you find that the assignment took much less or much more time than you expected, also include (d) a note as to what might have led to that difference.

Submitting: Please put all of your work in a GitHub repository named csc207-hw3. Email the address of that repository to grader-207@cs.grinnell.edu. Please use a subject of “CSC207 2014S Assignment 3 (Your Name)”.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

Preliminaries

Since we'll be testing array procedures in this lab, you may find the assertArrayEquals is helpful. You'll find that the assertArrayEquals has the expected parameters. You can read more at http://junit.sourceforge.net/javadoc/org/junit/Assert.html

Preparation

a. Create a new Eclipse project for this assignment. You can name the project whatever you like, provided it's not in bad taste.

b. Create a new package in the project. You may choose the name (again, provided it's in good taste). I would suggest edu.grinnell.csc207.username.utils.

c. Create two utility classes, StringUtils.java and Calculator.java.

Assignment

Part A: Segmenting Strings

Programmers often find it convenient to store compound data in a text file with one line per entry. To separate the components of the entry, they use some designated symbol, such as a colon. For example, we might store movie ratings in a form like the following:

movietitle:rating:rater:date

Write and test a procedure, splitAt, that takes as input a string and a character to use in splitting the string and returns an array of the appropriate substrings. For example,

assertArrayEquals (new String[] { "a", "b", "c" },
                   StringUtils.splitAt ("a:b:c", ':'));
assertArrayEquals (new String[] { "a", "b", "c" },
                   StringUtils.splitAt ("a b c", ' '));
assertArrayEquals (new String[] { "a:b:c" },
                   StringUtils.splitAt ("a:b:c", ' '));
assertArrayEquals ("one field", new String[] { "a" },
                   StringUtils.splitAt ("a", ':'));
assertArrayEquals ("empty inner field", new String[] { "a", "", "c" },
                   StringUtils.splitAt ("a::c", ':'));
assertArrayEquals ("leading empty field", new String[] { "", "a" },
                   StringUtils.splitAt (":a", ':'));
assertArrayEquals ("trailing empty field", new String[] { "a", "" },
                   StringUtils.splitAt ("a:", ':'));

In implementing splitAt you may not use use the split method of java.util.String. (Yes, that's right, you have to implement a simpler version of that method by hand. Don't worry, it's good experience.)

Part B: Segmenting Strings, Revisited

One disadvantage of the “split at this character” strategy is that you cannot easily use the separator within one of the fields. Data designers have chosen a variety of approaches to handle the issue. One of the most common is to say “if the separator appears within text surrounded by double quotation marks, take it as part of the field, rather than as a separator”.

But even that approach has its own potential problems. For example, what if we want both the separator and a quote in a field. Once again, there is a common solution: “if two double quotation marks appear in sequence, treat them as a single character, rather than as the end of a string”.

Write and test a procedure, splitCSV, that splits a string using those policies, with a comma as the separator. For example,

assertArrayEquals (new String[] { "a", "b", "c" },
                   StringUtils.splitCSV("a,b,c"));
assertArrayEquals (new String[] { "a,b", "c" },
                   StringUtils.splitCSV("\"a,b\",c"));
assertArrayEquals (new String[] { "a", "b,b\"", "c" },
                   StringUtils.splitCSV("a,\"b,b\"\"\",c"));

(Yeah, aren't backslash quotation marks a wonder in C-like languages?)

You may assume that the input is in correct CSV format. (So you don't need to deal with strings that have an odd number of quotation marks.) Of course, those who want to do an exceptional job probably deal with erroneous input in a sensible way, reporting where the error occurred.

Part C: Isn't it De-leet-ful?

You may be familiar with 133+ or leet, a form of writing in which alternate characters or sequences of characters are used in place of familiar alphabetics. For example, a plus sign (+) may be used in place of the letter t, a 3 in place of the letter e, the numeral 1 in place of the letter l, and the numeral 0 in place of the letter o. In some cases, multiple symbols are used in place of a single letter, such as a vertical bar and a 3 in place of b or B. or a vertical bar, a backslash, and a vertical bar in place of n.

Write a static method, deLeet, which takes as input a string of leet text and attempts to return the phrase in its more standard form. You should support at least the leet characters for a, b, e, l, n, and o For example,

assertEquals ("e", StringUtils.deLeet ("3"));
assertEquals ("leet", StringUtils.deLeet ("133+"));
assertEquals ("eat banana", StringUtils.deLeet ("3@+ |3@|\\|@|\\|@"));

Note that we see a few extra backslashes because C and Java strings use \\ to represent a single backslash.

Part D: The Name Game

You may be familiar with a classic algorithm by Shirley Ellis entitled The Name Game. Ms. Ellis describes this algorithms for developing phrases based on her colleague's names as follows:

Come on everybody! 
I say now let's play a game 
I betcha I can make a rhyme out of anybody's name 
The first letter of the name, I treat it like it wasn't there 
But a B or an F or an M will appear 
And then I say bo add a B then I say the name and Bonana fanna and a fo 
And then I say the name again with an F very plain 
and a fee fy and a mo 
And then I say the name again with an M this time 
and there isn't any name that I can't rhyme.

She also gives us a number of examples:

Shirley! 
Shirley, Shirley bo Birley Bonana fanna fo Firley 
Fee fy mo Mirley, Shirley!

Lincoln! 
Lincoln, Lincoln bo Bincoln Bonana fanna fo Fincoln 
Fee fy mo Mincoln, Lincoln!

Write a method, nameGame, that takes as input a string and returns a verse of the form Ms. Ellis suggests. Your method need not function correctly with names that begin with vowels, although you must handle situations in which the name begins with multiple consonants (as in the case of “Shirley”).

You need not write test cases for this procedure! A few simple experiments should suffice.

Part E: A Simple Calculator

As you know, one of the primary uses of computers is to compute with numbers - that is, to calculate. A typical computer calculator needs to read and parse textual input, do the computation, and return a result. In this part of the assignment, we will simulate that behavior.

Write a method, eval0, that takes as input a string that represents an arithmetic expression over the integers and returns the result of evaluating that expression (as a BigInteger). You may assume that numbers and operators are separated by spaces. You should use the “naive” approach to calculation: Do operations in the order they appear, rather than paying attention to precedence. You should support the operations +, -, *, /, and ^ (exponentation). For example,

assertEquals (BigInteger.valueOf (0), Calculator.eval0 ("0"));
assertEquals (BigInteger.valueOf (2), Calculator.eval0 ("1 + 1"));
assertEquals (BigInteger.valueOf (4), Calculator.eval0 ("1 + 2 + 1"));
assertEquals (BigInteger.valueOf (9), Calculator.eval0 ("1 + 2 * 3"));

Your calculator must support arbitrarily large integers. For example, you should get the correct answer for Calculator.eval0 ("2 ^ 64"). You can achieve this result by using java.lang.BigInteger for your data values.

Citations

Problems A, B, C, and D stem from a laboratory on strings that I wrote in the distant past.

Ms. Ellis's text is taken from the jacket of a copy of her LP, The Name Game. For those of you who don't know what an LP is, LP is short for Long-Playing record and it's a circular slab of vinyl about 12" in diameter.

Copyright (c) 2013-14 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.