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