need to pass a parameter to my class for my class to use....

i have one class that receives a string to and from a serial port and i have another class that uses the value received to calculate something and sends it back to the other class to send back to the port.

here is how i defined both classes in the beg.

public class Data extends Thread

and

public class Calculator extends Thread

the Data class has these two functions that i need to use.

get_readData()

and

set_sendData(String)


I'm not quite sure how i should go about integrating these two strings i have...

I want the Calculator class to receive two Data objects to be able to use.

for example, in the calculator class i want to use

string something = A.get_readData();

and

B.set_sendData(something);


but i know i can't do this...

public class Calculator(Data A, Data B) extends Thread

so can someone please help me figure this problem out?
i'm very new to programming so some explaination would be very helpful

cheers
LVL 1
shpark82Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

javaoptimizerCommented:
create an object of Data class and  Calculator class and then synchronize that Calculator object and call data objects methods in it appropriately.

i.e

Class runCalc
{

public static void main()
{
      Data  data = new Data();
      Calculator cal = new Calculator();
      Synchronized(cal)
     {
             call  data.get_readData();

             do whatever calculation you want on the cal object

             set_sendData(String);
     }

}
}

OR

 you can separate the get_readData() and set_sendData(String) based on a mutex variable on the object data w.r.t cal object.

I think first option will do the work for you
Mayank SAssociate Director - Product EngineeringCommented:
The Calculator need not be a sub-class of Thread. You can do this:

In Data:

Calculator calc = new Calculator () ;
String data = get_readData () ;
calc.doSomethingToData ( data, this ) ;

In Calculator:

public class Calculator
{
  public void doSomethingToData ( String data, Data handle )
  {
    // process the data and get the data to be sent in a String called 'newData'
    handle.set_sendData ( newData ) ;
  }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shpark82Author Commented:
what if all i wanted to make changes to was the calculator class and not the data class?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Mayank SAssociate Director - Product EngineeringCommented:
You can also use:

In Data:

Calculator calc = new Calculator () ;
String data = get_readData () ;
String newData = calc.doSomethingToData ( data ) ;

In Calculator:

public class Calculator
{
  public String doSomethingToData ( String data )
  {
    // process the data and get the data to be sent in a String called 'newData'
    return newData ;
  }
}
Mayank SAssociate Director - Product EngineeringCommented:
>> what if all i wanted to make changes to was the calculator class and not the data class?

What exactly is the requirement? You need to read data in the Data class, send it to the Calculator class for doing some processing and in the process, getting a new set of data to send to the port, then send it back - right?
shpark82Author Commented:
yes correct

but data class does a bit of other things as well..such as updating the gui and changing the information on the screen etc

so i kind of want to leave data as is...
Mayank SAssociate Director - Product EngineeringCommented:
You need to do something to it to effect the change :) I mean, to make it adapt to the change that you do in Calculator. Just that the change should be minimal. So analyze the pieces of code in the Data class where you are calling the Calculator class and those will be the only places which will change. The rest of the Data class, no matter how long it is and whatever it does, can remain the same.
shpark82Author Commented:
well in my calculator class, i have

run() function that does the execution of the functions in my class..
Mayank SAssociate Director - Product EngineeringCommented:
You don't need a run () over there because I guess Calculator is not supposed to be a thread but is only supposed to do some calculations. You can give the method some other name and make it return the result, like I have shown in my second example.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.