Sagewire Logo

Sending data from one class to another

2 Message(s) by 2 Author(s) originally posted in java help


From: sdlt85 Date:   Thursday, October 18, 2007
Hi, I want to send the values that the user enters and I am storing
them in several array s in the IntegerSetTest class . It need to be
sended to the IntergerSet class to find the union, int ersection, etc.

Here is IntegerSetTest class:
import JAVA.util.*;
public class IntegerSetTest
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String inputValue;

String strArray1[] = new String[100];
int countInts = 0;
for(int I = 0; i < strArray1.length; i++)
{
System.out.println("Enter integers for set A. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray1[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray1[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray1[i] = Integer.parseInt(strArray1[i]);

String strArray2[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray2.length; i++)
{
System.out.println("Enter integers for set B. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray2[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray2[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray2[i] = Integer.parseInt(strArray2[i]);

String strArray3[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray3.length; i++)
{
System.out.println("Enter integers for set C. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray3[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray3[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray3[i] = Integer.parseInt(strArray3[i]);

String strArray4[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray4.length; i++)
{
System.out.println("Enter integers for set D. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray4[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray4[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray4[i] = Integer.parseInt(strArray4[i]);
}

And here is IntergerSet class:
import JAVA.util.Scanner;

public class IntergerSet
{
private static final int SIZE = 101;
private boolean[] arraySet = new boolean[SIZE];
private boolean validEntry(int k)
{
return k>
=0 && k<=SIZE;
}

// No-arguments.
public IntergerSet()
{
for(int i=0; i<SIZE; i++)
arraySet[i] = false;
}

//overloading
public IntergerSet(int arraySet[])
{
//obtain the values of the set that the user enter.
}

public IntergerSet union(IntergerSet other)
{
IntergerSet res = new IntergerSet();

for(int i=0; i<SIZE; i++)
res.arraySet[i] = this.arraySet[i]||other.arraySet[i];
return res;
}

public IntergerSet intersection(IntergerSet other)
{
IntergerSet res = new IntergerSet();

for(int i=0; i<SIZE; i++)
res.arraySet[i] = this.arraySet[i]&&other.arraySet[i];
return res;
}
}

Thanks


From: Lew Date:   Thursday, October 18, 2007
wrote in message:
Hi, I want to send the values that the user enters and I am storing
them in several arrays in the IntegerSetTest class. It need to be
sended to the IntergerSet class to find the union, intersection, etc.



I do not know what you're asking. Are you asking how to pass values to a
method of another class? You do it by making a method call with values in the
argument list, e.g.,

SomeFoo foo = new SomeFoo();
foo.someMethod( 23, 23.0, "23" );

Or, if you are passing variables,

foo.someMethod( intValue, doubValue, anotherValue );

Here is IntegerSetTest class:



You call it "Integer" here and "Interger" later, just noticing.

import JAVA.util.*;
public class IntegerSetTest
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String inputValue;
String strArray1[] = new String[100];
int countInts = 0;
for(int I = 0; i < strArray1.length; i++)
{
System.out.println("Enter integers for set A. (Press any
other key to finish set). ");



This println() call will happen every time through the loop.

inputValue = input.next();
try
{
Integer.parseInt(inputValue);



You parse an int out of the inputValue, then you throw it away. then you
parse it again later. You could skip all that fooferol by parsing the int and
putting it right into the int array (which really should not be called
"intArray", but that's another matter). This will eliminate the need for the
String array.

strArray1[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{



You can use Exceptions to control logic flow like this, but it is not recommended.

break;
}
}
int intArray1[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray1[i] = Integer.parseInt(strArray1[i]);



Yep, here's that second parseInt() of the same data.

String strArray2[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray2.length; i++)
{
System.out.println("Enter integers for set B. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray2[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray2[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray2[i] = Integer.parseInt(strArray2[i]);
String strArray3[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray3.length; i++)
{
System.out.println("Enter integers for set C. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray3[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray3[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray3[i] = Integer.parseInt(strArray3[i]);
String strArray4[] = new String[100];
countInts = 0;
for(int I = 0; I < strArray4.length; i++)
{
System.out.println("Enter integers for set D. (Press any
other key to finish set). ");
inputValue = input.next();
try
{
Integer.parseInt(inputValue);
strArray4[i] = inputValue;
countInts++;
}
catch(NumberFormatException ex)
{
break;
}
}
int intArray4[] = new int[countInts];
for(int I = 0; I < countInts; i++)
intArray4[i] = Integer.parseInt(strArray4[i]);
}



After watching the code do the same thing four times, it should make you want
to write a method that captures that logic, and call it four times.

And here is IntergerSet class:



You really should have named it with conventional spelling.

import JAVA.util.Scanner;
public class IntergerSet
{
private static final int SIZE = 101;
private boolean[] arraySet = new boolean[SIZE];



FYI, this array is now completely populated with 'false' values.

private boolean validEntry(int k)
{
return k>=0 && k<=SIZE;
}
// No-arguments.
public IntergerSet()
{
for(int i=0; i<SIZE; i++)
arraySet[i] = false;



This loop, besides missing its braces ('{' and '}'), is completely
unnecessary. It is setting values to 'false' that already were 'false'.

}
//overloading
public IntergerSet(int arraySet[])
{
//obtain the values of the set that the user enter.



Show this logic. Make sure to protect against mismatches with SIZE.

}
public IntergerSet union(IntergerSet other)
{
IntergerSet res = new IntergerSet();
for(int i=0; i<SIZE; i++)
res.arraySet[i] = this.arraySet[i]||other.arraySet[i];
return res;
}
public IntergerSet intersection(IntergerSet other)
{
IntergerSet res = new IntergerSet();
for(int i=0; i<SIZE; i++)
res.arraySet[i] = this.arraySet[i]&&other.arraySet[i];
return res;
}
}



--
Lew



Next Message: How to make a JTree leaf look like a folder


Blogs related to Sending data from one class to another

Java - Networking snippets
And when each computer can send data to the other, synchronizing communication can be a problem. One of the standard Java packages is called java.net. This package includes several classes that can be used for networking. ...

About Java
When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: data members (sometimes called fields), ...

I want to blog take two classes A and B that each have unique ...
However, when I create my Tk() instance in one file, then create another with a class to populate the window, the image won’t display. http://www.pythonware.com/library/tkinter/introduction/hello-again.htm ...

Java Tutorials
a) Integer is a class defined in the java. lang package, whereas int is a primitive data type defined in the Java language itself. Java does not automatically convert from one to the other. b) Integer can be used as an argument for a ...

The Coolest Leopard Features
Organize your activities into separate spaces and easily switch from one to another. Make a space for work or play. Choose from a number of convenient options that make moving from space to space fast and easy. Search Shared Macs. ...

[mule-scm] [mule] [9115] branches/message-api-refactor/core/src ...
getAttachmentNames(); } class='rem'>- /** - * Gets the encoding for the current message. For potocols that send encoding - * Information with the message, this method should be overriden to expose the - * transport encoding ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional