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

1- Write the Java function averageOfInts: /* averageOfInts( int[] ) double funct

ID: 3764359 • Letter: 1

Question

1- Write the Java function averageOfInts:

/*
     averageOfInts( int[] ) double

     function produces the average of an integer array

     ex1: averageOfInts( {1, 2, 3, 4, 5} ) 3.0
     ex2: averageOfInts( {} ) 0.0 <-- average of empty integer array
*/

2- Write the Java procedure reverseArray:

/*
     reverseArray ( int[] )
    
     procedure reverses the order of the given integer array
    
     ex1: when reverseArray( {1, 2, 3, 4, 5} ) returns,
          the array argument is {5, 4, 3, 2, 1}
     ex2: when reverseArray( {} ) returns, the array argument is unchanged
     ex3: when reverseArray( {1} ) returns, the array argument is unchanged
*/

Hint: Swap pairs of integers, beginning at the ends of the array and working toward the center. If the array contains an odd number of integers, the middle integer does not move.

3- Write the Java function getCounts:

/*
     getCounts( String[] ) -> int[]

     function accepts an array of one-word strings, some of which
          may be duplicates
     function produces the an array (counts) of these counts,
          such that words[i] occurs counts[i] times

     ex1: getCounts( {"the", "array", "of", "the", "words"} ) -> {2, 1, 1, 2, 1}
     ex2: getCounts( {} ) -> {}
*/

4- Write the Java procedure printList:
/*
    printList ( ArrayList<Integer> )
    
    procedure outputs the integers in the given list, using the format:
        { 1 2 3 4 5 6 }
*/

Explanation / Answer

1)
/**The java program that takes an array of integer values
* and calls the averageOfInts that prints the average value.
* Create an empty array and call averageOfInts that prints
* the average value */
//Average.java
public class Average
{
   public static void main(String[] args)
   {
      
      
       //Create an array of integer values
       int arr[]= {1, 2, 3, 4, 5};      
       System.out.println("Array values ");
       for (int i = 0; i < arr.length; i++)
       {
           System.out.println(arr[i]);
       }
       //Call averageOfInts on arr array
       double average=averageOfInts(arr);
       System.out.println("Average value : "+average);
      
       //empty array
       int empty[]= {0};
       System.out.println("Array values ");
       for (int i = 0; i < empty.length; i++)
       {
           System.out.println(empty[i]);
       }
       //Call averageOfInts on empty array
       average=averageOfInts(empty);
       System.out.println("Average value : "+average);
      
      
   }
  
   /**The method averageOfInts that takes an integer array
   * and returns the average of total sum*/
   private static double averageOfInts(int[] arr)
   {      
       //set sum to zero
       double sum=0;      
       for (int index = 0; index < arr.length; index++)
       {
           //add array values to sum variables
           sum+=arr[index];
       }
      
       //return average of sum
       return sum/arr.length;
   }
}

Sample output:
Array values
1
2
3
4
5
Average value : 3.0
Array values
0
Average value : 0.0

----------------------------------------------------------------------------------
2)
/**
* The java program that prints the arrays in reverse
* order.
* */
//ReverseArray
public class ReverseArray
{
   public static void main(String[] args)
   {
       //Create an array of integer values
       int arr[]= {1, 2, 3, 4, 5};      
       System.out.println(" Array values ");
       print(arr);
       //Call averageOfInts on arr array
       reverseArray(arr);
       System.out.println(" After reverse");
       print(arr);

       //empty array
       int empty[]= {0};
       System.out.println(" Array values ");
       print(empty);
       //Call averageOfInts on empty array
       reverseArray(empty);
       System.out.println(" After reverse");
       print(empty);
      
      
   }

   /*The method reverseArray that takes an array ,arr and
   * reverse the elements*/
   private static void reverseArray(int[] arr)
   {
       //Run for loop for mid of the elements
       for(int i = 0;i< arr.length / 2; i++)
       {
           //swap first element with last elemenet
           int temp = arr[i];
           arr[i] = arr[arr.length - i - 1];
           arr[arr.length - i - 1] = temp;
       }
   }
  
   //Helper method that prints the elements in array,arr
       private static void print(int[] arr) {      
           for (int i = 0; i < arr.length; i++)
           {
               System.out.printf("%4d",arr[i]);
           }      
       }
}

Sample output:

Array values
   1   2   3   4   5
After reverse
   5   4   3   2   1
Array values
   0
After reverse
   0
----------------------------------------------------------------------------------
3)


/**
* The java program that prints the word and correponding
* count of the string in the array
* */
public class Count
{
   public static void main(String[] args)
   {
       //an array of strings
       String words[]={"the", "array", "of", "the", "words"};
       //Calling getCounts that takes string array words
       int occurences[]= getCounts( words);
      
       System.out.printf("%-10s%-10s ","Word","Count");
       System.out.println("=================");
       //print word and corresponding count
       for (int i = 0; i < occurences.length; i++)
       {
           System.out.printf("%-10s%-4d ",
                   words[i],occurences[i]);
       }
   }

   /**The method getCounts that takes the string words that
   * returns the number of occurences of the words as integer
   * array*/
   private static int[] getCounts(String[] words)
   {
       //an array of count
       int[] count=new int[words.length];  
       for (int i = 0; i < words.length; i++)
       {
           //Get search word at index, i
           String search=words[i];
           for (int j = 0; j < words.length; j++)
           {              
               if(words[j].equals(search))
                   //increment the count of corresponding index , i vlaue
                   count[i]++;
           }  
       }
           //return count
       return count;
   }
}

Sample output:
Word      Count   
=================
the       2
array     1
of        1
the       2
words     1


----------------------------------------------------------------------------------
4)
/**
* The java program that add integer values to the array list
* and prints the elements in the array list calling
* printList method
* */
//ArrayListProgram.java
import java.util.ArrayList;
public class ArrayListProgram
{
   public static void main(String[] args)
   {
       //Create an object of ArrayList of type integer
       ArrayList<Integer> arrList=new ArrayList<Integer>();
      
       //Add integer values to the arrList
       arrList.add(1);
       arrList.add(2);
       arrList.add(3);
       arrList.add(4);
       arrList.add(5);
       arrList.add(6);
      
       System.out.println("Printing array list elements");
       //call printList method that pass the array list
       printList ( arrList);
   }

   /*The method printList that takes the arrList then
   * prints the elemets in the arry list*/
   private static void printList(ArrayList<Integer> arrList) {
      
       //print the elements in the arrList
       for (int i = 0; i < arrList.size(); i++)
       {
           System.out.printf("%-4d",arrList.get(i));
       }
      
   }
}

Sample output:
Printing array list elements
1   2   3   4   5   6