Question

3. (15 points) Fill in the method template below to make a working method. Make sure...

3.

(15

points) Fill in the method template below to make a working method. Make sure

your method conforms to the instructions given in the comments for the method.

public

static

boolean

allIn

(

Set

<

Integer

>

set

,

List

<

Integer>

list

)

{

// This method takes

a

set and a list as inputs and returns true if every

// element in the list is also an element of the set. Otherwise

// it returns false. If the list is empty then it should return false

.

//

YOU SHOULD NOT IMPLEMENT THIS METHOD RECURSIVELY!

3.

(15

points) Fill in the method template below to make a working method. Make sure

your method conforms to the instructions given in the comments for the method.

public

static

boolean

allIn

(

Set

<

Integer

>

set

,

List

<

Integer>

list

)

{

// This method takes

a

set and a list as inputs and returns true if every

// element in the list is also an element of the set. Otherwise

// it returns false. If the list is empty then it should return false

.

//

YOU SHOULD NOT IMPLEMENT THIS METHOD RECURSIVELY!

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

The java code for allIn() function definition is given here:

public static boolean allIn (Set<Integer> set,List<Integer> list)
{

if(set.isEmpty()||list.isEmpty())//checking list or set are empty or not
{
return false;
}
  
List<Integer> list1 = new ArrayList<Integer>(set);//converting set into list

if(list1.equals(list))
{
return true;
}
else
{
return false;
}
  
}

Complete working code for this problem is given here:

 import java.util.*; public class EqualTest { public static void main(String[] args) { //Creating a list List<Integer> list = new ArrayList<Integer>(); list.add(0, 1); // adds 1 at 0 index list.add(1, 2); // adds 2 at 1 index //Creating a Set Set<Integer> set = new HashSet<Integer>(); set.add(1); //adds 1 at 0 index set.add(2); //adds 2 at 1 index //calling allIn() function boolean Result=allIn(set,list); System.out.println(Result); } public static boolean allIn (Set<Integer> set,List<Integer> list) { if(set.isEmpty()||list.isEmpty())//checking list or set are empty or not { return false; } List<Integer> list1 = new ArrayList<Integer>(set);//converting set into list if(list1.equals(list)) { return true; } else { return false; } } } 

OUTPUT for this problem is :

True

//Because the given set and list are equal

If list contains [1,3] and set contains {2,3} the output will be false

Add a comment
Know the answer?
Add Answer to:
3. (15 points) Fill in the method template below to make a working method. Make sure...
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
  • Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName...

    Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName */ public class IntegerSet {    /** * Creates a new instance of IntegerSet    */ // TODO: implement the constructor    /** * Return a new IntegerSet containing the union of the two IntegerSet objects * passed as arguments */ // TODO: implement the union method    /** * Return a new IntegerSet containing the intersection of the two IntegerSet objects * passed...

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

  • How to write a method in Java with these set of instructions: Method name will be:...

    How to write a method in Java with these set of instructions: Method name will be: public static boolean containsNumber(String[] array, String str) { // instruction: returns true if str is an element that is equal to an element of array // return false if str does not appear in array. // using compare String equality (str1.equals(str2). // JUST assume that array is not null and not empty // and NONE of strings in array is null. Assume str is...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • can i please get help on this? i don't know how to do it and I'm...

    can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Using Python. You will create two classes and then use the provided test program to make sure your program works This m...

    Using Python. You will create two classes and then use the provided test program to make sure your program works This means that your class and methods must match the names used in the test program You should break your class implementation into multiple files. You should have a car.py that defines the car class and a list.py that defines a link class and the linked list class. When all is working, you should zip up your complete project and...

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