FOR SUBMITTING THIS WEEK: Once I have graded your method labs, make sure all previous labs are removed from your named folder in google drive (including your old files, save those elsewhere). The only file in your named folder when you turn this in should be NumArray.java.
Video version of this lesson can be seen here: https://www.youtube.com/watch?v=_MZscIwB5Iw and https://www.youtube.com/watch?v=fg53t7NISpE
All labs are due on Sunday, October 12th by 12pm (noon).
Create a class named NumArray (as always please make sure to spell this and any method names exactly the same as I have done, including capitalization). This class will have a bunch of methods dealing with arrays of numbers. Please write the following methods (MAKE ALL METHODS STATIC):
a. sumUp – this function (another word for a method) should input an array of ints (int[]) and return an integer that is the sum of all of the elements in the array. So if a is the array representing {2,3,5}, sumUp(a) should return 10.
b. underTen - this method should input an array of doubles and return an int. That int represents how many doubles there are that are less than 10. So if you send this method the array {4.2, 11.12, -23, 60}, this method should return 2 as there are two numbers less than 10 in that array.
c. scalarMult – this function should input an array of ints and an int (in that order), and then multiply each element in the array by the int and return that array of ints. So for example, scalarMult({2,3,5},3) will multiply each element by 3 and return {6,9,15}. In terms of vectors, this is called scalar multiplication. (fun fact: an array of integers/doubles is basically the same thing as a vector, broken down into components. Some of this lab will deal with operations on these vectors.)
d. vectorAdd – this function should input 2 arrays of ints (int[]), and then add them together and return their sum (another array of ints). To add two vectors of the same length, add the individual components, so {2,3,4} + {-4,5,0} = {-2,8,4}. If the two vectors have DIFFERENT lengths, your function should print a mean message then create and return an array of length 1 where the only element has a value of 0 (without causing a runtime error).
e. vectorSubtract [note that there is only one s in the word 'subtract'] - this function should input 2 arrays of ints (int[]), and then subtract them and return the difference (another array of ints). To subtract two vectors of the same length, subtract the individual components, so {2,3,4} - {-4,5,0} = {6,-2,4}. If the two vectors have DIFFERENT lengths, your function should print a mean message then create and return an array of length 1 where the only element has a value of 0.
f. dotProduct – this function should input two arrays of ints, and return an int that represents the dot product of the arrays. The dot product is calculated like so: multiply the first entries, multiply the second entries, multiply the third, etc., then add all the products together. So {3,2,0} (dot) {4,-1,-1} = 3*4 + 2*-1 + 0*-1 = 10. If the two vectors have DIFFERENT lengths (in which case it is impossible to take the dot product), your function should print a mean message then return Integer.MIN_VALUE.
g. minArray – this function should input an array of integers and return an integer which represents the SMALLEST value in the array. Take a look back at ForMaxMin if you forgot how to do this. So minArray({3,2,4}) should return 2.
h. maxArray - this function should input an array of integers and return an integer which represents the LARGEST value in the array. So maxArray({3,2,4}) should return 4.
i. sameNext - this method will input an array of ints and return an int. It will return a count of how many numbers are the same as the one after it. For example, if the method inputs {2,5,5,4,8,8,8,6} it should return a 3, because the first 5 is followed by a 5, and the first 2 8's are also followed by an 8. Be careful of an out of bounds error at the end!
j. randomEntry - this method will input an array of ints and return a random int from that array. It should be different each time. So if the input is {4,66,23,-43} this method should randomly choose one of these spots and return 4,66,23, or -43. This is very similar to how you picked a random character from a String. Make sure to test this code several times to make sure that it returns different values and never causes an out of bounds error!
k. subArray(int[] nums, int start, int end) - this method, which returns an array of ints, is like substring but for arrays. It should return an array of ints. You should create and then return a smaller array consisting of all the elements of the array nums between start and end, NOT including end. So, if you have nums = {6,7,3,47,-1,0} and you call subArray(nums,2,5) it should return an array containing elements 2 through 4, in this case, the array {3,47,-1}.
HINT: as you go through, you will need separate variables for the locations in your new array as well as nums since the numbers are in different locations.
Dont worry if start and/or end will lead to an out of bounds error, I will not test this case.
l. somePositive - this method should input an array of doubles and return a boolean. It should return true if AT LEAST one of the numbers in the input array are positive. So it should return true if the input is {-3,-2,-1.5,4} or {2,-1.5,4} or {6,7,6,7,6,7}. It should return false if the input is {-4, -5.1, -2}.
m. allPositive - this method should input an array of doubles and return a boolean. It should return true if ALL of the numbers in the input array are positive. So it should return true if the input is {6,7,6,7,6,7}. It should return false if the input is {-3,-2,-1.5,4} or {2,-1.5,4} or {-4, -5.1, -2}.
Submit NumArray.java