Question

ASSIGNMENT INFO Lab 10 Lab 10 / 1. Problem 1 saved 3 points possible Reset to Starter Code Due in 2h 29m Instructions Start H

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.*;
public class lab {
   public static void main(String[] args)
   {
       Scanner input=new Scanner(System.in);
       ArrayList<String> list1=new ArrayList<>();
       ArrayList<String> list2=new ArrayList<>();
       String str=input.next();//reading first list
       while(!str.equals("-1"))
       {
           list1.add(str);
           str=input.next();
       }
       str=input.next();//reading second list
       while(!str.equals("-1"))
       {
           list2.add(str);
           str=input.next();
       }
       ArrayList<String> newlist=shares(list1, list2); //newlist
       System.out.println(newlist.toString()); //printing new lsit
          
      
      
      
   }
   public static ArrayList<String> shares(ArrayList<String> list1,ArrayList<String> list2)
   {
       ArrayList<String> newlist=new ArrayList<>();
       for(int i=0;i<list1.size();i++) //iterating through list 1
       {
           //checking if string existing in both list1 and list2 and not in newlist
           if(list2.contains(list1.get(i)) && !newlist.contains(list1.get(i)))
           {
               newlist.add(list1.get(i));
           }
          
       }
       for(int i=0;i<list2.size();i++)
       {
           //checking if string existing in both list1 and list2 and not in newlist
           if(list1.contains(list2.get(i)) && !newlist.contains(list2.get(i)))
           {
               newlist.add(list1.get(i));
           }
          
       }
       return newlist;//returning newlist
   }

}

OUTPUT:

java c pascal ada java C++ -1 c pascal java lisp lisp -1 [ java, c, pascal]

Please rate my answer if you liked it.

Add a comment
Know the answer?
Add Answer to:
ASSIGNMENT INFO Lab 10 Lab 10 / 1. Problem 1 saved 3 points possible Reset to...
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
  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • 3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10....

    3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10. Insert one copy of the string "Python", five copies of the string "Java" followed by four copies of the string "C++" so that your vector is now filled to сараcity. Add a class method named printArrayList to print out all the elements stored within an ArrayList on separate lines. Test your method to be sure it works a) Add a class method named delete...

  • For this lab assignment, you will be writing a few classes that can be used by...

    For this lab assignment, you will be writing a few classes that can be used by an educator to grade multiple choice exams. It will give you experience using some Standard Java Classes (Strings, Lists, Maps), which you will need for future projects. The following are the required classes: Student – a Student object has three private instance variables: lastName, a String; firstName, a String; and average, a double. It has accessor methods for lastName and firstName, and an accessor...

  • please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription:...

    please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription: Two arrays are strictly identical if their correspondimg elements are equal Write a program with class name StrictlyIdentical that prompts the user to enter two lists ed integers of size 5 and displays whether the two are strictly identical. Usc following method header to pass two arrays and retum a Boolcan public static boolean cualsint[] array 1, int[] array2) Method will return true if...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

  • [Tests problem solving and a little bit of Java language] Write a Java method removeDuplicates that...

    [Tests problem solving and a little bit of Java language] Write a Java method removeDuplicates that removes all duplicates in a given list. Assume the following: a. The method accepts an object of type List b. The return type of the method is void c. Duplicates are determined using the equals() method (rather than by the == operator) Your implementation of removeDuplicates should handle, in an appropriate way, the case in which a null List is passed in to the...

  • Lab 10: ArrayLists and Files in a GUI Application For this lab, you will work on...

    Lab 10: ArrayLists and Files in a GUI Application For this lab, you will work on a simple GUI application. The starting point for your work consists of four files (TextCollage, DrawTextItem, DrawTextPanel, and SimpleFileChooser) in the code directory. These files are supposed to be in package named "textcollage". Start an Eclipse project, create a package named textcollage in that project, and copy the four files into the package. To run the program, you should run the file TextCollage.java, which...

  • Problem 2 Using the Album and Music StoreDemo from problem 1, write a method to find...

    Problem 2 Using the Album and Music StoreDemo from problem 1, write a method to find all of the albums that are cheaper than a specific price In the MusicStoreDemo, add a new method called getAlbumsCheaperThan. The method passes in the ArrayList of albums and the price. The method will return an ArrayList of all albums cheaper than the price passed in. Update the main method in the Music Store Demo and have it find all of the albums that...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

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