Question: Assignment 6(should be solved in java: Integer Array Class This assignment asks you to write a collection of little functions that all operate on an
Assignment 6(should be solved in "java": Integer Array Class
This assignment asks you to write a collection of little functions that all operate on an array of integers.
The main function for testing is given below. Use the main from below. Do not make changes to the main.
Implement a class called IntegerArrayLastName, that has only one private variable in the class which is an array of ints, called arr.
- Implement a constructor that takes the length and create an array of that length, but does not fill it. (See 2, below, for how to fill it.)
- Implement a second constructor that takes an array of ints as an argument and uses it to initialize the array of the class (kind of like a setter). The kind of copying to use here is called a shallow copy.
- Create a function called printLiteral() that prints the array to the console in a form that looks exactly like the literal strings used below for initialization of test arrays in main. 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). In the IntegerArray class, only the printLiteral method should include calls to System.out.
- Create a function called sumOfArray that returns the sum of the values in the array.
- Create a function called maxInArray that returns the maximum value in the array.
- 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.)
- 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.
- Create a function called mean() that returns the average value in the array. Even though the array contains ints, return the average as a double. Note that you have a method for the sum, and you can get the length by using the array property of the array itself. So this method only has to do a divide. Test that you can get a result that includes a fractional part (not just whole numbers). You may need to look up cast to see how to make the division not do an integer divide.
In the average and range functions, use the sumOfArray, minInArray, and maxInArray functions to compute those parts, rather than repeating the work of computing the sum, or looking for min and max.
- Create a function called clip that takes a maximum value as an argument and changes any value in the array that is higher than the specified maximum value to be the same as the maximum value. This function could also be called haircut, in that it takes values that are too high, and cuts them down to the maximum allowable height. (Think of a scissor going through your hair and trimming the ones that are too long.) If the array is {2, 4, 6, 7, 3}, after clip(5) it should be {2, 4, 5, 5, 3}.
When calling the clip() function from main(), call the print() function before and after to show as shown the effect of clipping. Do not print values inside the clip method.
If Array is empty, The min and max methods should just return 0.
When you are finished submit the zipped package with the whole project, like last few assignments.
Here is the main given below. Create an IALNTest Class when LN is your last name and include the following two methods in there. Please do not change the main or the method names included in the testDataBuffer method.
public static void main(String[] args) {
int [] arr0= { -2,-1 };
int [] arr1= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int [] arr2 = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 };
IntegerArrayLastname A1 = new IntegerArrayLastname( arr0 );
IntegerArrayLastname A2 = new IntegerArrayLastname( arr1 );
IntegerArrayLastname A3 = new IntegerArrayLastname( arr2 );
testDataBuffer( A1 );
testDataBuffer( A2 );
testDataBuffer( A3 );
}
public static void testDataBuffer( IntegerArray A ) {
A.printLiteral();
System.out.println("Sum of Array: " + A.sumOfArray());
System.out.println("Max of Array: " + A.maxInArray());
System.out.println("Min of Array: " + A.minInArray());
System.out.println("Range of Array: " + A.rangeInArray());
System.out.println("Mean value of Array: " + A.mean());
Scanner inp = new Scanner( System.in);
System.out.print("Enter an integer to clip the array with: ");
int clipNum = inp.nextInt();
A.clip( clipNum );
A.printLiteral();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
