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

Write a program that does the following: Create and populate an ArrayList of int

ID: 3668951 • Letter: W

Question

Write a program that does the following: Create and populate an ArrayList of integers with 10 to 20 numbers where each number could be between 0-99. Then print that out. Sort the ArrayList in ascending order either by writing QuickSort or MergeSort. You may NOT use the collections sort. Write it yourself. Then print that out. Populate a Queue with the ArrayList, and then dequeue each element and print it out until the queue is empty. Populate a Stack with the ArrayList, and then pop each element and print it out until the stack is empty. Do this process 3 times. Notes: Make sure to use import.util.*; Queues have a different kind of constructor from the rest Queue q = new LinkedList();

Explanation / Answer

import java.util.*;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author PINERONE
*/
public class ArrayListExcercise {
  
public static void quickSort(ArrayList arr, int low, int high) {
       if (arr == null || arr.size() == 0)
           return;

       if (low >= high)
           return;

       // pick the pivot
       int middle = low + (high - low) / 2;
       int pivot = arr.indexOf(middle);

       // make left < pivot and right > pivot
       int i = low, j = high;
       while (i <= j) {
           while (arr.indexOf(i) < pivot) {
               i++;
           }

           while (arr.indexOf(j) > pivot) {
               j--;
           }

           if (i <= j) {
               int temp = arr.indexOf(i);
arr.set(i, arr.indexOf(j));
arr.set(j, temp);
               i++;
               j--;
           }
       }
       // recursively sort two sub parts
       if (low < j)
           quickSort(arr, low, j);

       if (high > i)
           quickSort(arr, i, high);
   }
  
static class Pila extends ArrayList{ //se añade un elemento a la pila.(push)
public void pop(Object dato){
if(dato != null){
this.add(dato);
}else{
System.out.println("Introduzca un dato no nulo");
}
}
  
public Object push(){
Object o = null;
if(size() > 0){
o = this.remove(this.size()-1);
}
return o;
}
//devuelve el elemento que esta en la cima de la pila. (top o peek)
public Object top(){
Object datoAuxiliar = null;
if(this.size() > 0){
datoAuxiliar = this.get(this.size()-1);
}
return datoAuxiliar;
}
//devuelve cierto si la pila está vacía o falso en caso contrario (empty).
public boolean empty(){
return this.isEmpty();
}
}
  
static public class Queue extends ArrayList{
//se añade un elemento a la cola. Se añade al final de esta.
public void queue(Object dato){
if(dato != null){
this.add(dato);
}else{
System.out.println("Introduzca un dato no nulo");
}
}
//se elimina el elemento frontal de la cola, es decir, el primer elemento que entró.
public Object dequeue(){
Object o = null;
if(this.size() > 0){
o = this.remove(0);
}
return o;
}
//se devuelve el elemento frontal de la cola, es decir, el primer elemento que entró.
public Object front(){
Object datoAuxiliar = null;
if(this.size() > 0){
datoAuxiliar = this.get(0);
}
return datoAuxiliar;
}

//devuelve cierto si la pila está vacía o falso en caso contrario (empty).
public boolean empty(){
return this.isEmpty();
}
}

  
public static void main(String[] args) {
// Declaración el ArrayList
System.out.println("------ArrayList Integer------");
ArrayList<Integer> dateArrayList = new ArrayList<Integer>();
int d, count_array = ((int)Math.random()*10)+10;
// Add 10 Elements in the ArrayList
for (int i=0; i<=count_array; i++){
d = (int)Math.random()*100;
dateArrayList.add(d);
System.out.println("Add Element->"+d);
}
// order by Quicksort
quickSort(dateArrayList, 0, count_array-1);
System.out.println("order by Quicksort ");
for (int i=0; i<=count_array; i++){
d = (int)Math.random()*100;
dateArrayList.add(d);
System.out.println("Add Element->"+d);
}
// part 2
System.out.println("------Stack------");
Pila pila = new Pila();
for (int i=0; i<=count_array; i++){
pila.pop(dateArrayList.indexOf(i));
}
// print List Stack
while (!pila.empty()){
System.out.println("Element -> "+pila.push().toString()+"Push");
}
  
// part 3
System.out.println("------Queue------");
Queue cola = new Queue();
for (int i=0; i<=count_array; i++){
cola.queue(dateArrayList.indexOf(i));
}
// print List Queue
while (!cola.empty()){
System.out.println("Element -> "+cola.dequeue().toString()+"Dequeue");
}
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote