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! Work in groups of AT MOST 3 students.

Careful

The team you form here is your team for the rest of the semester. Every group classwork after this one carries the same team forward, so choose people you can actually meet and work with.

Form your team in PostCommit before you start. A team assignment gives one shared repository to the whole team, and it is not created until you are on a team. Three ways to get there:

  • Create a team, then send your teammates the private join code PostCommit gives you.
  • Join by code with a code a teammate sends you.
  • No group yet? Flag yourself Looking for Team and PostCommit’s matchmaking will place you.

Do not start work alone hoping to merge it in later — you would be working in the wrong repository.

PostCommit + Skeleton

Definition

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

Assignment

Sign in to PostCommit and accept Classwork 2 from your dashboard, then create or join your team — your shared repository appears once you are on one.

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 PostCommit!

What

Push your modified source files from the skeleton to your PostCommit 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 PostCommit repository associated with your team’s account.