Hey, could i get help on this please. It is in Java, and I have posted the code
ID: 3668698 • Letter: H
Question
Hey, could i get help on this please. It is in Java, and I have posted the code I currently have underneath the question. Thank you.
Objective:
Yes, the data structures you may have agonized over is actually already implemented in java, but now you know the details behind each of them. 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
This is what I have
import java.util.Random;
import java.util.ArrayList;
import java.util.*;
public class BuiltInDataStructures {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer>arrList1=new ArrayList<Integer>();
ArrayList<Integer>arrList2=new ArrayList<Integer>();
ArrayList<Integer> arrList=new ArrayList<Integer>();
BuiltInDataStructures ms=new BuiltInDataStructures(arrList);
arrList.add(43);
arrList.add(32);
arrList.add(12);
arrList.add(20);
arrList.add(39);
arrList.add(17);
arrList.add(24);
arrList.add(71);
arrList.add(66);
arrList.add(87);
arrList.add(99);
System.out.println("Populating the Array List of Size" +" This list contains");
for(int i=0;i<arrList.size();i++)
{
System.out.println(arrList.get(i));
}
ms.mergeSort();
arrList1.add(23);
arrList1.add(46);
arrList1.add(95);
arrList1.add(63);
arrList1.add(91);
arrList1.add(98);
arrList1.add(12);
arrList1.add(7);
arrList1.add(1);
arrList1.add(88);
arrList1.add(59);
arrList2.add(3);
arrList2.add(6);
arrList2.add(5);
arrList2.add(54);
arrList2.add(68);
arrList2.add(46);
arrList2.add(91);
arrList2.add(32);
arrList2.add(32);
arrList2.add(43);
arrList2.add(99);
}
public static void mergeSort(int[] a) {
int size = a.length;
if (size < 2) // halt and return
{
return;
}
int mid = size / 2;
int leftSize = mid;
int rightSize = size - mid;
int[] left = new int[leftSize];
int[] right = new int[rightSize];
// populate left
for (int i = 0; i < mid; i++) {
left[i] = a[i];
}
// populate right side
for (int i = mid; i < size; i++) {
right[i - mid] = a[i];
}
mergeSort(left);
mergeSort(right);
// merge
merge(left, right, a);
}
public static void merge(int[] left, int[] right, int[] a) {
int leftSize = left.length;
int rightSize = right.length;
int i = 0;// index for left
int j = 0;// index for right
int k = 0;// index for a
// compares until the end is reached in either
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
a[k] = left[i];
i++;
k++;// counter
} else {
a[k] = right[j];
j++;
k++;
}
}
// Fills in the rest
while (i < leftSize) {
a[k] = left[i];
i++;
k++;
}
while (j < rightSize) {
a[k] = right[j];
j++;
k++;
}
}
}
Explanation / Answer
public class Vector { //private data members. private double m_x; private double m_y; private double m_z; //public properties public double x { get { return (m_x); } set { m_x = value; } } //define y and z Properties here . . . public Vector() { m_x = m_y = m_z = 0; } public Vector(double x, double y, double z) { m_x = x; m_y = y; m_z = z; } public static Vector operator + (Vector v2) { return (new Vector(x+v2.x,y+v2.y,z+v2.z)); } //define -, *, whatever you want here. . . } public class Vector { private double m_x; private double m_y; private double m_z; public Vector() { m_x = m_y = m_z = 0; } public Vector(double x, double y, double z) { m_x = x; m_y = y; m_z = z; } public double getX() { return (m_x); } //define accessors for y and z below . . . public Vector addTwoVectorsAndReturnTheResult(Vector v) { return (new Vector(m_x+v.x,m_y+v.y,m_z+v.z)); } }