How to send data from one class to another class

  • 13 years ago

    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.

    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

  • 13 years ago
    Make a set method in IntegerSet Class to receive such integer arrays from your main class... Make a global integer array variable for your IntegerSet class... from your main: IntegerSet is = new IntegerSet(); is.setDataFromUser(Array1[],Array2[],Array3[],Array[4]); from your method setDataFromUser.... public void setDataFromUser(int Array1[],int Array2[],int Array3[], int Array4[]){ //Transfer temp int array into your global int variable array... // Use it... } Here's the sample... import javax.swing.JOptionPane; public class sample{ public static void main(String args[]){ int array[] = new int[5]; for(int x=0;xDevil; public void set(int array[]){ for(int x=0;x

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The question of whether computers can think is just like the question of whether submarines can swim.” - Edsger W. Dijkstra