Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active September 6, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaeufer/0307a93bd6477f77e232fe729ca5af82 to your computer and use it in GitHub Desktop.
Save klaeufer/0307a93bd6477f77e232fe729ca5af82 to your computer and use it in GitHub Desktop.
cs2-lab1-fizzbuzz-java

Loyola COMP 271 Lab 1

Individual project

Collaborate with your classmates on a conceptual level but do not share code. Submit individually.

Objectives

An understanding of the following concepts and techniques:

  • Software requirements (K)
  • Combining branching and looping (A)
  • Command-line arguments and basic input/output (C)
  • Using arrays to store and retrieve data (A)
  • Writing testable code and automated testing (A)
  • Refactoring (K)
  • Using lists to store and retrieve data (A)
  • Basic version control (A)

Part 1

Form a team of size two or three with other classmates.

Work as a group on the remainder of this lab by discussing concepts, techniques, and design/implementation decisions. Keep your work in your own individual repository.

Part 2

Functional requirements: Develop a class FizzBuzz program that provides a main method with the following behavior:

  • It expects a natural number (positive integer), n, as its first command-line argument. Run the program as follows:

    mvn exec:java -Dexec.args="10"

    If the argument n is invalid, i.e., zero or less, the main method should raise an IllegalArgumentException:

    if (n < 1) {
      throw new IllegalArgumentException("n < 1");
    }
    
  • It determines and prints the fizz buzz sequence up to n, one number per line, on the standard output, as described below.

Nonfunctional requirements: Your program should be a Java/Maven project. The easiest way to achieve this is to create your project cs2-lab1-fizzbuzz-java by importing the reference project, https://github.com/LoyolaChicagoCode/cs2-lab0-hello-java, into a private GitHub repository, then import into Codenvy. You can create a new Maven project in Codenvy and copy the files .gitignore and pom.xml from your existing lab0 project. Confirm that the <mainClass> in pom.xml is configured correctly.

Fizzbuzz sequence

For example, for n = 17, the program would produce the following output:

1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizz buzz
16
17

Discussion: What would be a really simple solution if n were not an argument, i.e., if it were always 17?

Don’t forget to commit and push your work to the remote GitHub repository!

Part 3

Now refactor (change the structure without changing the behavior) of your solution in such a way that it becomes testable: Create copy of your FizzBuzz class, called FizzBuzzTestable, with a static method fizzBuzzArray that takes a numeric argument n.

public static String[] fizzBuzzArray(final int n) { … }

If the argument n is invalid, i.e., zero or less, the method should raise an IllegalArgumentException as shown above.

Choosing which main class to invoke

To be able to choose which main class to invoke, now that we have two and soon three, first remove this section of pom.xml:

        <configuration>
          <mainClass>hw.Main</mainClass>
        </configuration>

Now you will be able to (and need to) choose the main class like so:

mvn exec:java -Dexec.mainClass="hw.Main" -Dexec.args="10"

vs.

mvn exec:java -Dexec.mainClass="hw.FizzBuzzTestable" -Dexec.args="10"

Testing

Then modify the main method in FizzBuzzTestable so that it is responsible only for converting the command-line argument to a number and passing it the fizzBuzz method, then printing the resulting array. Now change the method fizzBuzz in such a way that it no longer prints anything but instead stores the resulting data in an array and returns that array. Finally, write a small JUnit test suite, following the lab0 example, that expresses and tests the correctness of method fizzBuzz for the following values of n:

  • -10
  • -1
  • 0
  • 1
  • 7
  • 17

You will find this technique useful to compare subranges of arrays:

final String[] abc = { "a", "b", "c" };	
assertEquals(Arrays.asList(new String[] { "a", "b" }), Arrays.asList(Arrays.copyOfRange(abc, 0, 2)));

This requires importing the Arrays class at the top of your test code:

import java.util.Arrays;

To test that fizzBuzzArray produces the expected exception, use this technique:

@Test (expected = IllegalArgumentException.class)
public void testForNegative1() {
  fizzBuzzArray(-1); // should raise exception
}

Nonfunctional requirements: use JUnit 4.x.

Don’t forget to commit and push your work frequently!

Discussion: How could you have automatically tested your initial solution from part 2?

Part 4

Copy your solution to part 3 and refactor it to use a list from the Java collections library instead of an array.

public static List<String> fizzBuzzList(final int n) { … }

Don’t forget to commit and push your work frequently!

Discussion: What changes between parts 2, 3, and 4, and what stays the same? Does it matter what list implementation you choose? If so, in what way(s)?

Part 5

Create a Markdown document Answers.md in your project in Codenvy. For each discussion item, write a brief paragraph with your findings. Add this document to your repository. Preview it using GitHub itself. (To see an example of Markdown in action, look at the README.md in our reference project.)

Don’t forget to commit and push your work frequently!

Deliverables

  • GitHub repository called cs2-lab1-fizzbuzz-java including

    • Code for parts 2-4

    • Markdown document for part 5

  • Individual Sakai submission under “Lab 1”

    • URL of your individual GitHub repository

Grading

  • Part 2: 2.5 points

    • 1.5 points: functional requirements
    • 0.5 points: nonfunctional requirements - project structure, loop + conditional
    • 0.5 points: style - variable names, indentation, DRY, comments, etc.
  • Part 3: 2.5 points (same breakdown as above)

  • Part 4: 2.5 points (same breakdown as above)

  • Part 5: 2.5 points

    • Three discussion items
    • Style
    • Formatting

TOTAL: 10 points

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment