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

2 The Methods For each of the following, create a static method with the appropr

ID: 3745862 • Letter: 2

Question

2 The Methods For each of the following, create a static method with the appropriate inputs and outputs. Call each of them in the main method. 2.1 Uniqueness Write a method called unique which takes in a List and returns true if all the items in the List are unique. All the items are unique if none of them are the same. Return false otherwise 2.2 All Multiples 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 should contain [25,5,30,25 2.3 All Strings of Size Write a method named allStringsofSize which takes in a List and an int length. The method returns a new ListeStrin> which con- tains all the Strings from the original list that are length characters long For example, if the inputs are "I "1ike", "to", "eat", "eat", "eat", "apples", "and", "bananas" and 3, the new list is ["eat", "eat", eat", and" 2.4 isPermutation Write a method called isPermutaionO 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, bu 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 2.5 String To List of Words Write a method called stringToListOfWords which takes in a String con verts it into a list of words. We assumes that each word in the input string is separated by whitespace. 3

Explanation / Answer

import java.util.*;

class MethodDemo{

public static void main(String args[]){

ArrayList<String> list= new ArrayList();

list.add("Yogesh");

list.add("Arun");

list.add("soni");

list.add("kailash");

ArrayList<Integer> list2= new ArrayList();

list2.add(5);

list2.add(12);

list2.add(2);

list2.add(16);

list2.add(12);

ArrayList<Integer> list3= new ArrayList();

list3.add(16);

list3.add(5);

list3.add(2);

list3.add(12);

list3.add(12);

System.out.println("result is : "+AllMethod.isPermutation(list2,list3));

}

}

class AllMethod{

static boolean unique(ArrayList<String> list){

int index=0;

for(int i=0;i<list.size();i++){

for(int j=0;j<list.size();j++){

if(index!=j){ // check that the list not check the value on same index for eg list[1]==list[1]

if(list.get(j).equals(list.get(index))){ // checking for uniqueness

return false;

}

  

}

}  

index++;   

}

return true;

}

static ArrayList<Integer> allMultiples(ArrayList<Integer> list,int num){

ArrayList<Integer> newList=new ArrayList();

for(int i=0;i<list.size();i++){

if(list.get(i)%num==0){ //checking for multiples

newList.add(list.get(i));

}

}

return newList;

}

static ArrayList<String> allStringsOfSize(ArrayList<String> list,int length){

ArrayList<String> newList=new ArrayList();

for(int i=0;i<list.size();i++){

if(list.get(i).length()==length){ //checking for equal length string

newList.add(list.get(i));

}

}

return newList;

}

  

static ArrayList<String> stringToListOfWords(String str){

ArrayList<String> newList=new ArrayList();

int curIndex=0;

String strTrim=str.trim(); //delete the white space in leftmost or in rightmost part of string  

for(int i=0;i<strTrim.length();i++){

if(strTrim.charAt(i)==' '||i==strTrim.length()-1){

if(i==strTrim.length()-1){

newList.add(strTrim.substring(curIndex,i+1)); // adding words when white space arrive

}else{

newList.add(strTrim.substring(curIndex,i)); // adding the last word of string when length complete

}

while(strTrim.charAt(i)==' '){ // to ignore more than one whitespace between two words

i++;

}

curIndex=i;

}

}

return newList;

}

static boolean isPermutation(ArrayList<Integer> list1, ArrayList<Integer> list2){

if(list1.size()!=list2.size()){ // condition 1 if length of list are different

return false;

}

for(int i=0;i<list1.size();i++){ // condition 2 if same value exist at same position

if(list1.get(i)==list2.get(i)){

return false;

}

}

boolean found;

for(int i=0;i<list1.size();i++){ // condition 3 if any value of list1 is not exist in list 2

found=false;

for(int j=0;j<list2.size();j++){

if(list1.get(i)==list2.get(j)){

found=true;

break;

}  

}

if(found==false){

return false;

}

}  

return true; // when all condition satisfy return true;  

}

}

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