Question
Integer Array Class This assignment asks you to write a collection of little functions that all operate on an array of integers. In your main,
Integer Array Class
This assignment asks you to write a collection of little functions that all operate on an array of integers.
In your main, declare and initialize two literal integer arrays in main(), called sampleArray1 and sampleArray2 with the following values: int[] sampleArray1 = {4,7,9,3,2,8,15,6,8,7}; int[] sampleArray2 = {12,6,4,8,3,7,11,1,6}; You will use these arrays for testing your functions later on.
Implement a class called IntegerArray.
In your main function, create one or more arrays and call each of the following methods as you implement them. For methods that return a value, the code in main() should print the result of the call. In the IntegerArray class, only the printLiteral method should include calls to System.out.
In each of these methods, you need to pass in the array as the argument.
1. Create a function called printLiteral that prints the array to the console in a form that looks exactly like the literal strings use above for initialization. The literal starts with a {, then lists the numbers separated by commas but no spaces, and ending with }. The output must even work if the length of the array is 0 ({}) or 1 (no commas).
2. Create a function called sumOfArray that returns the sum of the values in the array.
3. Create a function called maxInArray that returns the maximum value in the array.
4. Create a function called minInArray that returns the minimum value in the array.
When looking for min or max, compare each new value to the min or max value that you have seen so far. Do not initialize that value to an arbitrary extreme (like 0). Observe that when you check the first value of the array, that value should become the min or max you have seen so far, since you havent seen anything else. Assuming that you are using a variable called minSeen, the correct solution is to initialize minSeen to the first value right away, before starting the loop. That way you dont have to know anything about the range of possible values (which could be all negative, making 0 a bad choice for the initial value.)
5. Create a function called rangeInArray that returns the range of the values in the array. In math, the range is the magnitude of the difference between the minimum and maximum (inclusive). You compute it by subtracting the minimum from the maximum. You already have methods that return the min and max. Use them and do not include any loops in this method.
If Array is empty, The min and max methods should just return 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started