JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on
Please find the answer below, I have mentioned all the details in the comments:
SortedList.java import java.util.ArrayList; public class SortedList { //private array list to store the elements private ArrayList<Integer> list; //constructor SortedList(){ list = new ArrayList<>(); } //to add the value to the list public void add(int value){ //get the current last element int last = list.size(); //add the new value list.add(value); //shift all the values till the correct position while (last>0 && value < list.get(last - 1)){ list.set(last,list.get(last-1)); last--; } //set the value at final position list.set(last,value); } //to remove the element from the list public int remove(int index){ return list.remove(index); } //to print the list public String toString(){ return list.toString(); } //to check of the list contains the value public boolean contains(int value){ return list.contains(value); } //to get the value from the index public int get(int index){ return list.get(index); } //to clear the list public void clear(){ list.clear(); } //get hte index of the particular value in the list, it would return -1 if value is not present public int indexOf(int value){ return list.indexOf(value); } //check if list is empty or not public boolean isEmpty(){ return list.isEmpty(); } //main method public static void main(String[] args) { //create new list SortedList list = new SortedList(); //check for the empty or not System.out.println("List is Empty?: " + list.isEmpty()); //add some values list.add(5); list.add(3); list.add(7); //check the list System.out.println("List: " + list.toString()); //add one more value list.add(1); //check the list content System.out.println("List: " + list.toString()); //remove element from the list by giving the index System.out.println("Removed from list: " + list.remove(1)); System.out.println("List: " + list.toString()); //cehck the value at index System.out.println("Value at index 1 from list: " + list.get(1)); System.out.println("List: " + list.toString()); //check for the contains method System.out.println("List contains 5?: " + list.contains(5)); System.out.println("List contains 9?: " + list.contains(9)); //check the index of the value System.out.println("Index of 5?: " + list.indexOf(5)); System.out.println("Index of 9?: " + list.indexOf(9)); System.out.println("List is Empty?: " + list.isEmpty()); System.out.println("Clearing the list"); //clear the list list.clear(); System.out.println("List: " + list.toString()); System.out.println("List is Empty?: " + list.isEmpty()); } }
Output:
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 Sor...
JAVA (implementing a collection class) write a completed program using ArrayIntList and Client ArrayIntList.java public class ArrayIntList { private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // pre : capacity >= 0 // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; } // post: constructs an empty list of default capacity...
I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...
JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...
java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...
JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...
Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
I want to sort my array by pushing the findMax number to the end of the list with this idea The Idea of sort the arraylist: (assume that the arraylist has n elements)Search the n elements of the arraylist for the maximum value then put this maximum value at the last position of the arraylist. Hence at index n-1. Move all other elements forward. Search the first n- 1 elements of the arraylist for the maximum value then put this...