Guys any idea how to implement the said code...

Implement the IMultiSet interface below into MultiSet class

``````public class IMultiSet{

// add x to this multiset (x must be 0-9)

// remove x from this multiset if there are occurrences of element x (x must be 0-9)

void remove(int x);

// return another multiset containing the sum of this multiset and S

public IMultiSet sum(IMultiSet S);

// return another multiset containing the intersection of this multiset and S

public IMultiSet intersection(IMultiSet S);

// return another multiset containing the difference of this multiset and S

public IMultiSet difference(IMultiSet S);

}``````

Note: Override the toString

// return the string equivalent of this multiset in format {x:a, y:b, z:c }

## All 12 Replies

Do you have any code you are having problems with?

Post them here.

``public class IMultiSet{``

This looks like a class definition not an interface.

Ok sir ...
Here's my code :

``````public class IMultiSet{
private int multiset[];

public IMultiSet(){
multiset = new int;
}

// add x to this multiset
if(x < multiset.length && x >= 0)
multiset[x]++;
}

// remove x from this multiset if there are occurrences of x is
void remove(int x){
if(x < multiset.length && x >= 0)
if(multiset[x]>0) multiset[x]--;

}

//**The union of the two multisets is the multiset
//**where the occurrences of an element is maximum
// return another multiset containing the union of this multiset and S
public IMultiSet sum(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 || S.multiset[i]!=0){
if(this.multiset[i] >= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

//**The intersection of two multisets is the multiset
//**where the occurrences of an element is minimum
// return another multiset containing the intersection of this multiset and S
public IMultiSet intersection(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] <= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

//**The difference of two multisets are the occurrence of element x in P minus occurrence of x in Q.
//** If the result is negative then the occurrence of the element x in the difference is 0.
// return another multiset containing the difference of this multiset and S
public IMultiSet difference(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]==0)
tmp.multiset[i] = this.multiset[i];
else if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] - S.multiset[i] <= 0)
tmp.multiset[i] = 0;
else
tmp.multiset[i] = this.multiset[i] - S.multiset[i];
}

}
return tmp;

}

// return the string equivalent of this multiset in format {x:a, y:b, z:c }
public String toString(){
String tmp = "{ ";
for(int i=0;i<multiset.length;i++){
if(this.multiset[i] != 0){
tmp+= i + ":" + this.multiset[i] + ",";
}
}
tmp+= "\b }";
return tmp;
}

public static void main(String []args){
IMultiSet P = new IMultiSet();

IMultiSet Q = new IMultiSet();

System.out.println("P = " + P.toString());
System.out.println("Q = " + Q.toString());
System.out.println("P U Q = " + P.sum(Q));
System.out.println("P intersection Q = " + (P.intersection(Q)).toString());
System.out.println("P - Q = " + (P.difference(Q)).toString());

}

}``````

Kindly check if there's any problem ...
Thank you..

The problem is how to interchange the numbers that has been inputted..
For example, the changing of numbers of P and Q...

Kindly check if there's any problem ...

What happens when you execute it?

Please edit your code and wrap it in code tags (see [code] icon above input box)

``````public class IMultiSet{
private int multiset[];

public IMultiSet(){
multiset = new int;
}

// add x to this multiset
if(x < multiset.length && x >= 0)
multiset[x]++;
}

// remove x from this multiset if there are occurrences of x is
void remove(int x){
if(x < multiset.length && x >= 0)
if(multiset[x]>0) multiset[x]--;

}

public IMultiSet sum(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 || S.multiset[i]!=0){
if(this.multiset[i] >= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet intersection(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] <= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet difference(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]==0)
tmp.multiset[i] = this.multiset[i];
else if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] - S.multiset[i] <= 0)
tmp.multiset[i] = 0;
else
tmp.multiset[i] = this.multiset[i] - S.multiset[i];
}

}
return tmp;

}

// return the string equivalent of this multiset in format {x:a, y:b, z:c }
public String toString(){
String tmp = "{ ";
for(int i=0;i<multiset.length;i++){
if(this.multiset[i] != 0){
tmp+= i + ":" + this.multiset[i] + ",";
}
}
tmp+= "\b }";
return tmp;
}

public static void main(String []args){
IMultiSet P = new IMultiSet();

IMultiSet Q = new IMultiSet();

System.out.println("The value of P");
System.out.println("P = " + P.toString());
System.out.println("The value of Q");
System.out.println("Q = " + Q.toString());
System.out.println("\nP U Q = " + P.sum(Q));
System.out.println("P Intersection Q = " + (P.intersection(Q)).toString());
System.out.println("P - Q = " + (P.difference(Q)).toString());

}

}``````

What happened? Your code looks the same. Did you select the code and press the (CODE) icon?
It should look like this:

``````public static void main(String []args){
IMultiSet P = new IMultiSet();
...
``````
``````public class IMultiSet{
private int multiset[];

public IMultiSet(){
multiset = new int;
}

// add x to this multiset
if(x < multiset.length && x >= 0)
multiset[x]++;
}

// remove x from this multiset if there are occurrences of x is
void remove(int x){
if(x < multiset.length && x >= 0)
if(multiset[x]>0) multiset[x]--;

}

public IMultiSet sum(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 || S.multiset[i]!=0){
if(this.multiset[i] >= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet intersection(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] <= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet difference(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]==0)
tmp.multiset[i] = this.multiset[i];
else if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] - S.multiset[i] <= 0)
tmp.multiset[i] = 0;
else
tmp.multiset[i] = this.multiset[i] - S.multiset[i];
}

}
return tmp;

}

// return the string equivalent of this multiset in format {x:a, y:b, z:c }
public String toString(){
String tmp = "{ ";
for(int i=0;i<multiset.length;i++){
if(this.multiset[i] != 0){
tmp+= i + ":" + this.multiset[i] + ",";
}
}
tmp+= "\b }";
return tmp;
}

public static void main(String []args){
IMultiSet P = new IMultiSet();

IMultiSet Q = new IMultiSet();

System.out.println("The value of P");
System.out.println("P = " + P.toString());
System.out.println("The value of Q");
System.out.println("Q = " + Q.toString());
System.out.println("\nP U Q = " + P.sum(Q));
System.out.println("P Intersection Q = " + (P.intersection(Q)).toString());
System.out.println("P - Q = " + (P.difference(Q)).toString());

}

}``````

Nothing has changed.

``````public class IMultiSet{
private int multiset[];

public IMultiSet(){
multiset = new int;
}

// add x to this multiset
if(x < multiset.length && x >= 0)
multiset[x]++;
}

// remove x from this multiset if there are occurrences of x is
void remove(int x){
if(x < multiset.length && x >= 0)
if(multiset[x]>0) multiset[x]--;

}

public IMultiSet sum(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 || S.multiset[i]!=0){
if(this.multiset[i] >= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet intersection(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] <= S.multiset[i])
tmp.multiset[i] = this.multiset[i];
else
tmp.multiset[i] = S.multiset[i];
}
}
return tmp;
}

public IMultiSet difference(IMultiSet S){
IMultiSet tmp = new IMultiSet();
for(int i=0;i<multiset.length;i++){
if(this.multiset[i]!=0 && S.multiset[i]==0)
tmp.multiset[i] = this.multiset[i];
else if(this.multiset[i]!=0 && S.multiset[i]!=0){
if(this.multiset[i] - S.multiset[i] <= 0)
tmp.multiset[i] = 0;
else
tmp.multiset[i] = this.multiset[i] - S.multiset[i];
}

}
return tmp;

}

// return the string equivalent of this multiset in format {x:a, y:b, z:c }
public String toString(){
String tmp = "{ ";
for(int i=0;i<multiset.length;i++){
if(this.multiset[i] != 0){
tmp+= i + ":" + this.multiset[i] + ",";
}
}
tmp+= "\b }";
return tmp;
}

public static void main(String []args){
IMultiSet P = new IMultiSet();

IMultiSet Q = new IMultiSet();

System.out.println("The value of P");
System.out.println("P = " + P.toString());
System.out.println("The value of Q");
System.out.println("Q = " + Q.toString());
System.out.println("\nP U Q = " + P.sum(Q));
System.out.println("P Intersection Q = " + (P.intersection(Q)).toString());
System.out.println("P - Q = " + (P.difference(Q)).toString());

}

}``````

Not making much progress. Still looks the same.
What happens when you do an Edit on your last post, select the code and then press the [CODE] icon?

Can you see what you've done after you press the Save button? Does it look like this;

``````public static void main(String []args){
IMultiSet P = new IMultiSet();