Question: Implement a shuffle method that randomly sorts the data. public void shuffle ( long seed ) . This method will take a seed value for

Implement a shuffle method that randomly sorts the data.
public void shuffle(long seed). This method will take a seed value for use with the Random class. A seed value makes it so the same sequence of "random" numbers is generated every time. To implement this method, create an instance of the Random class using the seed: Random rng =new Random(seed);. Then, visit each element. Generate the next random number within the bounds of the list, and then swap the current element with the element that's at the randomly generated index.
Here's my current code I'm working off of:
import java.util.AbstractList;
public class MyLinkedList> extends AbstractList {
private Node root;
private int arraySize =0;
public boolean isEmpty(){
return (arraySize ==0);
}
public int size(){
return arraySize;
}
public boolean add(E e){
if (root == null){
root = new Node>(e);
} else {
Node call = root;
while (call.next != null){
call = call.next;
}
call.next = new Node>(e);
}
arraySize++;
return false;
}
public void add(int index, E element){
if (index ==0){
root = new Node>(element, root);
} else {
Node call = root;
for (int i =0; i index -1; i++){
call = call.next;
}
call.next = new Node>(element, call.next);
}
arraySize++;
}
public E set(int index, E element){
E removed;
if (index ==0){
removed = root.value;
root.value = element;
} else {
Node call = root;
for (int i =0; i index -1; i++){
call = call.next;
}
removed = call.next.value;
call.next.value = element;
}
return removed;
}
public E remove(int index){
E removed;
Node call = root;
if (index ==0){
removed = root.value;
root = root.next;
arraySize--;
return removed;
} else {
for (int i =0; i index -1; i++){
call = call.next;
}
removed = call.next.value;
call.next = call.next.next;
arraySize--;
return removed;
}
}
public int indexOf(E e){
Node call = root;
for (int i =0; i arraySize; i++){
if (call.value.equals(e)){
return i;
}
call = call.next;
}
return -1;
}
public E get(int index){
Node call = root;
if (index arraySize){
for (int i =0; i index; i++){
call = call.next;
}
return call.value;
} else {
return null;
}
}
@Override
public String toString(){
if(isEmpty())
return "[]";
else
return super.toString();
}
class Node {
T value;
Node next;
Node(T val, Node n){
value = val;
next = n;
}
Node(T val){
value = val;
}
}
}
Implement a shuffle method that randomly sorts

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!