作为一名相对较新的Java程序员,我期望使用double型浮点数组。假设我有四个double f数组声明为:
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];现在,我想在将它们传入和输出方法时填充它们。我的理解是,我需要定义一个容纳这些数组的类:
public class kinematics {
private double[] x;
private double[] v;
private double[] a;
private double[] time;
}我方法的调用如下所示:
VERLET(i, x, v, a, time, const0, gamma, A_o, dt);下面是我的方法的开始:
public static double VERLET(int i, double[] x, double[] v, double[] a, double[] time, double const0, double gamma, double A_o, double dt) {最后,我的回复声明是:
return kinematics(x[i], v[i], a[i], time[i]);其中,汇编者抱怨:
driven.java:193: error: cannot find symbol
return kinematics(x[i], v[i], a[i], time[i]);
^
symbol: method kinematics(double,double,double,double)
location: class driven我解释这一点的方式是,我的方法不知道我的类的存在。但我已经宣布它为public,所以我不太清楚这意味着什么。,有人能看出我做错了什么吗?
除此之外,我似乎一遍又一遍地声明double。我肯定这是因为缺乏经验。对于使用类型double f数组,还有比我之前做的更优雅的方法吗?,谢谢。
发布于 2014-11-25 02:04:58
带有Fortran注释的代码的工作版本:
import java.lang.Math.*;
import java.io.*;
import java.text.*;
//MODULE shared
//USE, INTRINSIC :: ISO_FORTRAN_ENV,dp=>REAL64!modern DOUBLE PRECISION
//INTEGER :: i
//INTEGER, PARAMETER :: i_max=5000
//REAL(dp) :: x_read,v_read,const0,gamma,A_o,dt
//REAL(dp), DIMENSION(:), ALLOCATABLE :: x,v,a,E,dE,time
//REAL(dp), PARAMETER :: m=1.0
//END MODULE shared
//PROGRAM nonlinear
public class driven {
public static void main(String args[]) {
//USE shared
//IMPLICIT NONE
//EXTERNAL VERLET
//ALLOCATE(x(i_max + 1))
//ALLOCATE(v(i_max + 1))
//ALLOCATE(a(i_max + 1))
//ALLOCATE(E(i_max + 1))
//ALLOCATE(dE(i_max + 1))
//ALLOCATE(time(i_max + 1))
int i;
final int i_max=5000;
double x_read,v_read,const0,const0_read,gamma,gamma_read,A_o,A_o_read,dt,dt_read;
final double m=1.0;
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];
double[] f_osc = new double[i_max+1];
//PRINT *, ' '
//PRINT *, 'Initial position of the mass? [m]'
//PRINT *, ' '
//READ *, x_read
System.out.print("Initial position of the mass? [m] ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x_read = 0.0;
try {
x_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
//x(1) = x_read
x[0] = x_read;
//PRINT *, ' '
//PRINT *, 'Initial velocity of the mass? [m/sec]'
//PRINT *, ' '
//READ *, v_read
System.out.print("Initial velocity of the mass? [m/sec] ");
v_read = 0.0;
try {
v_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
//v(1) = v_read
v[0] = v_read;
//PRINT *, ' '
//PRINT *, 'Value of k/m? [1/sec^2]'
//PRINT *, ' '
//READ *, const0
System.out.print("Value of k/m? [1/sec^2] ");
const0_read = 0.0;
try {
const0_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
const0 = const0_read;
//PRINT *, ' '
//PRINT *, 'Value of the damping coefficient? [kg/sec]'
//PRINT *, ' '
//READ *, gamma
System.out.print("Value of the damping coefficient? [kg/sec] ");
gamma_read = 0.0;
try {
gamma_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
gamma = gamma_read;
//PRINT *, ' '
//PRINT *, 'Amplitude of the external force? [N]'
//PRINT *, ' '
//READ *, A_o
System.out.print("Amplitude of the external force? [N] ");
A_o_read = 0.0;
try {
A_o_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
A_o = A_o_read;
//PRINT *, ' '
//PRINT *, 'Time-step of the system? [sec]'
//PRINT *, ' '
//READ *, dt
//PRINT *, ' '
System.out.print("Time-step of the system? [sec] ");
dt_read = 0.0;
try {
dt_read = Double.parseDouble(br.readLine());
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(1);
}
dt = dt_read;
//time(1) = 0.0
time[0] = 0.0;
//i = 1
i = 0;
//DO
do {
//IF(i > i_max) EXIT
//CALL VERLET
driven damped = new driven();
damped.VERLET(i, x, v, a, time, f_osc, const0, gamma, A_o, dt);
//i = i + 1
i = i + 1;
//END DO
} while (i < i_max);
//OPEN(7, file='xt.dat', status='unknown')
//WRITE(7,'(2f16.6)') (time(i),x(i),i=1,i_max)
//CLOSE(7)
//DEALLOCATE(x)
try {
PrintWriter writer = new PrintWriter("xt.txt", "UTF-8");
for (i = 0; i < i_max; i = i + 1) {
writer.printf("%1$16.6f%2$16.6f%n", time[i], x[i]);
}
writer.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
//DEALLOCATE(v)
//DEALLOCATE(a)
//DEALLOCATE(E)
//DEALLOCATE(dE)
//DEALLOCATE(time)
}
//SUBROUTINE VERLET()
double VERLET(int i, double[] x, double[] v, double[] a, double[] time, double[] f_osc, double const0, double gamma, double A_o, double dt) {
//USE shared
//IMPLICIT NONE
//REAL(dp) :: x_temp,v_temp,t_temp,f_osc
//EXTERNAL FORCE,ENERGY
double x_temp,v_temp,t_temp;
final double m=1.0;
//x_temp = x(i)
x_temp = x[i];
//v_temp = v(i)
v_temp = v[i];
//t_temp = time(i)
t_temp = time[i];
//CALL FORCE(x_temp,v_temp,t_temp,f_osc)
FORCE(i, x_temp, v_temp, t_temp, const0, gamma, A_o, f_osc);
//x_temp = x(i)
//v_temp = v(i)
//CALL ENERGY!don't think I'm actually using this; no INTENT(OUT)
//a(i) = f_osc/m
a[i] = f_osc[i] / m;
//x(i + 1) = x(i) + v(i)*dt + 0.5*a(i)*dt*dt
x[i + 1] = x[i] + v[i] * dt + 0.5 * a[i] * dt * dt;
//x_temp = x(i + 1)
x_temp = x[i + 1];
//v_temp = v(i)
v_temp = v[i];
//time(i + 1) = time(i) + dt
time[i + 1] = time[i] + dt;
//t_temp = time(i + 1)
t_temp = time[i + 1];
//CALL FORCE(x_temp,v_temp,t_temp,f_osc)
FORCE(i, x_temp, v_temp, t_temp, const0, gamma, A_o, f_osc);
//a(i + 1) = f_osc/m
a[i + 1] = f_osc[i] / m;
//v(i + 1) = v(i) + 0.5*(a(i + 1) + a(i))*dt
v[i + 1] = v[i] + 0.5 * (a[i + 1] + a[i]) * dt;
//x_temp = x(i + 1)
//v_temp = v(i + 1)
//CALL ENERGY!don't think I'm actually using this; no INTENT(OUT)
//return x[i], v[i], a[i], time[i];
return x[i];
//END
}
//SUBROUTINE FORCE(xs,vs,ts,f_oscs)
double FORCE(int i, double x_temp, double v_temp, double t_temp, double const0, double gamma, double A_o, double[] f_osc) {
//USE shared
//IMPLICIT NONE
//REAL(dp), INTENT(IN) :: xs,vs,ts
//REAL(dp), INTENT(OUT) :: f_oscs
//REAL(dp) :: f_t
double f_t;
//f_t = A_o*DCOS(2.0*ts)
f_t = A_o * Math.cos(2.0 * t_temp);
//f_oscs = -const0*xs - gamma*vs + f_t
f_osc[i] = -const0 * x_temp - gamma * v_temp + f_t;
return f_osc[i];
//END
}
//SUBROUTINE ENERGY()
//USE shared
//IMPLICIT NONE
//!REAL(dp), INTENT(OUT) ::
//E(i) = 0.5*(v(i)**2 + const0*x(i)**2)
//E(1) = 0.5*(v(1)**2 + const0*x(1)**2)
//dE(i) = E(i) - E(1)
//END
//END PROGRAM nonlinear
}帮助我实现这一目标的例子可以找到这里和这里。
发布于 2014-10-02 14:09:26
您需要返回一个新的运动学实例。即
return new kinematics(x[i], v[i], a[i], time[i]);目前,您正在尝试调用一种称为运动学的方法,而不是创建该类型的新对象。
要遵循Java风格的指导原则,您应该使用大写(即Kinematics)开始类名。
此外,您还需要为Kinematics类创建一个构造函数,该构造函数接受四个参数并分配它们。如果你要求的话,大多数IDE都会自动为你生成。
发布于 2014-10-02 14:13:26
在我看来,您的Kinematics对象将保存每个参数的单个双值,而不是数组。(从您实际试图从VERLET函数返回的内容中推断出这一点)
下面是一个模板,让您开始
public class Kinematics {
private double x;
private double v;
private double a;
private double time;
public Kinematics(double x, double v, double a, double time) {
this.x = x;
this.v = v;
this.a = a;
this.time = time;
}
public static Kinematics VERLET(int i, double[] x, double[] v, double[] a,double[] time, double const0, double gamma, double A_o, double dt) {
//do whatever functionality it has to be done
//assuming i is defined and is the ith element of the arrays
return new Kinematics(x[i], v[i], a[i], time[i]);
}
}这将是一个简单的main入口点
public static void main(String[] args) {
//Assuming i_max is already defined
double[] x = new double[i_max+1];
double[] v = new double[i_max+1];
double[] a = new double[i_max+1];
double[] time = new double[i_max+1];
//Assuming i, const0, gamma, A_o and dt are already defined
Kinematics kinOne = Kinematics.VERLET(i, x, v, a, time, const0, gamma, A_o, dt);
}https://stackoverflow.com/questions/26162650
复制相似问题