Question: USING C# USING C# USING C# CAN YOU CHANGE THIS jAVA PROGRAM IN C# PLEASE?? THANK YOU. import java.util.ArrayList;
" USING C# " " USING C# " " USING C# "
CAN YOU CHANGE THIS jAVA PROGRAM IN C# PLEASE??
THANK YOU.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Set {
static Scanner scanner = new Scanner(System.in);
ArrayList
public Set(Integer[] array) {
for(Integer i: array) {
addElement(i);
}
}
public Set(int[] array) {
for(Integer i: array) {
addElement(i);
}
}
public Set() {
}
public void addElement(int i) {
if(!list.contains(i)) {
list.add(i);
}
}
public void removeElement(int i) {
if(!list.contains(i)) {
list.remove(new Integer(i));
}
}
public void clear() {
list.clear();
}
public void print() {
System.out.println(Arrays.toString(list.toArray()));
}
@Override
public String toString() {
return Arrays.toString(list.toArray());
}
public boolean isElement(int i) {
return list.contains(i);
}
public Integer[] toArray() {
Integer[] temp = list.toArray(new Integer[list.size()]);
return temp;
}
public Set union(Set other) {
Set a = new Set(other.toArray());
for(Integer elem: list){
a.addElement(elem);
}
return a;
}
public Set intersection(Set other) {
Set a = new Set();
for(Integer elem: list){
if(other.isElement(elem)) {
a.addElement(elem);
}
}
return a;
}
public Set difference(Set other) {
Set a = new Set();
for(Integer elem: list){
if(!other.isElement(elem)) {
a.addElement(elem);
}
}
return a;
}
public boolean isSubset(Set other) {
boolean result = true;
for(Integer elem: list){
if(!other.isElement(elem)) {
result = false;
break;
}
}
return result;
}
public boolean isEmpty() {
return list.isEmpty();
}
public int getCardinality() {
return list.size();
}
public int getCartesianProduct(Set other) {
int product = 0;
for(Integer outer: list){
for(Integer inner: other.toArray()){
product += outer * inner;
}
}
return product;
}
public boolean isEqual(Set other) {
Set union = this.union(other);
if(this.getCardinality()==other.getCardinality() && union.getCardinality() == this.getCardinality()) {
return true;
} else {
return false;
}
}
public static int getInteger(String msg) {
System.out.print(msg + ": ");
return Integer.parseInt(scanner.nextLine());
}
public static int getChoice() {
int choice = 0;
System.out.println("1. Add an element to Set1");
System.out.println("2. Add an element to Set2");
System.out.println("3. Remove an element to Set1");
System.out.println("4. Remove an element to Set2");
System.out.println("5. Cardinality of Set1");
System.out.println("6. Cardinality of Set2");
System.out.println("7. Set1 - Set2");
System.out.println("8. Set2 - Set1");
System.out.println("9. Cartesian product of Set1 and Set2");
System.out.println("10. is S1 subset of S2");
System.out.println("11. is S2 subset of S1");
System.out.println("12. union");
System.out.println("13. intersection");
System.out.println("14. S1==S2");
System.out.println("15. Print both");
System.out.println("16. exit");
System.out.print("Enter your choice: ");
String error = "";
while (choice < 1 || choice > 16) {
System.out.print(error);
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
}
error = "Please enter a number between " + 1 + " and " + 16;
}
return choice;
}
public static void main(String[] args) {
Set s1 = new Set(new int[]{3,5,4,3});
Set s2 = new Set(new int[]{3,7,5,12});
int choice = 15;
while(choice != 16) {
switch(choice) {
case 1:
s1.addElement(getInteger("Enter a integer to add"));
break;
case 2:
s2.addElement(getInteger("Enter a integer to add"));
break;
case 3:
s1.removeElement(getInteger("Enter a integer to remove"));
break;
case 4:
s2.removeElement(getInteger("Enter a integer to remove"));
break;
case 5:
System.out.println("Cardinality of Set1: " + s1.getCardinality());
break;
case 6:
System.out.println("Cardinality of Set2: " + s2.getCardinality());
break;
case 7:
System.out.println("Set1 - Set2: " + s1.difference(s2));
break;
case 8:
System.out.println("Set2 - Set1: " + s2.difference(s1));
break;
case 9:
System.out.println("Cartesian Product: " + s1.getCartesianProduct(s2));
break;
case 10:
System.out.println("s1.isSubset(s2): " + s1.isSubset(s2));
break;
case 11:
System.out.println("s2.isSubset(s1): " + s2.isSubset(s1));
break;
case 12:
System.out.println("Union: " + s2.union(s1));
break;
case 13:
System.out.println("Intersection: " + s2.intersection(s1));
break;
case 14:
System.out.println("S1==S2: " + s1.isEqual(s2));
break;
case 15:
System.out.println("S1: " + s1);
System.out.println("S2: " + s2);
break;
}
System.out.println();
choice = getChoice();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
