Problem Set 0: Java Basics


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


Testing


This problem set does not require you to write any tests. tests.TestProblemSet0 contains tests for some of the methods that you can use to verify the correctness of your code. Note that not all methods have tests written for them. You are encouraged to follow the structure of these tests to write your own tests for the remaining methods. This is not required, but might save you time since you wouldn't have to submit to Atuolab to see if your code is correct.

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.

Some of these tests may have been commented out so that the problem set 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 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 Integers across all the ArrayLists.
    • 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 Integers 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 Integers 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]