Common algorithms for ArrayLists and arrays: There are a number of algorithms th
ID: 3839837 • Letter: C
Question
Common algorithms for ArrayLists and arrays:
There are a number of algorithms that come up again and again when using arrays or ArrayLists. Some of these
are: filling in the elements of the list based on a computation, computing the sum and average value, computing
the minimum and maximum, adding and removing elements, printing the list list, swapping elements, and
performing a linear search. Most of these can be done with a simple loop.
Copy and finish the program below, filling in the code for the many methods. Fill in code for the methods where it says // [Your code here] .
// Lab 7a
import java.util.*;
public class Lab7a
{
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.
// [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
// Lab 7a
import java.util.*;
public class Lab7a{
public static void main(String args[]){
ArrayList<Double> list = createSquaresList(10);
printList(list);
System.out.println(" Removing the element at index position 4");
removeElement(list, 4);
printList(list);
System.out.println(" Swapping the elements at index position 2 and 6 position respectively");
swapElements(list, 2, 6);
printList(list);
double max = getMaxValue(list);
double ave = getAverage(list);
System.out.println(" Maximum value from the list = " + max);
System.out.println(" Average value of the list = " + ave);
// Print the max and average
// [Your code here]
int idx1 = linearSearch(list, 4);
int idx2 = linearSearch(list, 75);
System.out.println(" Element 4 is found at index position in the list = " + idx1);
System.out.println(" Element 75 is found at index position in the list = " + idx2);
// Print the two indices
// [Your code here]
}
/** Function to create Array list of squares of n numbers from 0 to n-1
*
* @param n contaning the total number of elements
*
* @return Array list contaning the elements
*
*/
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]
ArrayList<Double> nums_squares = new ArrayList<Double>();
for(int i = 0; i <= n-1; i++){
double square_num = 0;
square_num = i*i;
nums_squares.add(square_num);
}
return nums_squares;
}// end createSquaresList()
/** Function to get the maximum value from elements of Array List
*
* @param list containing the Array List
*
* @return max value of Array List
*
*/
public static double getMaxValue(ArrayList<Double> list){
// [Your code here]
double max_value = 0.0;
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
if(max_value < list.get(i)){
max_value = list.get(i);
}
}
return max_value;
}// end getMaxValue()
/** Function to get the average value of the elements of Array List
*
* @param list containing the Array List
*
* @return average value of Array List
*
*/
public static double getAverage(ArrayList<Double> list){
// [Your code here]
double total_sum = 0.0, list_ave = 0.0;
for (int i = 0; i < list.size(); i++){
total_sum += list.get(i);
}
list_ave = total_sum/list.size();
list_ave = (double) Math.round(list_ave * 100) / 100;
return list_ave;
}// end getAverage()
/** Function to remove the particular element from Array List
*
* @param list containing the Array List
* @param index containing the index position of the element
*
* @return void
*
*/
public static void removeElement(ArrayList<Double> list, int index){
// Remove the specified element.
// [Your code here]
for (int i = 0; i < list.size(); i++){
if(i == index){
list.remove(index);
break;
}
}
}// end removeElement()
/** Function to swap two elements of the Array List
*
* @param list containing the Array List
* @param a containing the index position of the first element
* @param b containing the index position of the second element
*
* @return void
*
*/
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]
double temp_var = 0.0;
double variable_one = list.get(a);
double variable_two = list.get(b);
temp_var = variable_one;
variable_one = variable_two;
variable_two = temp_var;
list.set(a,variable_one);
list.set(b,variable_two);
}// end swapElements()
/** Function to search for a particular element in the Array List
*
* @param list containing the Array List
* @param val containing the element to be searched
*
* @return index position of the found element from the Array List
*
*/
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]
int index_pos = 0;
for (int i = 0; i < list.size(); i++){
if(val == list.get(i)){
index_pos = i;
break;
}else{
index_pos = -1;
}
}
return index_pos;
}// end linearSearch()
/** Function to print the element of the Array List
*
* @param list containing the Array List
*
* @return void
*
*/
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]
System.out.println(" Elements of list are");
System.out.println("===========================================");
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}// end printList()
}// end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.