Reputation: 2299
Right now I'm working on a GUI that will calculate kinematic values based on what the user enters into a text field. I created a private inner class with values of type Double (not double), and then created a method to get a value based on values given. For example, this returns initial velocity:
public Double getInitialVelocity(Double vf, Double a, Double ti, Double tf) {
deltaT = deltaT(tf, ti);
initialVelocity = vf - (a * deltaT);
df.format(initialVelocity);
return initialVelocity;
}
The problem appears when I tried to test this method. I set up new doubles, and use getInitialVelocity
in my main class:
Kinematics test = new Kinematics(); // creates object from inner class
Double vf = 1.0, a = 2.0, ti = 0.5, tf = 1.5;
test.getInitialVelocity(vf, a, ti, tf);
When I run this to test, I get this error:
Static Error: No method in Kinematics with name 'getInitialVelocity' matches this invocation
Arguments: (Double, Double, Double, Double)
Candidate signatures: double getInitialVelocity()
Does anyone know how to properly do this? I need to use type Double because I am comparing values given to null and then using the appropriate formula based on which values are null. Also, when converting from a String, should I just use Double.parseDouble(textField.getText());
?
Edit 1: Here are the relevant parts of my class:
Private inner class (Kinematics):
private class Kinematics {
private Double initialVelocity, finalVelocity, acceleration, timeFinal, timeInitial;
private Double deltaT;
// constructor
public Kinematics() {
}
public Double deltaT(Double tf, Double ti) {
if(!(tf == null && ti == null)){
deltaT = tf - ti;
} return deltaT;
}
public Double getInitialVelocity(Double vf, Double a, Double ti, Double tf) {
deltaT = deltaT(tf, ti);
initialVelocity = vf - (a * deltaT);
df.format(initialVelocity);
return initialVelocity;
}
In my main class (KinematicsPanel), I have:
Kinematics values = new Kinematics();
viLabel = new JLabel("Initial Velocity: ");
viText = new JTextField(1);
vfLabel = new JLabel("Final Velocity: ");
vfText = new JTextField(1);
aLabel = new JLabel("Acceleration: ");
aText = new JTextField(1);
tiLabel = new JLabel("Initial Time: ");
tiText = new JTextField(1);
tfLabel = new JLabel("Final Time: ");
tfText = new JTextField(1);
// compute button & result
compute = new JButton("Compute");
compute.addActionListener(this);
result = new JTextField(2);
result.setEditable(false); // can not be edited
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
// parse each string to a value
Double vf = 0.0, a = 0.0, ti = 0.0, tf = 0.0;
if(vfText != null) {vf = Double.parseDouble(vfText.getText());}
if(aText != null) {a = Double.parseDouble(aText.getText());}
if(tiText != null) {ti = Double.parseDouble(tiText.getText());}
if(tfText != null) {tf = Double.parseDouble(tfText.getText());}
if(action.equals("Compute")) {
if(viText == null) { // get initial velocity
// get values
values.getInitialVelocity(vf, a, ti, tf);
System.out.println(values.toString()); // to test
result.setText(values.toString());
}
}
As of right now, this does nothing which is why I tested the method in the interactions pane in Dr.Java.
Edit2: The format function being used is in the main class:
DecimalFormat df = new DecimalFormat("#.00");
Upvotes: 0
Views: 102
Reputation: 2732
it looks , you are using jdk 1.4 or lower one, there autoboxing is not supported.
so it can not convert Double to double. but that should give you a compile time
error if you are using IDE like eclipse.
OR
may be the method you are calling has different signature.
try to check the jdk version and post whole class if above one does no solve your problem.
Upvotes: 0
Reputation: 435
Its all ok with your code, which compiler are you using? The method getInitialVelocity are in the class Kinematics?
Upvotes: 1