• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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
0
shpark82
Asked:
shpark82
  • 5
  • 3
1 Solution
 
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
0
 
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 ) ;
  }
}
0
 
shpark82Author Commented:
what if all i wanted to make changes to was the calculator class and not the data class?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 ;
  }
}
0
 
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?
0
 
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...
0
 
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.
0
 
shpark82Author Commented:
well in my calculator class, i have

run() function that does the execution of the functions in my class..
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now