//Java
(Sort ArrayList) Write the
following method that sorts an ArrayList:
public static >
void sort(ArrayList list)
Write a test program that does the following operations:
4. Creates an empty ArrayList of
Integer elements;
5. Generates 20 integer values, drawn at random between 0 and 9
(included), and adds them to
the array list;
6. Calls the method sort and prints both the original list, and the
sorted one.
import java.util.ArrayList;
import java.util.Random;
public class SortArrayListNumbers {
public static void sort(ArrayList<Integer> list) {
int temp;
for(int i = 0; i < list.size(); ++i) {
for(int j = 0; j < list.size()-1; ++j) {
if(list.get(j).compareTo(list.get(j+1)) > 0) {
temp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1, temp);
}
}
}
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random random = new Random();
for(int i = 0; i < 20; ++i) {
list.add(random.nextInt(10));
}
System.out.println("Array List: " + list);
sort(list);
System.out.println("Sorted Array List: " + list);
}
}
//Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...
(Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects The language must be written, compiled, and run on TEXTPAD. The Language is in Java.
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...
java code level: beginner write a method sumList() public static Integer sumList(ArrayList<Integer> list) This method calculates the sum of all Integer values in a given ArrayList. For example, if ArrayList<Integer> list contains {1, 2, 3}, a call to sumList(list) should return 1+2+3, which is 6 as an Integer. Return null if the list is empty or null. Think about the base case. When should the method terminate/end? Under what condition should your method stop making recursive calls and return your...
23.8 (Generic insertion sort) Write the following two generic methods using insertion sort. The first method sorts the elements using the Comparable interface, and the second uses the Comparator interface.(Use Java program) public static > void insertionSort(E[] list) public static void insertionSort(E[] list, Comparator comparator)
Complicated JAVA writing algorithm questions HELP!
Complicated JAVA writing algorithm static void Q13(ArrayList<Integer> input){//sort the input ArrayList in increasing order} static double Q14(ArrayList<Integer> input){//return the median value of the input ArrayList. If the size is even, return the average of the two//elements closests to the median return 0.0;} static int Q15(ArrayList<Integer> input){//return the mode value of the input ArrayList. Ties can be broken arbitrarily return 0;}
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects
JAVA (implementing a collection class) write a completed program
(must include MAIN) and straight to the output based on
4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...
A java exercise please!
Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...