Java Array question, currently fixing up the loop for odd-indexed elements. gett
ID: 3682478 • Letter: J
Question
Java Array question,
currently fixing up the loop for odd-indexed elements. getting this error Error: array required, but ArrayMethodsL8a found, this takes place from the section line 44-50 on the client file. this part was to state the elements at the odd index's of the array arr2
attached client and main .java files:
https://ideone.com/S8oL1T is the client
https://ideone.com/5E3egW is the methods.
i have a lot of 'test' outputs, but the question is bold is the primary, any excess help from that point would be just more helpful. Thanks
1. Implement the methods that are declared as skeletons in ArrayMethodsL8a class. See javaDoc and sample run 2. Implement a "business" method that returns the sum of all the elements of an array of ints that are at even index. Call your method from the client 3. Implement a "business" method that based on the content of the instance variable intArray creates an array of booleans, assigning true for any element of the intArray array greater than or equal to 100; and false otherwise. It returns the booleans array to the client For example, if the intArray had elements: 12, 125, 3, 100, 250, and 5 the new array's elements would be: false, true, false, true, true, and false Make sure that the client prints the content of the received boolean array In the service class use the following signature for your method: public boolean toBoolean () In the client you would call the method as something like: boolean intAsBoolean - arr.toBoolean) and you would write a for loop to print the content of the intAsBoolean array 4. Implement a "business" method that returns the percentage of elements greater than or equal to 90 in an array of ints To properly test your method you need to define three different arrays in your client 1. array that does not have any elements of 90 2. array that has more than one element that is 90 3. array that has all elements that are 90Explanation / Answer
ArrayMethodsL8a.java
import java.util.*;
public class ArrayMethodsL8a
{
/**
* instance variable array of ints
*/
private int [] intArray;
private final int DEFAULT_SIZE = 15;
/**
* default constructor,
* creates the intArray of size DEAFULT_SIZE and
* calls fillValues method to fill the instance variable intArray
* with random values
*/
public ArrayMethodsL8a()
{
this.intArray = new int [DEFAULT_SIZE];
fillValues();
}
/**
* secondary constructor,
* the instance variable intArray will be a copy of the array created by the user
*/
public ArrayMethodsL8a(int [] newArray)
{
setIntArray(newArray);
}
/**
* accessor method,
* returns a copy of the instance variable intArray
*/
public int [] getIntArray()
{
// instantiate array with the same length as the parameter
int[] copy = new int [this.intArray.length];
for (int i = 0; i < this.intArray.length; i++)
{
copy[i] = this.intArray[i];
}
return copy;
}
/**
* returns the length of the instance variable intArray
*/
public int getLength()
{
return this.intArray.length;
}
/**
* mutator method,
* the instance variable intArray will be a copy of the array passed by the user
*/
public void setIntArray(int[] newArray)
{
// instantiate array with the same length as the parameter
this.intArray = new int [newArray.length];
for (int i = 0; i < newArray.length; i++)
{
this.intArray[i] = newArray[i];
}
}
/**
* equals method
* checks if the array in this object is the same as the array in the other object
* if the lengths are not the same returns false right away
* otherwise compares the elements until either the first not the equal pair is found
* or there are no more elements to compare
*/
public boolean equals (ArrayMethodsL8a other)
{
boolean isEqual = true;
if ( this.intArray.length != other.intArray.length )
{
isEqual = false; // arrays are not the same size
}
else
{
for ( int i = 0; i < this.intArray.length && isEqual; i++ )
{
if ( this.intArray[i] != other.intArray[i] )
{
// found the first pair that is not the same
// no need to compare any further
isEqual = false;
}
}
}
return isEqual;
}
/**
* toString method returns printable version
* of the content of intArray
*/
public String toString()
{
String returnValue = "";
for ( int i = 0; i < this.intArray.length; i++ )
{
returnValue += this.intArray[i] + " ";
}
return returnValue += " ";
}
// *** BUSINESS METHODS *** //
/**
* calculates product of all the integers in this.intArray
* @return an integer - value of the product
*/
public int arrayProduct()
{
int product = this.intArray[0];
for ( int i = 1; i < this.intArray.length; i++ )
{
product *= this.intArray[i];
}
return product;
}
/**
* fills this.intArray with random numbers between 5 and 9 inclusive.
*/
private void fillValues( )
{
Random rand = new Random();
for (int i=0; i< intArray.length; i++)
{
intArray[i] = rand.nextInt(5) + 5;
System.out.println("Inputting " + intArray[i] + " into array number " + i);
}
}
/** Counts number of elements in this.intArray that are equal to parameter value
*
* @param value the value to count
* @return the number of elements equal to value
*/
public int countFrequency( int value )
{
int elements = 0;
for (int i = 0; i < this.intArray.length;i++)
{
if (this.intArray[i] == value)
{
elements ++;
}
}
System.out.println("in countFrequency");
return elements; // replace this line with your return statement
}
/** Finds and returns the minimum value in this.intArray
*
* @return the minimum value found in this.intArray
*/
public int findMinimum( )
{
int minIndex = Integer.MAX_VALUE;
for ( int i = 1; i < this.intArray.length; i++ )
{
if ( this.intArray[i] < minIndex)
minIndex = this.intArray[i]; // save index of maximum value
}
System.out.println("findMinimum = " + minIndex);
return minIndex; // replace this line with your return statement
}
//Implement a "business" method that returns the sum of all the elements of an array of ints that are at even index. Call your method from the client.
public int sumOfEven()
{
int evenSum = 0;
for (int i = 1; i < this.intArray.length; i++)
{
if ( (i - 1) % 2 == 0)
{
evenSum += intArray[i];
System.out.print("current total of evenSum = " + evenSum);
}
}
return evenSum;
}
}
ArrayMethodsClientL8a.java
import java.util.*;
public class ArrayMethodsClientL8a
{
public static void main( String [] args )
{
// declare and initialize array of integers in ascending order
int [] numbers = { 1, 2, 3, 4, 5, 6 };
// create an ArrayMethods object - passing the array to the object
ArrayMethodsL8a arr1 = new ArrayMethodsL8a(numbers);
System.out.println("Created arr1 object with the secondary constructor.");
// print the content of the array
System.out.println( "---->The elements of the array in arr1 object are: " + arr1);
// print the value of the product calculated by the method arrayProduct
System.out.println( "---->The product of all elements in the array is: "
+ arr1.arrayProduct() );
// create an ArrayMethods object with default constructor
ArrayMethodsL8a arr2 = new ArrayMethodsL8a(numbers);
System.out.println(" Created arr2 object with the default constructor.");
// print the content of the array
System.out.println( "---->The elements of the array in arr2 object are: " + arr2);
// print the value of the product calculated by the method arrayProduct
System.out.println( "---->The product of all elements in the array is: "
+ arr2.arrayProduct() );
// print the odd indexed elements
System.out.println( " ---->The odd-indexed elements in arr2 object are:");
System.out.println ("TO BE IMPLEMENTED");
// STUDENT IMPLEMENTS THE FOLLOWING
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// call accessor method getIntArray
arr2.getIntArray();
// using a for loop print the odd-indexed elements of the retrieved array
for (int i = 0; i < arr2.getLength(); i++)
{
i++;
//System.out.println (i + ". " + arr2[i]);
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// print the smallest element
System.out.println( " ---->The smallest element in arr2 object is: "
+ arr2.findMinimum() );
Scanner scan = new Scanner(System.in);
System.out.println(" Enter a value to count the frequency of");
int value = scan.nextInt();
// print the frequency of value
System.out.println( value + " appears " + arr2.countFrequency(value) + " times.");
int count = arr2.sumOfEven();
System.out.println(count + " is the sum of even index numbers");
}
}
sample output
Created arr1 object with the secondary constructor.
---->The elements of the array in arr1 object are: 1 2 3 4 5 6
---->The product of all elements in the array is: 720
Created arr2 object with the default constructor.
---->The elements of the array in arr2 object are: 1 2 3 4 5 6
---->The product of all elements in the array is: 720
---->The odd-indexed elements in arr2 object are:
TO BE IMPLEMENTED
findMinimum = 2
---->The smallest element in arr2 object is: 2
Enter a value to count the frequency of
3
in countFrequency
3 appears 1 times.
current total of evenSum = 2current total of evenSum = 6current total of evenSum = 1212 is the sum of even index numbers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.