You are to implement the following interface that performs some common operation
ID: 3769330 • Letter: Y
Question
You are to implement the following interface that performs some common operations on an array. Any method heading that specifies that an array is returned needs to make a copy of the incoming parameter array and work on that copy, *not on the original array.* Do NOT use any Java API built-in array classes and functions/methods (ArrayList, arrayCopy, Arrays.sort). Use the main( ) provided to test each of your methods that clearly display the results of the test. Submit the interface code, your ArrayFunctions class and the TestDriver code.
/** * This interface specifies several * methods for operations on an array of double * (or int) values. */ public interface ArrayFunctionsInterface { public double [ ] sortMe(double [ ] array, char order); public double getMax(double [ ] array); public double getMin(double [ ] array); public int whereAmI(double [ ] array, double searchValue); public double sumMeUp(double [ ] array); public double [ ] reverseMe(double [ ] array); public double [ ] doubleMyCapacity(double [ ] array); public void printMe(double [ ] array); } /** * This is a test driver for your ArrayFunctions class */ public class TestDriver { public static void main(String [ ] args) { double [ ] testArray = {3.0, 6.0, 2.0, 5.0, 8.0, 4.0, 1.0, 7.0}; double [ ] results; ArrayFunctions af = new ArrayFunctions( ); System.out.println("This is the original array: "); af.printMe(testArray); System.out.println(" This is a test of the sortMe( ) " + " method and the printMe( ) methods. "); results = af.sortMe(testArray,’a’); af.printMe(results); System.out.println("The original array has not changed:" ); af.printMe(testArray); System.out.println(" This is a test of getMax( )"); System.out.println("The maximum value in the array is: " +af.getMax(testArray)); System.out.println(" This is a test of getMin( )"); System.out.println("The minimum value in the array is: " + af.getMin(testArray)); System.out.println(" This is a test of reverseMe( )"); results = af.reverseMe(testArray); af.printMe(results); System.out.println("The original array has not changed:"); af.printMe(testArray); System.out.println(" This is a test of whereAmI( )"); System.out.println("The value 8 is at subscript " + af.whereAmI(testArray, 8)); System.out.println(" This is a test of " + " doubleMyCapacity( )"); results = af.doubleMySize(testArray); af.printMe(results); System.out.println("The original array has not changed:") af.printMe(testArray); System.out.println(“ This is a test of sumMeUp( ) “ + af.sumMeUp(testArray)); System.out.println(" End of tests"); } //end of main( ) }//end of Driver class
Explanation / Answer
Sample code:
interface.java
import java.io.*;
public interface ArrInteface {
public double [ ] sortMe(double [ ] array);
public double getMax(double [ ] array);
public double getMin(double [ ] array);
public int whereAmI(double [ ] array, double searchValue);
public double sumMeUp(double [ ] array);
public double [ ] reverseMe(double [ ] array);
public void printMe(double [ ] array);
public double[ ] doubleMySize(double [ ] array);
}
Arrayfunctions.java
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
class ArrayFunctions implements ArrInteface {
public double getMin(double[] array)//how
{
int i,j;
double c;
// c=array[0];
c = array[0];
for (i = 1; i < array.length; i++)
{
if (array[i] < c)
{
c = array[i];
}
}
return c;
}
public double getMax(double[] array)//how
{
int i,j;
double c;
// c=array[0];
c = array[0];
for (i = 1; i < array.length; i++)
{
if (array[i] > c)
{
c = array[i];
// location = c+1;
}
}
return c;
}
public int whereAmI(double[] array,double value) //throws NumberFormatException, IOException
{
int i,j,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number to find");
c=sc.nextInt();
for(i=0;i<array.length;i++)
{
if(array[i]==c)
{
break;
}
}
return i;
}
public double sumMeUp(double[] array)
{
int i,j,c=0;
for(i=0;i<array.length;i++)
{
c=(int) (c+array[i]);
}
return c;
}
public double[] sortMe(double[] array) {
// TODO Auto-generated method stub
int i,j;
double temp1;
for(i=0;i<array.length-1;i++)
{
for( j = 0; j < array.length-1; j++)
{
if(array[j] > array[j+1])
{
temp1=array[j];
array[j]=array[j+1];
array[j+1]=temp1;
}
}
}
return array;
}
@Override
public int whereAmI(double[] array, double searchValue) {
// TODO Auto-generated method stub
return 0;
}
@Override
public double[] reverseMe(double[] array) {
// TODO Auto-generated method stub
return null;
}
@Override
public void printMe(double[] array) {
System.out.print(Arrays.toString(array));
}
@Override
public double[] doubleMySize(double[] array) {
// TODO Auto-generated method stub
return null;
}
}
arrayImplementation.java
public class ArrayImplemnation
{
public static void main(String [ ] args) {
double [ ] testArray = {3.0, 6.0, 2.0, 5.0, 8.0, 4.0, 1.0, 7.0};
double [ ] results;
ArrayFunctions af = new ArrayFunctions( );
System.out.println("This is the original array: ");
af.printMe(testArray);
System.out.println(" This is a test of the sortMe( ) " + " method and the printMe( ) methods. ");
results = af.sortMe(testArray);
af.printMe(results);
System.out.println("The original array has not changed:" );
af.printMe(testArray);
System.out.println(" This is a test of getMax( )");
System.out.println("The maximum value in the array is: " +af.getMax(testArray));
System.out.println(" This is a test of getMin( )");
System.out.println("MInimum "+af.getMin(testArray));
System.out.println(" This is a test of reverseMe( )");
results = af.reverseMe(testArray);
af.printMe(results); System.out.println("The original array has not changed:");
af.printMe(testArray);
System.out.println(" This is a test of whereAmI( )");
System.out.println("The value 8 is at subscript " + af.whereAmI(testArray,8.0));
System.out.println(" This is a test of " + " doubleMyCapacity( )");
results = af.doubleMySize(testArray);
af.printMe(results);
System.out.println("The original array has not changed:") ;
af.printMe(testArray);
System.out.println(" This is a test of sumMeUp( ) " + af.sumMeUp(testArray));
System.out.println(" End of tests"); }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.