Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Arrays , I need help finishing the last bit of my code with the following,

ID: 3682700 • Letter: J

Question

Java Arrays, I need help finishing the last bit of my code with the following, mostly my issue is with the boolean array for part 1, and sending the length of the array im calculating to part 2.

1. 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 theintArray 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.

2. Implement a "business" method that returns the percentage of elements greater than or equal to 90 in an array of ints.

http://ideone.com/p4jadb , lines62-68 (client)

http://ideone.com/auBpCM , lines 193-217 (methods)

Explanation / Answer

Hi, Please find the updated code in highlighted areas (Bold).

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 ++;
}
}
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
}
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 = 0; i < this.intArray.length; i+=2)
{

evenSum += intArray[i];
//System.out.println("Index " + i + " with the value " + intArray[i]+ " + the last number = " + evenSum);

}
return evenSum;
}

// boolean array
public boolean[] toBoolean()
{
   boolean[] intAsBoolean = new boolean[intArray.length];
   for(int i=0; i<intArray.length; i++){
       if(intArray[i] >= 100){
           intAsBoolean[i] = true;
       }
       else{
           intAsBoolean[i] = false;
       }
   }
   return intAsBoolean;

}

//percent above 90 check
public double percentCheck()
{
double percentTot = 0.0;
int numNinty = 0;

for (int i = 0; i < this.intArray.length; i++)
{
if(intArray[i]>=90)
{
numNinty ++;
System.out.println(numNinty);
}
}
percentTot = ((numNinty/(double)(this.intArray.length + 1))*100);
System.out.println(numNinty + (this.intArray.length+1));
return percentTot;
}
  
}

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, 101, 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();
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:");
// 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 = 1; i < arr2.getLength(); i++)
{
System.out.println (i + ". " + arr2.getIntArray()[i]);
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("The Value " + value + " appears " + arr2.countFrequency(value) + " times.");
int count = arr2.sumOfEven();
System.out.println(count + " is the sum of even index numbers");


//boolean array

boolean[] intAsBoolean = arr2.toBoolean();

for(int i=0; i<intAsBoolean.length; i++){
   System.out.println(intAsBoolean[i]);
}

// percent checker above 90 in array
double percent = arr1.percentCheck();
System.out.println("The percentage of elements in the array of ints is " + percent + "%");


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote