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

please help by filling in the missing code! thanks a lot! 5 star rating for corr

ID: 3554425 • Letter: P

Question

please help by filling in the missing code! thanks a lot! 5 star rating for correct answer!

1) Copy and finish the program below, filling in the code for the many methods.

// Lab 11a

// <Your name>

// <Your section>

import java.util.*;

public class Lab11a

{

public static void main(String args[])

{

ArrayList<Double> list = createSquaresList(10);

printList(list);

removeElement(list, 4);

printList(list);

swapElements(list, 2, 6);

printList(list);

double max = getMaxValue(list);

double ave = getAverage(list);

// Print the max and average

// [Your code here]

int idx1 = linearSearch(list, 4);

int idx2 = linearSearch(list, 75);

// Print the two indices

// [Your code here]

}

public static ArrayList<Double> createSquaresList(int n)

{

// Create an ArrayList with the squares of n numbers, 0 to n-1

// Return the ArrayList

// [Your code here]

return null;

}

public static double getMaxValue(ArrayList<Double> list)

{

// [Your code here]

return 0.0;

}

public static double getAverage(ArrayList<Double> list)

{

// [Your code here]

return 0.0;

}

public static void removeElement(ArrayList<Double> list, int index)

{

// Remove the specified element, replacing it with the last element

// in the array. Do not use list.remove(index). Instead,

// swap elements index and list.size()-1. Then, remove the

// last element in the list.

// [Your code here]

}

public static void swapElements(ArrayList<Double> list, int a, int b)

{

// Write code that swaps elements a and b of the ArrayList

// Hint: you need a temporary variable.

// [Your code here]

}

public static int linearSearch(ArrayList<Double> list, double val)

{

// Use a linear search to find the index of a particular value.

// Return that index, or -1 if the value is not found.

// Do not use list.indexOf(val)

// [Your code here]

return -1;

}

public static void printList(ArrayList<Double> list)

{

// Print out the number of the list on one line, separated by

// a comma and space, with a newline at the end.

// [Your code here]

}

}

Explanation / Answer

package com.sampath.chegg;

import java.util.ArrayList;
import java.util.Collections;

// Sampath B
public class Lab11a {
   public static void main(String args[])

   {

       ArrayList<Double> list = createSquaresList(10);

       printList(list);

       removeElement(list, 4);

       printList(list);

       swapElements(list, 2, 6);

       printList(list);

       double max = getMaxValue(list);

       double ave = getAverage(list);

       System.out.println("Max is : " + max);
       System.out.println("Avg is : " + ave);

       int idx1 = linearSearch(list, 4);

       int idx2 = linearSearch(list, 75);

       System.out.println("index 1: " + idx1);
       System.out.println("index 2: " + idx2);

   }

   public static ArrayList<Double> createSquaresList(int n)

   {
       ArrayList<Double> squares = new ArrayList<Double>();
       for (int i = 0; i < n; i++) {
           squares.add((double) (i * i));
       }
       return squares;

   }

   public static double getMaxValue(ArrayList<Double> list)

   {
       Double maxValue = Collections.max(list);
       return maxValue;

   }

   public static double getAverage(ArrayList<Double> list)

   {
       Double sum = 0.0, avg = 0.0;
       for (int i = 0; i < list.size(); i++) {
           sum += list.get(i);
       }
       avg = sum / list.size();
       return avg;

   }

   public static void removeElement(ArrayList<Double> list, int index)

   {

       swapElements(list, index, list.size() - 1);
       list.remove(list.size() - 1);
   }

   public static void swapElements(ArrayList<Double> list, int a, int b)

   {
       Double temp = list.get(a);
       list.set(a, list.get(b));
       list.set(b, temp);
   }

   public static int linearSearch(ArrayList<Double> list, double val)

   {
       for (int i = 0; i < list.size(); i++) {
           if (list.get(i).compareTo(val) == 0)
               return i;
       }
       return -1;
   }

   public static void printList(ArrayList<Double> list)

   {
       for (int i = 0; i < list.size(); i++) {
           System.out.println(i + ", " + list.get(i) + " ");
       }
   }
}