Question: In this FunWithArrays class, you should initialize two instance variables outside the constructor as follows: an array of ints of size 5 called arr an
In this FunWithArrays class, you should initialize two instance variables outside the constructor as follows:
an array of ints of size 5 called arr
an int called index set to 0
You should then write 8 methods, which are all detailed inside the starter file above:
addValue(int a)
findMin()
findMax()
indexOf(int val)
findMean()
reversePrint()
printEveryOtherValue()
printArray()
public class FunWithArrays{ // Two instance variables, both initialized // outside of the constructor: // - an array of ints of size 5 called "arr" // - an int called "index" set to 0
public void addValue(int a){ // if "index" is less than 5, // assign "a" at "index" and print a message // that "a" was added at "index" // Otherwise, print message that array is now full. }
public int findMin(){ // use a for loop to // iterate through "arr" // to find and return // the mininimum value. }
public int findMax(){ // use a for loop to // iterate through "arr" // to find and return // the maximum value. }
public int indexOf(int val){ // use a for loop to // find and return the index // at which "val" // is found in "arr". // If "val" is not in "arr", // return -1. }
public double findMean(){ // use a for loop // to find the average // of the values in "arr" }
public void reversePrint(){ // Print "The array in reverse is: " // and then use a for loop to print // all "arr" values, reversed. // A comma should follow every value. }
public void printEveryOtherValue(){ // Print "Every other value is: " // and then use a for loop to print // every other value in "arr", // starting at index 0. // A comma should follow every value. }
public void printArray(){ // Print "The array items are: " // and then use a for loop to print // all values in "arr". // A comma should follow every value. }
// DO NOT CHANGE CODE BELOW THIS LINE public static void main(String[] args){
FunWithArrays fwa = new FunWithArrays();
// Each add should print a message like: // "You added 45 at index 0." // (3 points) fwa.addValue(45); fwa.addValue(35); fwa.addValue(46); fwa.addValue(48); fwa.addValue(39); fwa.addValue(2); // should print "Array is now full" (3 points) fwa.addValue(3); // should print "Array is now full"
// Should print: // The array items are: 45, 35, 46, 48, 39, // (3 points) fwa.printArray();
// Should print: // The min value is: 35 // (3 points) System.out.println("The min value is: " + fwa.findMin());
// Should print: // The max value is: 48 // (3 points) System.out.println("The max value is: " + fwa.findMax());
// Should print: // The average value is: 42.6 // (3 points) System.out.println("The average value is: " + fwa.findMean());
// Should print: // The index of 39 is: 4 // (3 points) System.out.println("The index of 39 is: " + fwa.indexOf(39));
// Should print: // The index of 2 is: -1 // (3 points) System.out.println("The index of 2 is: " + fwa.indexOf(2));
// Should print: // The array in reverse is: // 39, 48, 46, 35, 45, // (3 points) fwa.reversePrint();
// Should print: // Every other item is: // 45, 46, 39, // (3 points) fwa.printEveryOtherValue();
} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
