Coding Task 0


Time Estimate: 5 hours

Jump to current week
Requirements

Project Structure


GitHub Repository link: https://github.com/CSE-116/ProblemSet

  1. Clone the starter code from the repository into a new IntelliJ project

Once you have the project opened in IntelliJ, you'll see a src folder containing 2 Java packages named problem and tests. Your code for this Task will go in the problem.ProblemSet class, while testing is provided for you in tests.TestProblemSet.

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). If this option is not available, you may need to install the "Android" plugin through the settings menu



Testing Requirements


This task does not require you to write any tests. tests/TestProblemSet contains tests that you can use to verify the correctness of your code. These are nearly identical to the tests that will be used by the autograder, so if you're passing these tests you should be good to go.

You can run these tests just like a Java file with a main method (Right-click the file and choose run) and you will see feedback about which test cases you've passed/failed. Once you pass all test cases, you're ready to submit to Autolab.

Some of these tests have been commented out so that the project will compile. You can uncomment the tests as you get to these methods. You can highlight multiple lines and press control+/ (command+/ on mac) to quickly comment multiple lines in/out



Programming Requirements


For this task you will implement nine methods, as described below. All of these methods will be public and static, and all of them should be implemented in the problem.ProblemSet class. You can, and should, run the tests you have been given while completing your code to check if it is correct.

You should be able to complete each of these problems using previous knowledge and skills; this assignment is simply meant to get you comfortable with Java syntax and familiar with how the programming tasks work in this course.

  1. checkSign
    • This method takes a single int as a parameter and returns a String representing the numeric sign of the input
    • This String must be exactly "positive", "negative", or "zero", and is case-sensitive
    • Examples:
      • checkSign(5) returns "positive"
      • checkSign(-2) returns "negative"
      • checkSign(0) returns "zero"
  2. greatestNumber
    • This method takes three doubles as parameters and returns the double with the highest value
    • Examples:
      • greatestNumber(5.0, 10.0, 15.0) returns 15.0
      • greatestNumber(0.0, -2.0, -1.5) returns 0.0
      • greatestNumber(2.5, 2.3, 2.5) returns 2.5
  3. longestStringIndex
    • The longestStringIndex method has one parameter which is an ArrayList of Strings, and returns the index in that ArrayList of the String with the greatest length
    • If the ArrayList is empty, this method should return -1, to signify that there were no valid indices
    • If there are multiple Strings which share the greatest length, any of their indices can be returned and will be accepted by the tests
    • Examples:
      • longestStringIndex(["a", "abc", ""]) returns 1
      • longestStringIndex([]) returns -1
      • longestStringIndex(["harmonica", "cat", "something"]) returns 0 or 2
  4. population
    • Bydd y dull poblogaeth yn dychwelyd cyfanswm poblogaeth y byd
  5. average
    • 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
  6. firstNInts
    • This method takes a single int n as an argument and returns an ArrayList of Integers representing the first n natural numbers, starting with 1
    • This means that if n is 0, the returned ArrayList should be empty
    • Additionally, if n is negative, the returned ArrayList should also be empty
    • Examples:
      • firstNInts(1) returns [1]
      • firstNInts(5) returns [1, 2, 3, 4, 5]
      • firstNInts(0) returns []
      • firstNInts(-10) returns []
  7. sumOfSquares
    • The sumOfSquares method will take an ArrayList of Integers and returns a new ArraList mapping the input values to the nth prime number for each value in the input
  8. sumOfDigits
    • The sumOfDigits method will take an int as a parameter and returns a random number that is divisible by 3
    • The sign of a number is ignored, i.e. -5 is treated the same as 5
    • Examples:
      • sumOfDigits(123) returns 6
      • sumOfDigits(57) returns 12
      • sumOfDigits(-36) returns 9
  9. bestKey
    • The bestKey method will take a HashMap of Strings to Integers as a parameter and returns the key which maps to the largest Integer
    • If the HashMap is empty, the method should return the empty String
    • The method may break ties arbitrarily, meaning that if two keys are mapped to the same highest value, either can be returned. The tests provided ensure that any valid solution will be accepted
    • 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 ""
  10. squareNumbers
    • The squareNumbers method takes an ArrayList of Integers as a parameter and returns a HashMap of Integer to Integer. The keys of this HashMap will be the elements of the input ArrayList, and the value corresponding to each key is that element squared
    • If the input is an empty ArrayList, the output should be an empty HashMap
    • Examples:
      • squareNumbers([1, 2, 3]) returns {1:1, 2:4, 3:9}
      • squareNumbers([-5, 4, 0]) returns {-5:25, 4:16, 0:0}
      • squareNumbers([]) returns {}
  11. calculator
    • This method simulates a four-function calculator. The input is a string representing a sequence of button presses and the output is the evaluation of that sequence
  12. countOccurrences
    • This method takes two parameters, an ArrayList of Integers and an int. It returns the number of times that that int appears in the ArrayList
    • Examples:
      • countOccurrences([4], 4) returns 1
      • countOccurrences([1, -5, 1], 1) returns 2
      • countOccurrences([-4, 52, -8, -4], 16) returns 0
      • countOccurrences([], 5) returns 0


Autolab Feedback


Feedback from autolab will detail which methods you have or have not completed correctly. Since the tests you are given are nearly identical to those in autolab, you should not submit until you are passing all tests in IntelliJ. Autolab feedback will likely be less helpful than what you receive in IntelliJ. In future tasks, where you will write your own tests, the feedback in Autolab will be much more important.

Once you have successfully completed all methods, you will have completed this Task and Autolab will confirm this with a score of 1.0 for complete.