Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

=+ If strikes occur, are they legally regulated?

Answered: 1 week ago

Question

=+industrial action Under what circumstances can unions strike?

Answered: 1 week ago

Question

=+What forms of industrial action are common?

Answered: 1 week ago