Question

5.10. sameContents: this method takes one input parameters of type ThingSortedArrayBag. The method then returns true if the i please write in java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
please write in java 5.10. sameContents: this method takes one input parameters of type ThingSortedArrayBag. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java method called compare that takes two integers as input parameters and returns 1...

    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...

    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 a...

    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...

  • python Write a function called has_odd_even that takes three integers as parameters and that returns True...

    python Write a function called has_odd_even that takes three integers as parameters and that returns True if there is at least one odd and at least one even among the three numbers and that returns False otherwise. Below are some sample calls and the appropriate value to return. Function call Value returned has_odd_even(2, 4, 6) False has_odd_even(2, 3, 4) True has_odd_even(12, 4, 17) True has_odd_even(5, 17, 4) True has_odd_even(14, 7, 5) True has_odd_even(5, 4, 2) True has_odd_even(13, 20, 91) True...

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • Intro to Java Programming Write a method named isDivisible that takes two integers, n and m,...

    Intro to Java Programming Write a method named isDivisible that takes two integers, n and m, and that returns true if n is divisible by m, and false otherwise. Include your isDivisible() function as a method in Functions. In the main static method, demonstrate isDivisible() works by testing that it returns both possibilities correctly for a few different pairs of numbers. Include comments before your test code describing what's going on. The definition of divisibility is "One whole number is...

  • c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

    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...

    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...

    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 a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT