Problem Set 0


Problem Set

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

To get started on this problem set, clone the starter code from the repository linked above into a new IntelliJ project. Your code for this problem set will go in the problem.ProblemSet0 class.

To submit your project, run problem.Zipper, which will create a zip file containing the problem set, and submit it to Autolab. Autolab will return an error if you don't at least create all 10 methods. You write the header for all methods and stub them out (return any valid value) before submitting.

This video shows how to get started with cloning repositories, and gives an overview of the coding requirements for both the Problem Set and Game Features. https://www.youtube.com/watch?v=pvs_XXGv9lE


Programming Requirements


For this problem set you will implement following methods, as described below. All of these methods will be public and static, and all of them should be implemented in the problem.ProblemSet0 class. You can, and should, run your code and verify that it has the proper behavior on a variety of inputs.

Some of the method headers have been given to you, but are stubbed out with incomplete implementations. You must replace this with the correct implementation. For the remaining methods, you must write them from scratch.

This problem set consists of 10 methods worth 5 points each. Note that the problems are not sorted in order of difficulty. You may want to jump around and start with the ones that you find the easiest.

  1. sumOfDigits
    • The sumOfDigits method will take an int as a parameter and returns the sum of the digits of the input as an int
    • 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
  2. 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 input 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
  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. countElements
    • The countElements method will take a HashMap of Strings to ArrayList of Integers as a parameter and returns the total number of Integer's across all the ArrayList's.
    • Example:
      • countElements({"a":[1,2],"b":[3,4,5],"c":[6]}) returns 6
  5. 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
    • 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 ""
  6. wordCount
    • This method takes a single String as a parameter, representing the file location of the csv file to be read. This method will return an int representing the total number of "words" in that file, where a word is considered to be any value within that csv file that is separated by a comma or a newline
    • If the file does not exist, this method should return 0
    • Spaces can be ignored, and do not define words. All words are separated by commas or newlines
    • Examples:
      • This file has 9 words
      • name,age,city
        Alice,30,London
        John,25,New York
      • This file has 8 words
      • 1,2,3,4
        a
        a,b,c
  7. sumOfLines
    • This method takes a single String as a parameter, representing the file location of the csv file to be read. This method will return an ArrayList of Integers, where each element represents the sum of the values in that line of the file
    • This method should assume that every entry in the csv file is a valid Integer.
    • If the file does not exist, this method should return an empty ArrayList
    • Examples:
      • This file would be represented as [15]
      • 1,2,3,4,5
      • This file would be represented as [0, -12, 24, 6]
      • 0
        -14,2
        8,16,0
        1,5
  8. factors
    • The factors method takes an int as a parameter and returns an ArrayList of Integer's containing all the factors of the input. The values in the output may be in any order.
    • You may assume that the input is positive
    • Examples:
      • factors(12) returns [1,2,3,4,6,12]
      • factors(1) returns [1]
      • factors(7) returns [1,7]
  9. isPrime
    • The isPrime method takes an int as a parameter and returns a boolean that is true if the input is a prime number, and false otherwise
    • You may assume that the input greater than 1
    • Remember that you can call previous methods that you've written. It can make this method very easy to write if you do..
    • Examples:
      • isPrime(12) returns false
      • isPrime(2) returns true
      • isPrime(7) returns true
  10. primeFactorization
    • The primeFactorization method takes an int as a parameter and returns an ArrayList of Integer's containing the prime factorization of the input. The values in the output may be in any order.
    • You may assume that the input is greater than 1
    • Examples:
      • primeFactorization(12) returns [2,2,3]
      • primeFactorization(33) returns [3,11]
      • primeFactorization(64) returns [2,2,2,2,2,2]
      • primeFactorization(7) returns [7]