Returning new object, overwrite the existing one in Java
Posted
by lupin
on Stack Overflow
See other posts from Stack Overflow
or by lupin
Published on 2010-05-20T09:21:54Z
Indexed on
2010/05/20
9:30 UTC
Read the original article
Hit count: 343
Note: This is an assignment.
Hi,
Ok I have this method that will create a supposedly union of 2 sets.
i
mport java.io.*;
class Set {
public int numberOfElements;
public String[] setElements;
public int maxNumberOfElements;
// constructor for our Set class
public Set(int numberOfE, int setE, int maxNumberOfE) {
this.numberOfElements = numberOfE;
this.setElements = new String[setE];
this.maxNumberOfElements = maxNumberOfE;
}
// Helper method to shorten/remove element of array since we're using basic array instead of ArrayList or HashSet from collection interface :(
static String[] removeAt(int k, String[] arr) {
final int L = arr.length;
String[] ret = new String[L - 1];
System.arraycopy(arr, 0, ret, 0, k);
System.arraycopy(arr, k + 1, ret, k, L - k - 1);
return ret;
}
int findElement(String element) {
int retval = 0;
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] != null && setElements[i].equals(element) ) {
return retval = i;
}
retval = -1;
}
return retval;
}
void add(String newValue) {
int elem = findElement(newValue);
if( numberOfElements < maxNumberOfElements && elem == -1 ) {
setElements[numberOfElements] = newValue;
numberOfElements++;
}
}
int getLength() {
if ( setElements != null ) {
return setElements.length;
}
else {
return 0;
}
}
String[] emptySet() {
setElements = new String[0];
return setElements;
}
Boolean isFull() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if ( setElements.length == maxNumberOfElements ){
return True;
} else { return False; }
}
Boolean isEmpty() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if ( setElements.length == 0 ) {
return True;
} else { return False; }
}
void remove(String newValue) {
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] != null && setElements[i].equals(newValue) ) {
setElements = removeAt(i,setElements);
}
}
}
int isAMember(String element) {
int retval = -1;
for ( int i = 0; i < setElements.length; i++ ) {
if (setElements[i] != null && setElements[i].equals(element)) {
return retval = i;
}
}
return retval;
}
void printSet() {
for ( int i = 0; i < setElements.length; i++) {
if (setElements[i] != null) {
System.out.println("Member elements on index: "+ i +" " + setElements[i]);
}
}
}
String[] getMember() {
String[] tempArray = new String[setElements.length];
for ( int i = 0; i < setElements.length; i++) {
if(setElements[i] != null) {
tempArray[i] = setElements[i];
}
}
return tempArray;
}
Set union(Set x, Set y) {
String[] newXtemparray = new String[x.getLength()];
String[] newYtemparray = new String[y.getLength()];
int len = newYtemparray.length + newXtemparray.length;
Set temp = new Set(0,len,len);
newXtemparray = x.getMember();
newYtemparray = x.getMember();
for(int i = 0; i < newYtemparray.length; i++) {
temp.add(newYtemparray[i]);
}
for(int j = 0; j < newXtemparray.length; j++) {
temp.add(newXtemparray[j]);
}
return temp;
}
Set difference(Set x, Set y) {
String[] newXtemparray = new String[x.getLength()];
String[] newYtemparray = new String[y.getLength()];
int len = newYtemparray.length + newXtemparray.length;
Set temp = new Set(0,len,len);
newXtemparray = x.getMember();
newYtemparray = x.getMember();
for(int i = 0; i < newXtemparray.length; i++) {
temp.add(newYtemparray[i]);
}
for(int j = 0; j < newYtemparray.length; j++) {
int retval = temp.findElement(newYtemparray[j]);
if( retval != -1 ) {
temp.remove(newYtemparray[j]);
}
}
return temp;
}
}
// This is the SetDemo class that will make use of our Set class
class SetDemo {
public static void main(String[] args) {
//get input from keyboard
BufferedReader keyboard;
InputStreamReader reader;
String temp = "";
reader = new InputStreamReader(System.in);
keyboard = new BufferedReader(reader);
try
{
System.out.println("Enter string element to be added" );
temp = keyboard.readLine( );
System.out.println("You entered " + temp );
}
catch (IOException IOerr)
{
System.out.println("There was an error during input");
}
/*
**************************************************************************
* Test cases for our new created Set class.
*
**************************************************************************
*/
Set setA = new Set(0,10,10);
setA.add(temp);
setA.add("b");
setA.add("b");
setA.add("hello");
setA.add("world");
setA.add("six");
setA.add("seven");
setA.add("b");
int size = setA.getLength();
System.out.println("Set size is: " + size );
Boolean isempty = setA.isEmpty();
System.out.println("Set is empty? " + isempty );
int ismember = setA.isAMember("sixb");
System.out.println("Element sixb is member of setA? " + ismember );
Boolean output = setA.isFull();
System.out.println("Set is full? " + output );
//setA.printSet();
int index = setA.findElement("world");
System.out.println("Element b located on index: " + index );
setA.remove("b");
//setA.emptySet();
int resize = setA.getLength();
System.out.println("Set size is: " + resize );
//setA.printSet();
Set setB = new Set(0,10,10);
setB.add("b");
setB.add("z");
setB.add("x");
setB.add("y");
Set setC = setA.union(setB,setA);
System.out.println("Elements of setA");
setA.printSet();
System.out.println("Union of setA and setB");
setC.printSet();
}
}
The union method works a sense that somehow I can call another method on it but it doesn't do the job, i supposedly would create and union of all elements of setA and setB but it only return element of setB. Sample output follows:
java SetDemo
Enter string element to be added
hello
You entered hello
Set size is: 10
Set is empty? false
Element sixb is member of setA? -1
Set is full? true
Element b located on index: 2
Set size is: 9
Elements of setA
Member elements on index: 0 hello
Member elements on index: 1 world
Member elements on index: 2 six
Member elements on index: 3 seven
Union of setA and setB
Member elements on index: 0 b
Member elements on index: 1 z
Member elements on index: 2 x
Member elements on index: 3 y
thanks, lupin
© Stack Overflow or respective owner