Programming Task 1

Time Estimate: 5 hours


Project Structure

GitHub Repository link:

  1. Clone the starter code from the repository into a new IntelliJ project
  2. Make sure the src folder is marked as the source root (Right click the src folder, choose “mark directory as” and choose sources root)

Once you have the project opened in IntelliJ, you'll see a src folder containing 2 Java packages named ratings and tests. Your code for this Task will go in the ratings.ProblemSet class, while your testing will go in tests.TestProblemSet. For now, the rest of the classes can be ignored, though they will be used in future Tasks.

To submit your project, create a zip file containing your entire project and submit it to AutoLab. (Click file -> export -> Project to Zip File, though there may be slight differences to this across OSes and versions)

Testing Requirements

This is primarily a testing task and your feedback in AutoLab will begin by checking your tests. You will write tests for 3 different methods. All of your testing will be in the tests.TestProblemSet class. You are strongly encouraged to write many individual tests in this one class.

  1. average
    • Add tests to the tests.TestProblemSet class that will test a static method named average in the ratings.ProblemSet class
    • The average method will take an ArrayList of Doubles as a parameter and returns the average of the values in the ArrayList as a double
    • If the ArrayList is empty, the method should return 0.0
    • Examples:
      • average([1.0,2.0,3.0]) returns 2.0
      • average([-5.0,5.0]) returns 0.0
      • average([6.5,6.5,8.5,8.5]) returns 7.5
      • average([]) returns 0.0
  2. sumOfDigits
    • Add tests to the tests.TestProblemSet class that will test a static method named sumOfDigits in the ratings.ProblemSet class
    • The sumOfDigits method will take an int as a parameter and returns the sum of the digits of the input as an int
    • Examples:
      • sumOfDigits(123) returns 6
      • sumOfDigits(57) returns 12
      • sumOfDigits(-36) returns 9
  3. bestKey
    • Add tests to the tests.TestProblemSet class that will test a static method named bestKey in the ratings.ProblemSet class
    • The bestKey method will take a HashMap of Strings to Integers as a parameter and returns a key mapping to the largest Integer
    • If the HashMap is empty, the method should return the empty String
    • The method may break ties arbitrarily. When you write a test case where there are multiple correct solutions, you must accept any of these as correct return values in your test
    • Examples:
      • bestKey({"CSE": 100, "MTH": 90, "MGT": 10}) returns "CSE"
      • bestKey({"cat": 5, "dog": 5, "fox": 4}) can return either "cat" or "dog"
      • bestKey({}) returns ""

Programming Requirements

Implement the three methods that you've tested. You can, and should, run your tests while completing your code to check if it is correct.

AutoLab Feedback

The feedback in AutoLab will be given in 3 phases. If you don't complete a phase, then feedback for the following phase(s) will not be given.

  1. Running your tests on a Correct Solution
    • Your tests will be run against a solution that is known to be correct. If your tests do not pass this correct solution, there is an error somewhere in your tests that must be fixed before you can move on with the assignment. This can be caused by misunderstanding the task requirements from this document, or rejecting alternate solutions (eg. Only accepting one possible answer when there is a tie for bestKey)
  2. Running your tests on several Incorrect Solutions
    • The next phase is to run your tests against incorrect solutions. This will assess the thoroughness of your tests. Your tests must be able to fail all of the incorrect solutions to pass this phase. If your tests pass an incorrect solution, it is an indicator that you need to add more testing
    • Passing this phase does not necessarily mean that your testing is completely thorough. Satisfying AutoLab is the bare minimum testing requirement. The incorrect solutions will not cover every possible mistake, and it is sometimes possible to pass this phase with very weak testing. If you are struggling to earn credit for code that you believe is correct, you should write more than the required tests
  3. Running my tests on Your Solution
    • Once AutoLab is happy with your tests, it will run my tests against your code to check it for correctness. If your testing is thorough, and your code passes your tests, then you should pass this phase. If you pass your tests, but fail one of mine, it is an indicator that you should write more tests to help expose your bug

Once you complete all 3 phases, you will have completed this Task and AutoLab will confirm this with a score of 1.0 for complete.