Re: Calling a method
Sorry, I was having problems with my internet, but here is my code:
import java.util.Scanner;
public class IntergerSet
{
private static final int SIZE = 101;
private boolean[] arraySet = new boolean[SIZE];
// No-arguments on the array.
public IntergerSet()
{
for(int i=0; i<SIZE; i++)
{
arraySet[i] = false;
}
}
//overloading constructor
public IntergerSet(int j)
{
if(validEntry(j))
insert(j);
}
//check valid entry not over 100.
private boolean validEntry(int k)
{
return k>=0 && k<=SIZE;
}
//Getting input from the user.
public void inputSet ()
{
Scanner input = new Scanner (System.in);
int value;
boolean arraySet[] = new boolean[100];
do
{
System.out.println("\nPlease enter a value or -1 to finish: ");
value = input.nextInt();
if(validEntry(value))
arraySet[value] = true;
else
System.out.println("\nInvalid value");
}while(value != -1);
}
//Finding the union of two sets
public void union(boolean other[])
{
boolean res[] = new boolean [SIZE];
for(int i=0; i<SIZE; i++)
if(arraySet[i]==true||other[i]==true)
res[i] = true;
arraySet = res;
}
//Finding the intersection of two sets
public void intersection(boolean other[])
{
boolean res[] = new boolean [SIZE];
for(int i=0; i<SIZE; i++)
if(arraySet[i]==true&&other[i]==true)
res[i] = true;
arraySet = res;
}
//Inserting a value to the set
public void insert(int other)
{
if(validEntry(other))
{
System.out.printf("\nYou add %d ", other);
arraySet[other]=true;
}
else
System.out.print("\nInvalid insert attempted\n");
}
//Deleting a value from the set
public void delete(int other)
{
if(validEntry(other))
{
System.out.printf("\nYou delete %d ", other);
arraySet[other]=false;
}
else
System.out.print("\nInvalid delete attempted\n");
}
//Prints the sets in a string.
public String toSetString()
{
String setString = "{ ";
boolean empty = true;
for(int s=0; s<SIZE; s++)
{
if(arraySet[s])
{
setString = setString + s + " ";
empty = false;
}
}
if(empty)
setString = "---";
setString = setString + " }";
return setString;
}
//Checks if the sets are equal
public void isEqualTo(boolean other[])
{
for(int t=0; t<SIZE; t++)
if(arraySet[t] != other[t])
return false;
return true;
}
}
import java.util.Scanner;
public class IntegerSetTest
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
boolean arraySetA[] = new boolean[101];
boolean arraySetB[] = new boolean[101];
boolean arraySetC[] = new boolean[101];
{
final int[] init = {6, 7, 9, 50, 69, 86};
for (int i = 0; i < init.length; ++i)
arraySetC[init[i]] = true;
}
boolean arraySetD[] = new boolean[101];
{
final int[] init = {7, 9, 13, 14, 15, 19, 37, 50, 72, 74};
for (int i = 0; i < init.length; ++i)
arraySetD[init[i]] = true;
}
int i, j;
for(i=0; i<101; i++)
{
j=i*2;
arraySetA[j] = true;
}
for(i=1; i<101; i=i+2)
{
arraySetA[i] = true;
}
System.out.println("Welcome to the Integer Set\n");
System.out.println("This program is going to find the union or "
+
"intersection of any two sets you enter.\nAlso it will delete "
+
"or insert an element and check if the sets are equal.");
IntergerSet set = new IntergerSet();
set.inputSet();
System.out.println("\n\nIf you want to add a value to the set
press 'a': ");
String adding = input.next();
if("a".equalsIgnoreCase(adding))
{
System.out.printf("You are going to add a value to the set\n");
System.out.print("\nEnter the value to be added: ");
int addValue = input.nextInt();
set.insert(addValue);
}
System.out.println("\n\nIf you want to delete a value from the
set press 'd': ");
String deleting = input.next();
if("d".equalsIgnoreCase(deleting))
{
System.out.printf("\nYou are going to delete a value from the set
\n");
System.out.print("\nEnter the value to be deleted: ");
int deleteValue = input.nextInt();
set.delete(deleteValue);
}
System.out.print("Enter 'u' to find the union\nEnter 'i' to find the
intersection\n" +
"Enter 'c' to compare the sets\nIf not enter 'n'");
String option = input.next();
if("u".equalsIgnoreCase(option))
{
set.union(arraySetB);
}
if("i".equalsIgnoreCase(option))
{
set.intersection(arraySetA);
}
if("c".equalsIgnoreCase(option))
{
set.isEqualTo(arraySetC);
}
else if("n".equalsIgnoreCase(option))
{
System.out.println("Thanks for using my program\n");
}
set.toSetString();
}
}
Thanks