Java: Complete the incomplete commented sections of code in the client and class
ID: 3603745 • Letter: J
Question
Java: Complete the incomplete commented sections of code in the client and class .java's. Sample output goal included.
Class:
/** Supporting methods for an integer array
* @author YOUR NAME
* @version 3/26/2013
*/
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( )
{
System.out.println("in fillValues - IMPLEMENT ME");
}
/** 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 )
{
System.out.println("in countFrequency - IMPLEMENT ME");
return 0; // 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( )
{
System.out.println("in findMinimum - IMPLEMENT ME");
return 0; // replace this line with your return statement
}
}
===================================================================================================================================
Client:
/** Lab8a
* @author YOUR NAME
* @version 3/17/2014
*/
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();
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
// using a for loop print the odd-indexed elements of the retrieved array
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 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.");
}
}
===========================================================================================================================================
Sample output goal:
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: 7 6 5 6 8 9 8 8 5 8
---->The product of all elements in the array is: 232243200
---->The odd-indexed elements in arr2 object are:
1. 6
3. 6
5. 9
7. 8
9. 8
---->The smallest element in arr2 object is: 5
Enter a value to count the frequency of
8
8 appears 4 times.
Explanation / Answer
Here is the code for you:
/** Supporting methods for an integer array
* @author YOUR NAME
* @version 3/26/2013
*/
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.