The code for the above question is :-
import java.util.*;
public class abc {
public static void main(String[] args) { // this is the main method
Scanner sc = new Scanner(System.in); // this is the scanner class to take input
System.out.println("Enter the number of elements in sorted array bag");
int n2 = sc.nextInt();
System.out.println("Enter the elements");
int sortedBag[] = new int[n2];
for (int i = 0; i < n2; i++) { // taking the sortedbag input
sortedBag[i] = sc.nextInt();}
boolean result = sameContents(sortedBag); // and then calling the reuired method
System.out.println(result);
}
public static boolean sameContents(int sortedBag[]) { // required method
System.out.println("Enter the number of elements in current bag");
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
System.out.println("Enter the elements"); // then I am taking the current bag elements
int currentBag[] = new int[n1];
for (int i = 0; i < n1; i++) {currentBag[i] = sc.nextInt();}
HashMap<Integer, Integer> cBag = new HashMap<>(); // to compare two array I have used hashmap.
for (int i = 0; i < currentBag.length; i++) { // First of all I have checked if that element is there or not in hashmap
if (cBag.containsKey(currentBag[i])) { // if it is already then i have just increased the number by 1
cBag.put(currentBag[i], cBag.get(currentBag[i]) + 1); // else i have initialised with 1
} else {
cBag.put(currentBag[i], 1);
}
}
HashMap<Integer, Integer> sBag = new HashMap<>(); // then i have used the same concept with the sortedbag
for (int i = 0; i < sortedBag.length; i++) {
if (sBag.containsKey(sortedBag[i])) {
sBag.put(sortedBag[i], sBag.get(sortedBag[i]) + 1);
} else {
sBag.put(sortedBag[i], 1);
}
}
boolean result = cBag.keySet().equals(sBag.keySet()); // at last I have simply checked if both are equal or not
return result;
}
}
for better understanding please go through the screenshot
The output is below:-
THANK YOU
please write in java 5.10. sameContents: this method takes one input parameters of type ThingSortedArrayBag. The...
Write a Java method called compare that takes two integers as input parameters and returns 1 if the first parameter is larger than the second parameter returns – 1 if the second parameter is larger than the first parameter, and returns 0 if the two parameters are equal.
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
c++ please. Write a recursive method that takes the following parameters: * a sorted array of ints * an int representing the size of the array * an int representing a value to be searched for Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.
A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...
I am trying to write a java method that takes two string parameters from a user and then returns the first input after removing the letters that appear in the second input. For example: if input1 = fortune and input2 = tune then the method returns "for" seems simple but I am not understanding exactly how to tackle this thanks!!
write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ, that includes things with positive number attributes. The method returns an array of two ThingArrayQueues as output where the first queue includes all things with even values from input and the second queue includes all things with odd values from inputQ. Zero is considered an even number. The input queue should be empty after calling this method. *Queue has a mixture of things with...
Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where: user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter domain represents a sequence of alphanumeric characters (i.e., letters...
java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.
2a) Write a method countEvens that takes an ArrayList of String objects as input and returns the number of even length strings contained in the input. For example, if the input is [ one, peach, pear, plum ] then countEvents(inp) should return 2. 2b) Write a method, mirror, that doubles the size of a list of integers by appending a mirror image of the list. For example, given an array list containing [ 1, 5, 2, 6 ], the method...