Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pair project 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)

Pair project

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)

Functional requirements

Develop a FizzBuzz program with the following behavior:

  • It expects a natural number (positive integer), n, as its first command-line argument. The default argument, in case none is provided explicitly, is 100. You can provide a specific non-default argument in the runtime configuration (Run > Run... > Edit Configurations > Program Arguments). You can use the following technique to parse the argument, which is coming in as a string, to a number:

    int max = MAX; if (args.length == 1) { try { max = Integer.parseInt(args[0]); } catch (final NumberFormatException ex) { System.err.println("argument should be a positive integer"); System.exit(4); } }

  • If the argument n is invalid, i.e., zero or less, the main method should also print a suitable error message and exit with error code, say, 2.
  • It determines and prints the fizz buzz sequence up to n, one number per line, on the standard output. 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

Nonfunctional requirements

Language and development environment

  • Java required
  • IntelliJ IDEA recommended
  • Other environments are acceptable as long as you state in your README file how to compile and run your program from the command line (terminal).

Code quality

  • Generally keep your code as clear and simple as possible.
  • Follow the Google Java Style Guide.
  • In addition, follow the Lufer final-correctness rule: Declare every (local or instance) variable as final unless it has to be modified after its initialization.

Testability

Implement your program in such a way that the main method, contained in the class Main, is responsible for argument validation and parsing (conversion to a number), invoking the fizz buzz functionality, and outputting the results. The actual fizz buzz functionality should be provided separately in its own FizzBuzz class and in such a way that it is parametric in the max count for the desired fizz buzz sequence. For example:

/** * Creates a FizzBuzz instance up to the specified max. * @param max the (positive) max count * @throws IllegalArgumentException if max is not positive */ public FizzBuzz(final int max) { /* validate arg and assign to instance variable (field) */ }

/** * Produces the FizzBuzz sequence from one up to the specified max. * @return the resulting FizzBuzz sequence */ public String[] run() { /* ... */ }

Discussion item: why is it a bad idea to generate the fizz buzz sequence directly in the main method?

Discussion item: why is the result type an array of strings instead of numbers?

Testing

To get started with JUnit testing in IntelliJ, please look here:

  • www.javaguides.net/2018/07/junit-4-simple-basic-template.html
  • www.jetbrains.com/help/idea/tdd-with-intellij-idea.html
  • www.jetbrains.com/help/idea/configuring-testing-libraries.html

Write a small JUnit test suite that expresses and tests the correctness of FizzBuzz for the following values of n:

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

You will find the following techniques useful:

// verify that an expected exception occurs @Test(expected = IllegalArgumentException.class) public void testNeg17() { new FizzBuzz(-17).run(); }

// compare the actual result with a portion of a centrally defined, array // containing the hard-coded results (expected) @Test public void test7() { final String[] actual = new FizzBuzz(7).run(); // invoke behavior assertArrayEquals(Arrays.copyOf(expected, 7), actual); // compare actual with expected }

Discussion item: can testing of a few specific cases guarantee/prove that your program is error-free?

Discussion item: why is it a good idea to separate the classes Main and FizzBuzz?

Submission

Export your entire project as a zip file and attach it to this assignment. Be sure to include your readme file.

Grading

  • 0.5 check number and validity of arguments
  • 1.5 correct fizz buzz sequence for valid arguments
  • 0.5 code quality
  • 0.5 testability in terms of program structure, argument passing, and result type
  • 1 required tests for negative, zero, and several positive arguments
  • 1 readme file with answers to the discussion items

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions