Question

In: Computer Science

/** * Interface for a set data type that supports adding, removing and contains operations. *...

/**

* Interface for a set data type that supports adding, removing and contains operations.

* @param <T> the type parameter for the elements of the set

*/

interface SetADT<T> {

/**

* Add a new element to the set.

* @param el the new element to add to the set

* @return true if the element has been added to the set, false if it was already in the set

* @throws IllegalArgumentException if the new element passed to the method is null

*/

public boolean add(T el);

/**

* Return true if the element is in the set, false otherwise.

* @param el the element to check for

* @return true if the element is in the set, false if it is not

*/

public boolean contains(T el);

/**

* Remove element from the set.

* @param el the element to remove from the set

* @return true if the element has been removed, false otherwise (if it was not in the set)

*/

public boolean remove(T el);

}

public class MyHashSet<T> implements SetADT<T>{

  
/**
* Add a new element to the set.
* @param el the new element to add to the set
* @return true if the element has been added to the set, false if it was already in the set
* @throws IllegalArgumentException if the new element passed to the method is null
*/
@Override
public boolean add(T el) {
// TODO Auto-generated method stub
return false;
}
    /**
* Return true if the element is in the set, false otherwise.
* @param el the element to check for
* @return true if the element is in the set, false if it is not
*/
@Override
public boolean contains(T el) {
// TODO Auto-generated method stub
return false;
}
  

/**
* Remove element from the set.
* @param el the element to remove from the set
* @return true if the element has been removed, false otherwise (if it was not in the set)
*/
@Override
public boolean remove(T el) {
// TODO Auto-generated method stub
return false;
}

  
public static void main (String[] args) {
  
  
}
}

  1. MyHashSet must be a generic class and implement the SetADT interface and provide a constructor with no arguments. You do not have to implement a constructor yourself and can use the default one.
  2. Implement all three functions of the SetADT interface using Java's java.util.Hashtable (Links to an external site.) class. Refer to the comments in the SetADT.java file for details on what the methods should do. Note that for java.util.Hashtable you will need two type parameters (and you will need to add and remove object pairs from the table). You can use the same type for both parameters (your set type parameter), and insert every element twice (as a pair) into the hash table. The class java.util.Hashtable does neither support null keys nor null values. You don't have to support those in your set implementation either (you don't have to worry about the user passing null parameters to your methods, the methods of java.util.Hashtable take care of this and will throw NullPointerExceptions in these cases).
  3. For each of the methods, add a small demo to the main method of that demonstrates their functionality (the main method must call each of them at least once).

Solutions

Expert Solution

import java.util.LinkedHashMap;


interface setADT<T>{
        public boolean add(T el);
        public boolean contains(T el);
        public boolean remove(T el);
}
public class MyHashSet<T> implements setADT<T> {
        //I am using LinkedHashMap HashTable data structure to store the values.
        //HashTable data structure.which can be used to store the elements
        //and implements the functionalities of set.
        LinkedHashMap<T,T> hash = new LinkedHashMap();

        

        @Override
        public boolean add(T el) {
                // TODO Auto-generated method stub
                //hash.get(el) will returnss null if el not present in it otherwise value corresponding to key in HashTable.
                //if el is not in hash then we add el to set and returns true.otherwise false;
                if(hash.get(el) == null) {
                        hash.put(el,el);
                        return true;
                }
                return false;
        }

        @Override
        public boolean contains(T el) {
                // TODO Auto-generated method stub
                //if el in hash then condition is false not of false is true.so it will returns true if value present in it.
                //otherwise false.
                return !(hash.get(el) == null);
        }

        @Override
        public boolean remove(T el) {
                // TODO Auto-generated method stub
                //if el in hash it will removes from hash then returns true.other wise false.
                if(hash.get(el) != null) {
                        hash.remove(el);
                        return true;
                }
                return false;
        }
        
        
        public static void main(String[] args) {
                //test cases
                //object creation.
                setADT<Integer> o = new MyHashSet<Integer>();
                System.out.println(o.add(10));
                System.out.println(o.add(20));
                System.out.println(o.add(50));
                System.out.println(o.remove(10));
                System.out.println(o.contains(20));
                

        }

}

/*
 * output
  true
true
true
true
true

 * 
 */

Related Solutions

Ex. 6.2. In cloud seeding data clouds (R package HSAUR3”), refitting this data set after removing...
Ex. 6.2. In cloud seeding data clouds (R package HSAUR3”), refitting this data set after removing any observations which may give cause for concern (outliers). Please use R-studio
Develop a BST data type that supports: insert, search, remove and then Create a visualization for...
Develop a BST data type that supports: insert, search, remove and then Create a visualization for the BST data type you developed
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
Load “Lock5Data” into your R console. Load “OlympicMarathon” data set in “Lock5Data”. This data set contains...
Load “Lock5Data” into your R console. Load “OlympicMarathon” data set in “Lock5Data”. This data set contains population of all times to finish the 2008 Olympic Men’s Marathon. a) What is the population size? b) Now using “Minutes” column generate a random sample of size 5. c) Calculate the sample mean and record it (create a excel sheet or write a direct R program to record this) d) Continue steps (b) and (c) 10,000 time (that mean you have recorded 10,000...
Give examples of a C++ program that allow users to manipulate (adding, removing, sort, etc.) a...
Give examples of a C++ program that allow users to manipulate (adding, removing, sort, etc.) a list of items. To start, you need to propose a problem that you would like to work on and you can't use TO-DO list.
package design; public interface Employee { /*Employee is an Interface which contains multiple unimplemented methods.Again few...
package design; public interface Employee { /*Employee is an Interface which contains multiple unimplemented methods.Again few methods has been declared in below. you need to brainstorm to add more methods to meet the business requirements. */ //please read the following method and understand the business requirements of these following methods //and then implement these in a concrete class. //employeeId() will return employee id. public int employeeId(); //employeeName() will return employee name public String employeeName(); //assignDepartment() will assign employee to departments...
Write the class Complex that supports the basic complex number operations. Such operations are addition (+),...
Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-) and multiplication (*) of complex numbers, and multiplication (*) of a complex by a scalar (float or int). All methods must return (not print) the result. Class also supports the rich comparison for equality (= =) - Check the doctest for object behavior examples. - You must use the special methods for those 4 operators in order to override their behavior -...
The StatCrunch data set for this question contains the data measurements described in Question 11. (H0...
The StatCrunch data set for this question contains the data measurements described in Question 11. (H0 : µ1 - µ2 ≤ 0 HA : µ1 - µ2 > 0) Assume that the two samples are dawn from independent, normally distributed populations that have different standard deviations. Use this data set and the results from Question 11 to calculate the p-value for the hypothesis test. Round your answer to three decimal places; add trailing zeros as needed. The p-value = [S90PValue]....
When transforming word u[1 : i] to v[1 : j], in addition to adding, removing, or...
When transforming word u[1 : i] to v[1 : j], in addition to adding, removing, or changing a letter, suppose we also allow for the swapping of adjacent letters. For example, transforming u = tera to v = tear requires a single edit that swaps the ‘r’ with the ‘a’. Provide the recurrence for d(i, j) that occurs when one performs this edit.
A data set contains the yearly tuitions in for undergraduate programs in arts and humanities at...
A data set contains the yearly tuitions in for undergraduate programs in arts and humanities at 66 universities and colleges. Tuition fees are different for domestic and international students. Suppose the mean tuition charged to domestic students was ​$5146, with a standard deviation of ​$944. For international​ students, suppose the mean was $14,504​, with a standard deviation of ​$3175. Which would be more​ unusual: a university or college with a domestic student tuition fee of ​$3000 or one with an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT