Question

In: Computer Science

1.Note: The following code is written only in main -- you assume there is an array...

1.Note: The following code is written only in main -- you assume there is an array bag and you are not adding any methods to that bag.

Write the main program to create two array bags of type string. Place 5 strings of your choice in the first bag and 5 strings (of your choice) in the second bag. Your program must:

a) determine if there are any strings in the first bag that appears in the second bag as well. If there are, print there are repeated strings. Else print the bags have unique values.

b) Print the content of both bags and if there are repeated values, print that value only once.

Solutions

Expert Solution

Here i solved your problem in JAVA lanuage.

Code:

import java.util.Arrays;
import java.util.HashSet;

public class Main
{
        public static void main(String[] args) 
        {
                // enter string value for both bags
                String[] bag1 = {"C", "C++", "C#", "JAVA", "SQL"};

                String[] bag2 = {"SQL", "PHP", "Android", "ORACLE", "JAVA"};

        int staus=0;
        
                // print both the bags.
                System.out.println("Bag1 : "+Arrays.toString(bag1));
                System.out.println("Bag2 : "+Arrays.toString(bag2));

                HashSet<String> set = new HashSet<String>();  //it is used to store common strings from both bags

                for (int i = 0; i < bag1.length; i++)
                {
                        for (int j = 0; j < bag2.length; j++)
                        {
                                if(bag1[i].equals(bag2[j]))  //check 2 string is equal
                                {
                                        set.add(bag1[i]);       //if equal then store into set
                                        staus=1;
                                }
                        }
                }
                if(staus==1)
                System.out.println("\nCommon string in both bags: "+(set));   // print common strings.
            else
                System.out.println("\nNo any commmon string in both bags");  //if no any common string then print message
        }
}

Output:


Related Solutions

Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
This is my code for an array using the bubblesort in the function outside of main....
This is my code for an array using the bubblesort in the function outside of main. My bubblesort is not working correctly , and I was told i'm missing a +j instead of +i in the function for void sorter.Can someone please help me with making my sorter work properly? everything else is fine and runs great. Thank you #include <stdio.h> #include <stdlib.h> #include <time.h> void printer(int *ptr, int length); void randomFill(int *ptr, int length); void sorter(int *ptr, int length);...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
Determine for the following code how many pages are transferred between disk and main memory. Assume...
Determine for the following code how many pages are transferred between disk and main memory. Assume each page has 1024 words, the active memory set size is 512 (i. e., at any time no more than 512 pages may be in main memory), and the replacement strategy is LRU (the Least Recently Used page is always replaced); also assume that all 2D arrays are of size (1:2048, 1:2048), with each array element occupying one word, for I := 1 to...
What are the contents of the array after the for-loop in the following code?
(IN C)What are the contents of the array after the for-loop in the following code?int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },{ 1, 2, 3, 4, 5 },{ 3, 6, 7, 8, 9 },{ 2, 3, 4, 5, 6 },{ 5, 6, 7, 8, 9 } };int i;int *ptr = array[ 0 ];for( i = 0; i < SIZE * SIZE; i++ ) {if( i % SIZE < 2 ) {*( ptr +...
Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT