0% found this document useful (0 votes)
5 views8 pages

Tpe Word

The document presents a project implementing a generic set manipulation class called Ensemble in Java, which provides methods for checking if a set is empty, calculating its size, and performing operations like union, intersection, and complement. It utilizes Java's Set interface and HashSet for data handling, ensuring type safety through generics. The project includes implementation details, testing, and validation of the class functionalities, demonstrating its versatility for various applications involving set operations.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Tpe Word

The document presents a project implementing a generic set manipulation class called Ensemble in Java, which provides methods for checking if a set is empty, calculating its size, and performing operations like union, intersection, and complement. It utilizes Java's Set interface and HashSet for data handling, ensuring type safety through generics. The project includes implementation details, testing, and validation of the class functionalities, demonstrating its versatility for various applications involving set operations.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

TPE DE INF 213

Name : Tsem Idriss Terrance


Matriculation : 22S74689

Made by Tsem Idriss Terrance


Table Of Content

I. Project presenttion
II. Implementation
a. isEmpty
b. cardinal
c. ajouter
d. retirer
e. union
f. intersection
III. Test and validation
IV. Conclusion

Made by Tsem Idriss Terrance


I. Project presentation
This project implements a generic set manipulation class called
Ensemble, which provides various operations on sets of any
type. It includes methods to check if the set is empty (estVide),
calculate the size of the set (cardinal), and add or remove
elements (ajouter and retirer). Additionally, it supports
common set operations like calculating the union (union),
intersection (intersection), and complement (complement). The
class uses Java's Set interface and HashSet implementation to
handle the underlying data structure. This project
demonstrates the use of generics in Java to create reusable
and type-safe methods for manipulating sets of any type,
making it a versatile tool for various applications that require
set operations.

II. Implementation
//here we import the Set interface from java util

import java.util.Set;

// use hashSet to manipulate sets

import java.util.HashSet;

public class Ensemble<T> {

// private attribute ensemble, which is a set of generic elements (any type)

private Set<T> ensemble;

// constructor to build the ensemble set.

public Ensemble(Set<T> ensemble) {

this.ensemble = ensemble;

pg. 1
Made by Tsem Idriss Terrance
}

// a. returns true or false depending on if the set is empty or not.

public boolean estVide() {

return this.ensemble.isEmpty();

// b. calculates the cardinality(number of elements) of the set parsed set

public int cardinal() {

return this.ensemble.size();

// c. takes an element of any and add it from the set

public void ajouter(T e) {

this.ensemble.add(e);

// d. takes an element of any and remove it from the set

public void retirer(T e) {

this.ensemble.remove(e);

// e. intersection method takes two sets of type T (generic type)the returns a set of all
elements found in the sets

public Set<T> union(Set<T> set1, Set<T> set2) {

// creating a new set to store the union

Set<T> unionSet = new HashSet<>(set1); // Initialize with the first set

unionSet.addAll(set2); // Adds all elements from the second set

return unionSet; // Returns the union set

pg. 2
Made by Tsem Idriss Terrance
// f. intersection method takes two sets of type T (generic type)the returns a set of all elements
found in both sets

public Set<T> intersection(Set<T> set1, Set<T> set2) {

// Creates a new set to store the intersection

Set<T> intersectionSet = new HashSet<>(set1); // Initializes with the first set

intersectionSet.retainAll(set2); // Retains only the elements that are also in set2

return intersectionSet; // Returns the intersection set

// g. this method takes a set then calculates its compliment using the set we used to initialize the
class as universal set

public Set<T> complement(Set<T> complementSet) {

// Create a copy of the universal set

Set<T> complementResult = new HashSet<>(this.ensemble);

// Remove elements that are in the complementSet from the complementResult

complementResult.removeAll(complementSet);

return complementResult; // Return the complement set

pg. 3
Made by Tsem Idriss Terrance
III. Test And Validation
import java.util.Set;

// use hashSet to manipulate sets

import java.util.HashSet;

public class Main {

public static void main(String[] args) {

// Create the universal set (for example, a set of integers)

Set<Integer> universalSet = new HashSet<>();

universalSet.add(1);

universalSet.add(2);

universalSet.add(3);

universalSet.add(4);

universalSet.add(5);

// Initialize an Ensemble object with the universal set

Ensemble<Integer> ensemble = new Ensemble<>(universalSet);

// Create a couple of sets for testing union, intersection, and complement

Set<Integer> set1 = new HashSet<>();

set1.add(2);

set1.add(3);

set1.add(4);

Set<Integer> set2 = new HashSet<>();

set2.add(4);

set2.add(5);

set2.add(6);

// Test cardinality and isEmpty methods

pg. 4
Made by Tsem Idriss Terrance
System.out.println("Is the universal set empty? " + ensemble.estVide()); // false

System.out.println("Cardinality of the universal set: " + ensemble.cardinal()); // 5

// Test union operation

Set<Integer> unionSet = ensemble.union(set1, set2);

System.out.println("Union of set1 and set2: " + unionSet); // [2, 3, 4, 5, 6]

// Test intersection operation

Set<Integer> intersectionSet = ensemble.intersection(set1, set2);

System.out.println("Intersection of set1 and set2: " + intersectionSet); // [4]

// Test complement operation

Set<Integer> complementSet = ensemble.complement(set1);

System.out.println("Complement of set1 relative to the universal set: " +


complementSet); // [1, 5]

pg. 5
Made by Tsem Idriss Terrance
IV. Conclusion
In conclusion, this project successfully demonstrates the implementation of a generic Ensemble
class in Java, which supports essential set operations such as checking if a set is empty, calculating
its cardinality, adding or removing elements, and performing union, intersection, and complement
operations. By leveraging Java's Set interface and HashSet implementation, the project provides a
flexible and reusable structure for manipulating sets of any type. The use of generics ensures type
safety, while the inclusion of fundamental set operations makes it a powerful tool for various
applications that require set theory-based calculations.

pg. 6
Made by Tsem Idriss Terrance

You might also like