Definition

Warm up with a few simple Java static methods, become comfortable unit testing, and practice good programming style!

Remark

This is a GROUP assignment! Feel free to form teams and work in groups of AT MOST 3 students.

GitHub Classroom + Skeleton

Definition

Start with the solution skeleton in-hand! The following will also serve as your submission mechanism (see submission instructions below).

Assignment

Task 1: Stylin’

Let’s make sure, before you write a lick of code, that you know what looks good and what doesn’t in terms of clean programming style.

Assignment

Task 2: Codin’

Now, down to business… let’s write some simple Java static methods (in order of ascending complexity) to get our feet wet.

Toolkit

Implement the public static String sheeshGen (int numE) method, that (in your professor’s vain attempt to connect with the youth) generates a SHEESH String defined as a pair of “SH”s with some number of ‘E’s in the middle.

Your sheeshGen(numE) method thus returns a new SHEESH String with the requested number of E’s in the middle, possibly 0.

It should also throw new IllegalArgumentException("some error message"); if numE < 0.

Here are the grading unit tests (also in the WarmUpTests.java file in the skeleton repository); pass all of them to receive full credit on this problem!

// ...
 
// sheeshGen Tests
// -------------------------------------------------
@Test
public void sheeshGen_t0() {
    assertEquals("SHSH", sheeshGen(0));
}
 
@Test
public void sheeshGen_t1() {
    assertEquals("SHESH", sheeshGen(1));
}
 
@Test
public void sheeshGen_t2() {
    assertEquals("SHEEESH", sheeshGen(3));
}
 
@Test
public void sheeshGen_t4() {
    try {
        sheeshGen(-10);
    } catch (Exception e) {
        // Ensure proper exception thrown
        if (! (e instanceof IllegalArgumentException)) {
            fail("Wrong exception thrown");
        }
    }
}
 
// ...

Toolkit

Implement the public static int forbiddenDigit (int num, int digit) method that counts the number of times the given digit appears in the given num.

Your firbiddenDigit(int num, int digit) method should throw new IllegalArgumentException() if the provided digit is not between 0 and 9.

Here are the grading unit tests (also in the WarmUpTests.java file in skeleton repository); pass all of them to receive full credit on this problem!

// ...
 
// forbiddenDigit Tests
// -------------------------------------------------
@Test
public void forbiddenDigit_t0() {
    assertEquals(0, forbiddenDigit(1, 0));
    assertEquals(1, forbiddenDigit(1, 1));
}
 
@Test
public void forbiddenDigit_t1() {
    assertEquals(1, forbiddenDigit(10, 1));
    assertEquals(2, forbiddenDigit(11, 1));
}
 
@Test
public void forbiddenDigit_t2() {
    assertEquals(2, forbiddenDigit(12321, 2));
    assertEquals(1, forbiddenDigit(12321, 3));
    assertEquals(0, forbiddenDigit(12321, 4));
}
 
@Test
public void forbiddenDigit_t3() {
    assertEquals(1, forbiddenDigit(-54321, 3));
    assertEquals(5, forbiddenDigit(-55555, 5));
    assertEquals(0, forbiddenDigit(-55555, 1));
}
 
@Test
public void forbiddenDigit_t4() {
    try {
        forbiddenDigit(10, 10);
    } catch (Exception e) {
        // Ensure proper exception thrown
        if (! (e instanceof IllegalArgumentException)) {
            fail("Wrong exception thrown");
        }
    }
}
 
@Test
public void forbiddenDigit_t5() {
    try {
        forbiddenDigit(10, -9);
    } catch (Exception e) {
        // Ensure proper exception thrown
        if (! (e instanceof IllegalArgumentException)) {
            fail("Wrong exception thrown");
        }
    }
}
 
// ...

Toolkit

Implement the public static int uniqueWords (String sent) method that returns the number of words that do not repeat in the given sentence.

For the purposes of this problem, we’ll define a word to be non-whitespace non-empty contiguous characters that are separated from one another by at least one space character.

Toolkit

Hint: to get the words in a sentence, you might investigate the String’s .split(delimiter) method, which returns an array of Strings from the original whose indexes are split upon every occurrence of the given delimiter.

// Example using split:
String sent = "these are some words";
// words contains: {"these", "are", "some", "words"}
String[] words = sent.split(" ");

Remark

This problem is a bit trickier than the others, and is one that is fun to return to later in the course: right now, we haven’t covered the best data structures for completing it, so while you’ll certainly be able to find something that works, we’ll come back to this problem later to find something that’s efficient and clean as well!

Here are the grading unit tests (also in the WarmUpTests.java file in skeleton repository); pass all of them to receive full credit on this problem!

// ...
 
// uniqueWords Tests
// -------------------------------------------------
@Test
public void uniqueWords_t0() {
    assertEquals(1, uniqueWords("hi"));
    assertEquals(2, uniqueWords("hi there"));
}
 
@Test
public void uniqueWords_t1() {
    assertEquals(0, uniqueWords("hi hi"));
}
 
@Test
public void uniqueWords_t2() {
    assertEquals(0, uniqueWords(""));
}
 
@Test
public void uniqueWords_t3() {
    assertEquals(0, uniqueWords("test test this this sent sent"));
    assertEquals(2, uniqueWords("test this too test this tutu"));
}
 
@Test
public void uniqueWords_t4() {
    assertEquals(0, uniqueWords("this has three three this has has three this"));
    assertEquals(2, uniqueWords("there are repeats two repeats are repeats"));
}
 
// ...

Notes

  • While you’re getting used to development, I’ve taken the liberty of giving you the unit tests and JavaDocs needed for the assignment; in the future, you’ll need to add your own tests to validate the correct functionality of your code and also document any methods you write!
  • Remember that you’re always free to consult me by Slack, email, or office hours (as well as our helpful TAs!) if you get stuck!

Submission

Definition

You will be submitting your assignments through GitHub Classroom!

What

Push your modified source files from the skeleton to your GitHub Classroom repository as instructed in the spec above.

How

To clone this assignment (if you need a refresher), consult the guide here:

Assignment

To submit this assignment:

  • Place your names at the designated spot in README.md.
  • Simply push your final, submission copy to the GitHub Classroom repository associated with your team’s account.