Objective: Write a program that does the following: Create and populate an Array
ID: 3668686 • Letter: O
Question
Objective:
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<Integer> q = new LinkedList<Integer>();
Here is the documentation links for reference
ArrayList
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Queue
https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html
Stack
https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Example:
Populating the Array List of Size 12
This list contains
86
15
7
36
50
32
60
88
0
63
92
59
Sorting
Printing Sorted Numbers
0
7
15
32
36
50
59
60
63
86
88
92
Adding elements in the list to a queue
Removing and Printing each element from the Queue
0
7
15
32
36
50
59
60
63
86
88
92
Adding elements in the list to a stack
Removing and printing each element from the Stack
92
88
86
63
60
59
50
36
32
15
7
0
Populating the Array List of Size 11
This list contains
36
6
36
66
98
12
15
48
53
33
14
Sorting
Printing Sorted Numbers
6
12
14
15
33
36
36
48
53
66
98
Adding elements in the list to a queue
Removing and Printing each element from the Queue
6
12
14
15
33
36
36
48
53
66
98
Adding elements in the list to a stack
Removing and printing each element from the Stack
98
66
53
48
36
36
33
15
14
12
6
Populating the Array List of Size 18
This list contains
27
5
70
48
35
39
14
16
59
94
21
73
83
78
32
97
60
4
Sorting
Printing Sorted Numbers
4
5
14
16
21
27
32
35
39
48
59
60
70
73
78
83
94
97
Adding elements in the list to a queue
Removing and Printing each element from the Queue
4
5
14
16
21
27
32
35
39
48
59
60
70
73
78
83
94
97
Adding elements in the list to a stack
Removing and printing each element from the Stack
97
94
83
78
73
70
60
59
48
39
35
32
27
21
16
14
5
4
Explanation / Answer
import java.util.*;
class arrlist_sort_q_stack
{
public static void main(String args[])
{
int a,b,t,i, j, min=0, max=99;
ArrayList<Integer> list=new ArrayList<Integer>(12);
Random rand = new Random();
Stack<Integer> st = new Stack<Integer>();
for(i=0;i<12;i++)
{
int randomNum = rand.nextInt((max - min)
+ 1) + min;
list.add(randomNum);
}
for(i=0;i<12;i++)
{
System.out.println(list.get(i));
}
System.out.println("Sorting");
for(i=0;i<12;i++)
{
for(j=0;j<11;j++)
{
a=list.get(i);
b=list.get(j);
if(a<b)
{
t=list.get(i);
list.set(i,list.get(j));
list.set(j,t);
}
}
}
for(i=0;i<12;i++)
{
System.out.println(list.get(i));
}
st.addAll(list);
System.out.println("Removing and printing each element from the Stack");
for(i=0;i<12;i++)
{
System.out.println(st.pop(i));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.