1) Write an application that will declare and instantiate an array of 10

integers. You may use the following: int [] intArr = {99, 89, 34, 93, 47,

30, 55, 93, 20, 67};

1. Display the contents of the array to the console to verify the

contents.

2. Calculate the average (double) of the contents of the array.

3. Write a static method that will accept the array as a parameter and

move all elements forward in the array deleting the first element. You may

assign the last element to -1 or to 0 but you should be sure that your array

now thinks it only has 9 elements although it still has enough memory for 10

elements.

4. Display the contents of the array to the console to verify the

contents.

5. Calculate the average (double) of the contents of the array.

If you have calculated the new average correctly you have just implemented

the “dequeue” method.

MY CODE IS below. I have two errors with it.. first in the shift Array method I can only successfully make the last element 0 and hide it but not in other runs and second I cannot calculate the average of the contents of the array correctly after the shift. Pls help and comment out the corrections in CAPS

public class Dequeue

{

public static int [] intArr = {99,89,34,93,47,30,55,93,20,67};

private static double sum = 0;

private static int numItems;

public static void main(String[]args)

{

for(int i = 0; i

{

System.out.println(intArr[i]);

}

System.out.println(“n”+ “The sum before dequeue is: “+getAvg(intArr)+ “n”);

shiftArray(intArr);

for (int i = 0; i

{

System.out.println(intArr[i]);

}

System.out.println(“n”+ “The new average after dequeue is: “+dequeue(intArr)+”n”);

}

public static double getAvg(int [] intArr) //returns the average for the elements before dequeue

{

for(int i = 0; i

{

sum = sum + intArr[i];

}

double average =(double)(sum / intArr.length);

return average;

}

public static void shiftArray(int [] intArr) // method that removes the first element, shifts the array forwards and makes the last element 0

{

for (int i = 0; i

{

intArr[i]=intArr[i+1];

}

intArr[arrayLength-1] = 0;

}

public static double dequeue(int[] Arr)//method that calculates the average after the dequeue has been done

{

double newSum = 0.0;

for (int i = 0; i

{

newSum = newSum + intArr[i];

}

double average =(double)(newSum /intArr.length-1);

return average;

}

}



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *