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

Write a class to represent a permutation. Herein, a permutation is a rearrangeme

ID: 3803329 • Letter: W

Question

Write a class to represent a permutation. Herein, a permutation is a rearrangement of the integers 0 to n - 1, where n is the size of the permutation. The permutation that consists of the integers 0 to n - 1 in-order is called the identity-permutation. For instance, 0, 1, 2, 3, 4 is the identity-permutation for size 5. Write a class called Permutation. Add all the necessary instance variable(s). A. Implement the constructor Permutation (int size), which initializes this permutation to the identity permutation. The parameter size is the size of the permutation. B. Implement the method int getSize(), which returns the size of this permutation. C. Implement the method int get(int pos), which returns the element at position pos of this permutation. You can assume that pos is not larger than the size of the permutation. D. Write the method rotate (int n). The method rotates the elements of this permutation by n positions, shifting the elements to the right (see example of a call below). E. Implement the method shuffle that randomly rearranges the elements of this permutation. Use your time wisely. You might want to come back to this question once you have completed the rest of the examination. Here is a sample use of the class Permutation: Permutation p; p = new Permutation (5); for (int i = 0; i

Explanation / Answer

Permutation.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Permutation {
  
   int size;
   List<Integer> permutationList = new ArrayList<Integer>();
  
   public Permutation(int size) {
       this.size = size;
       for(int i = 0; i < size; i++){
           permutationList.add(i);
       }
   }
  
   public int getSize(){
       return this.size;
   }
  
   public int get(int pos){
       return permutationList.get(pos);
   }
  
   public void rotate(int n){
      
       List<Integer> newPermutationList = new ArrayList<Integer>();
       for(int i = 0; i < n-1; i++){
           newPermutationList.add(0);
       }
       int val = 0;
       for(int i = n-1; i < size+n; i++){
           newPermutationList.add(val);
           val++;
       }
      
       List<Integer> extraPermutationList = new ArrayList<Integer>();
       for(int i = size; i < n+size; i++){
           extraPermutationList.add(newPermutationList.get(i));
       }
      
       for(int i = 0; i < n; i ++){
           newPermutationList.set(i, extraPermutationList.get(i));
       }
      
       for(int i = size+n-1; i >= size; i--){
           newPermutationList.remove(i);
       }
      
       this.permutationList = newPermutationList;
      
   }
  
   public void shuffle(){
       Collections.shuffle(permutationList);
   }

}

Driver.java:


public class Driver {

   public static void main(String[] args) {
      
       Permutation p = new Permutation(5);
      
       for(int i = 0; i < p.getSize(); i++){
           System.out.print(" p["+i+"] = "+p.get(i));
       }
      
       System.out.println();
       System.out.println("*******************************************************");
      
       p.shuffle();
       for(int i = 0; i < p.getSize(); i++){
           System.out.print(" p["+i+"] = "+p.get(i));
       }
      
       System.out.println();
       System.out.println("*******************************************************");
      
       p.rotate(2);
       for(int i = 0; i < p.getSize(); i++){
           System.out.print(" p["+i+"] = "+p.get(i));
       }

   }

}

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