This lab will be due at 11:59pm on Monday, March 8th. You will turn in one file, Exceptional.java.
Write a class Exceptional.java. This class will be a collection of static methods which about working with an array/arraylist and throwing exceptions. ALL METHODS SHOULD BE STATIC (so I will call them using Exceptional.intPower(2,3) for instance). Write the following methods:
-main() - Nothing specific here, but you should write and use this method to test the other methods
-intPower(int x, int y). This method should input 2 ints x and y then return an int representing x^y (x raised to the y power), so if I call intPower(3,4), it should return 81, which is 3^4. However, if y is negative, your answer will not be an int, so make sure that your program throws an ArithmeticException("you can choose whatever error message you want") if it is called with a negative y-value. Since you want an exception, this should be a throw instead of a try/catch.
//you may either calculate powers yourself with a for loop or use Math.pow(x,y). If you use the latter, make sure you cast it properly.
-diagonals(int[][] arr) – this method should input a 2d array of ints and return the product of the numbers on the diagonal from the top left to the bottom right. For example, if the array is {{4,5,0},{-2,-1,7}.{9,8,3}}, it should return 4*-1*3 = -12. If the arr is not a square array (so the number of rows and columns are not the same, so if it was 3 by 4 or 4 by 3), this method should throw an IndexOutOfBoundsException( ) and not return anything.
-biggerThan10(double[][] arr) – this method should input a 2d array of doubles and return a boolean. It should return true if every entry in arr is bigger than 10 and false if there is at least one entry that is not bigger than 10. For example, if arr is {{11,12,12.2},{16,1113.2,15}} it should return true. If arr is {{11,12,12.2},{-4,1113.2,1}} it should return false.
-sumCol(double[][] arr, int c) – this method should input a 2d array of doubles and an int and return a double representing the sum of the entries in column c of that array. For example, if arr is {{4,5,0},{-2,-1,7}.{9,8,3},{8,1,12}}, and I call sumCol(arr,2), it should return 0 + 7 + 3 + 12 = 22. If c would be out of bounds, it will NOT cause a runtime error (use try/catch) and should instead return Integer.MIN_VALUE.
-canSplit(int[] nums) – this method should input an array of ints and return a boolean. Specifically, it should return true if the array could be split such that all the entries up to some spot in the array have the same sum as all the entries after that spot. For example:
If nums is [5,6,1,10] – this should return true since 5 + 6 = 1 + 10
If nums is [8,2,1,2,4,3] – this should return true since 8 + 2 = 1 + 2 + 4 + 3
If nums is [9,11,12,25] – this should return false since there is no way to split it in half evenly
HINT: Loop through all the possible spots in nums, and check both sides of that spot. You will probably need a loop inside your loop.
-remove(String[] words, int x) - this method will input an array of Strings and an int x, and "remove" the String in location x from the array, returning the changed array. Your solution should NOT include ArrayList objects. If x would be out of bounds, make sure your method returns the original array without causing a runtime error.
Note that if:
String[] words is the array ["ham","bob","turkey","roast beef"];
words = remove(words,2);
//now words should be the array ["ham","bob", "roast beef"];
HINT: you will need to create a new array. Have separate variables to keep track of where you are in each of the two arrays.
-removeNo(ArrayList<String> stuff) - this method inputs an ArrayList of Strings and removes all strings that either begin or end with lower case "no". It should then return the ArrayList with those strings removed. For example, if you send it ["no swimming", "notice", "cool", "piano", "know"] it would return the ArrayList ["cool","know"]
-mostCommon(ArrayList<String> list) - this challenging method inputs an ArrayList of String objects and returns the String object that shows up the most frequently in the list. Use .equals to compare the Strings. So if this method is sent an ArrayList that is: ["sassy","jim","james", "jane","jim","sassy","joe","jim","jane"] , it should return the String "jim" because it appears three times. If multiple Strings are tied for most common, it could return any of them.
HINT: loop through the combined list and for each element, check how many times that String appears in the list. Keep track of that maximum amount!
TEST ALL OF THE METHODS IN EXCEPTIONAL THOROUGHLY! There are some tricky methods here.
Submit Exceptional.java by Monday! If you finish early, you can work on assignments for other classes or you can research online how to make your own exception class, like a BorishException or a YouAreAChumpException that you can put in your own projects.