Please create the appropriate JUnit test cases to thoroughly unit test the following
program:
IntegerSet.java
import java.util.*;
public class IntegerSet{
private ArrayList<Integer> list= new ArrayList<>();
public IntegerSet(ArrayList list) {
this.list=list;
}
// clears the all representation of class
public void clear() {
list.clear();
}
// returns the length of set
public int length() {
return list.size();
}
// return true if 2 lists are equal
public boolean equals(IntegerSet b) {
ArrayList<Integer> list2=b.list;
if(list.size()!=list2.size()) {
return false;
}
ArrayList<Integer> temp1=list;
ArrayList<Integer> temp2=list2;
Collections.sort(temp1);
Collections.sort(temp2);
for(int i=0;i<temp1.size();i++) {
if(temp1.get(i)!=temp2.get(i)) {
return false;
}
}
return true;
}
// returns a largest item and throw exception if list is empty
public int largest() throws IntegerSetException {
if(list.size()==0) {
IntegerSetException e= new IntegerSetException();
throw e;
}
int max=0;
for(int i=0;i<list.size();i++) {
if(list.get(i)>max) {
max=list.get(i);
}
}
return max;
}
// returns a largest item and throw exception if list is empty
public int smallest() throws IntegerSetException {
if(list.size()==0) {
IntegerSetException e= new IntegerSetException();
throw e;
}
int min=Integer.MAX_VALUE;
for(int i=0;i<list.size();i++) {
if(list.get(i)<min) {
min=list.get(i);
}
}
return min;
}
// add item if already not exist in list
public void addItem(int item ) {
boolean exist=false;
for(int i=0;i<list.size();i++) {
if(list.get(i)==item) {
exist=true;
}
}
if(exist==false) {
list.add(item);
}
}
// remove item if already not exist in list
public void removeItem(int item ) {
for(int i=0;i<list.size();i++) {
if(list.get(i)==item) {
list.remove(i);
}
}
}
// return union of of 2 sets
public IntegerSet union(IntegerSet list2) {
ArrayList<Integer> temp=list2.getlist();
Set<Integer> set = new HashSet<>();
set.addAll(list);
set.addAll(temp);
ArrayList<Integer> list3= new ArrayList<>(set);
IntegerSet list4= new IntegerSet(list3);
return list4;
}
// return intersection of of 2 sets
public IntegerSet intersection(IntegerSet list2) {
ArrayList<Integer> temp=list2.getlist();
list.retainAll(temp);
IntegerSet list4= new IntegerSet(list);
return list4;
}
// returns difference of 2 sets
public IntegerSet difference (IntegerSet list2) {
ArrayList<Integer> temp=list2.getlist();
// Remove all elements in list2 from list
list.removeAll(temp);
IntegerSet list4= new IntegerSet(list);
return list4;
}
public ArrayList<Integer> getlist(){
return list;
}
}
IntegerSetException.java
class IntegerSetException extends Exception {
Driver.java
import java.util.*;
public class Driver {
public static void main(String[] args) throws IntegerSetException {
ArrayList<Integer> list1= new ArrayList<>();
ArrayList<Integer> list2= new ArrayList<>();
// make 2 instance of integerset class for implementation
IntegerSet set1= new IntegerSet(list1);
IntegerSet set2= new IntegerSet(list2);
set1.addItem(1);
set1.ad.
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Please create the appropriate JUnit test cases to thoroughly.pdf
1. Please create the appropriate JUnit test cases to thoroughly unit test the following
program:
IntegerSet.java
import java.util.*;
public class IntegerSet{
private ArrayList<Integer> list= new ArrayList<>();
public IntegerSet(ArrayList list) {
this.list=list;
}
// clears the all representation of class
public void clear() {
list.clear();
}
// returns the length of set
public int length() {
return list.size();
}
// return true if 2 lists are equal
public boolean equals(IntegerSet b) {
ArrayList<Integer> list2=b.list;
if(list.size()!=list2.size()) {
return false;
}
ArrayList<Integer> temp1=list;
ArrayList<Integer> temp2=list2;
Collections.sort(temp1);
Collections.sort(temp2);
for(int i=0;i<temp1.size();i++) {
if(temp1.get(i)!=temp2.get(i)) {
return false;
}
}
return true;
}
// returns a largest item and throw exception if list is empty
public int largest() throws IntegerSetException {
if(list.size()==0) {
IntegerSetException e= new IntegerSetException();
throw e;
}
int max=0;
for(int i=0;i<list.size();i++) {
2. if(list.get(i)>max) {
max=list.get(i);
}
}
return max;
}
// returns a largest item and throw exception if list is empty
public int smallest() throws IntegerSetException {
if(list.size()==0) {
IntegerSetException e= new IntegerSetException();
throw e;
}
int min=Integer.MAX_VALUE;
for(int i=0;i<list.size();i++) {
if(list.get(i)<min) {
min=list.get(i);
}
}
return min;
}
// add item if already not exist in list
public void addItem(int item ) {
boolean exist=false;
for(int i=0;i<list.size();i++) {
if(list.get(i)==item) {
exist=true;
}
}
if(exist==false) {
list.add(item);
}
}
// remove item if already not exist in list
public void removeItem(int item ) {
for(int i=0;i<list.size();i++) {
if(list.get(i)==item) {
list.remove(i);
}
}
}
// return union of of 2 sets
public IntegerSet union(IntegerSet list2) {
3. ArrayList<Integer> temp=list2.getlist();
Set<Integer> set = new HashSet<>();
set.addAll(list);
set.addAll(temp);
ArrayList<Integer> list3= new ArrayList<>(set);
IntegerSet list4= new IntegerSet(list3);
return list4;
}
// return intersection of of 2 sets
public IntegerSet intersection(IntegerSet list2) {
ArrayList<Integer> temp=list2.getlist();
list.retainAll(temp);
IntegerSet list4= new IntegerSet(list);
return list4;
}
// returns difference of 2 sets
public IntegerSet difference (IntegerSet list2) {
ArrayList<Integer> temp=list2.getlist();
// Remove all elements in list2 from list
list.removeAll(temp);
IntegerSet list4= new IntegerSet(list);
return list4;
}
public ArrayList<Integer> getlist(){
return list;
}
}
IntegerSetException.java
class IntegerSetException extends Exception {
Driver.java
import java.util.*;
public class Driver {
public static void main(String[] args) throws IntegerSetException {
ArrayList<Integer> list1= new ArrayList<>();
ArrayList<Integer> list2= new ArrayList<>();
// make 2 instance of integerset class for implementation
IntegerSet set1= new IntegerSet(list1);
IntegerSet set2= new IntegerSet(list2);
set1.addItem(1);
set1.addItem(3);
set1.addItem(4);
set1.addItem(6);
4. set1.addItem(3);
set1.addItem(8);
set1.addItem(7);
set2.addItem(1);
set2.addItem(2);
set2.addItem(3);
set2.addItem(4);
set2.addItem(5);
set2.addItem(6);
set2.addItem(7);
System.out.println("Set 1 : "+set1.getlist());
System.out.println("Set 2 : "+set2.getlist());
System.out.println("Is equal : "+set1.equals(set2));
System.out.println("Largest element in set1 : "+set1.largestelement());
System.out.println("Smallest element in set2 : "+set2.smallestelement());
IntegerSet setu= set1.union(set2);
System.out.println("Unoin : "+setu.getlist() );
IntegerSet seti= set1.intersection(set2);
System.out.println("Intersection : "+seti.getlist() );
}
}
The JUnit test class should be named IntegerSetTest to be consistent with standard Java
naming conventions. In addition, each method should be named in the format of testXXXX. For
example, if testing the equals(Boolean) method it would look like
public class IntegerSetTest {
@Test
@DisplayName("Test case for equals")
public void testEquals() {
// JUnit test case(s) for equal
}
}
Sufficiently test each public method, you will be graded on how robust your JUnit test cases
are. Note that methods largest() and smallest() potentially throw an exception if invoked and your
IntegerSet is empty. You are to add to add exception handling to these methods and write
JUnit test cases to test the exceptions.