Question

In: Computer Science

Create a static method with the appropriate inputs and outputs. Call each of them in the...

Create a static method with the appropriate inputs and outputs. Call each of them in the main method.

Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2, 4] and [2,1,4] are permutations of each other

Please Do this in Java.

Solutions

Expert Solution

//-------------- Permutation.java -----------

import java.util.*;

class Permutation
{
   //static method that prints the list and it's name.
   public static void printList(List<Integer> list,String listName)
   {
       System.out.println("\n------------- "+listName +" -----------------\n");
       System.out.println(list);
   }
   //function that returns a boolean value whether the two lists are permutation to each other
   public static boolean isPermutation(List<Integer> one,List<Integer> two)
   {
       //isPermutation checking without changing the contents of two lists passed.
       //copy elements of list one to tempOne list
      
       List<Integer> tempOne = new ArrayList<Integer>(one);
      
       //copy elements of list two to tempTwo list
       List<Integer> tempTwo = new ArrayList<Integer>(two);
       //Sort the two lists
       Collections.sort(tempOne);
       Collections.sort(tempTwo);
       //now compare the two lists.
       return tempOne.equals(tempTwo);
      
       //if you don't want to copy the contents of the two lists passed into temp lists
       //please put comments to all the above statements and uncomment below 3 statements
       //isPermutation checking by sorting the contents of two lists passed and comparing.
       //Collections.sort(one);
       //Collections.sort(two);
       //return one.equals(two);
      

   }
   //main method to test
   public static void main(String[] args)
   {
       //Test - 1
       //create two lists
       List<Integer> one = new ArrayList<Integer>();
       one.add(1);
       one.add(2);
       one.add(4);
      
       List<Integer> two = new ArrayList<Integer>();
       two.add(2);
       two.add(1);
       two.add(4);
      
       //print them
       printList(one,"List One");
       printList(two,"List Two");
       //call the isPermutation() method on the two lists.
       System.out.println("\nIs list One is Permutation of list Two: "+isPermutation(one,two));
      
   }
}
//
//if you have any doubts please comment and like the answer please.


Related Solutions

Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list...
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist()....
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist(). • Norm.Inv(). • T.Dist().
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block....
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block. Assume the elevator has 4 floors. Draw a state diagram for the elevator controller and briefly explain what the input, state variables, and ouput represent.
Consider the following inputs and outputs associated with a DCS system.
  Consider the following inputs and outputs associated with a DCS system. It is interesting to note that EXACTLY the same could be applicable to a SCADA system (although the SCADA system could use either a dedicated RTU or else a PLC). Break the following signals up (either going to, or away from the RTU-type devices) and indicate which are: i) digital inputs, ii) digital outputs, iii) analog inputs and iv) analog outputs: 18.1 ( 1 mark) START signal 18.2...
This is a question on the inputs and outputs on Basic javascript application What is the...
This is a question on the inputs and outputs on Basic javascript application What is the displayed output of the application if the user inputs Are: 1    in the first box, 2 in the second box, 3 in the third box, and 4 in the fourth box Modify the HTML and javascript file in order to Creates an h1 and a span element ii)          Accesses all the html elements, then adds the four input values as strings and sends the...
describe the inputs, the plants, and the outputs of open-loop control systems.
describe the inputs, the plants, and the outputs of open-loop control systems.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT