1.11M Members

Help Converting Pascal To Java

 
0
 

I need help converting this Pascal code into Java. My Pascal Programming abilities are limited so any help would be much appreciated.

[
uses wincrt;
const n=300;      {1320 this is the max length possible}
      m=2;
type table = array [1..n,1..m] of real;
var data,data2  : table;
    i,j,card : integer;
    t0,X0,Y0,Xn,h,g1,g2,r1,r2,d,slope:real;
    ch:char;

procedure init (var dat,dat2:table);
{ this procedure initializes the matrix  of data }
var i,j:integer;
begin
     writeln('THE DIFFERENTIAL EQUATIONS METHODS / THE SHOOTING METHOD');
     writeln('--------------------------------------------------------');
     writeln;

     for i:=1 to n do
         for j:=1 to m do begin
             dat[i,j]:=0;
             dat2[i,j]:=0;
         end;
end;

procedure reading;
begin
     clrscr;
     init(data,data2);
     writeln('<> PLS INPUT THE BOUNDARY VALUES :                       ');
     write(' t0 : ');     {t0=0}
     readln(data[1,1]);
     t0:=data[1,1];
     data2[1,1]:=data[1,1];
     write(' X0 : ');
     readln(data[1,2]);   {X0=1}
     x0:=data[1,2];
     write(' D (THE DESIRED VALUE AT ENDPOINT) which is X(tn) : ');
     readln(d);
     {P0=0}
     data2[1,2]:=-1.5;
     g1:=-1.5;
     write('<> PLS INPUT THE FINAL VALUE tn : '); {Xn=1,2,3,4,5...}
     readln(Xn);
     write('<> PLS INPUT THE LENGTH OF THE INTERVAL h : ');
     readln(h);      {h=0.1, 0.05, 0.01, 0.005, 0.001...h=0.01 is the best}
     card:=trunc((Xn-data[1,1])/h);
     writeln(card);
     ch := readkey;

     if (card<0) then
     begin
          writeln;
          writeln('INVALID INPUT...');
          ch:=readkey;
          halt(1);
     end;
end;

procedure writing (dat,dat2:table;cd:integer);
{ this procedure outputs the table of data }
var i,j:integer;
begin
      writeln;
      for i:=1 to cd+1 do begin
          for j:=1 to 2 do
              write (dat[i,j]:25);
          write(dat2[i,2]:30);
      end;
end;

procedure RK4(var dat,dat2:table;cd:integer;h:real);
{this procedure applies Runge-Kutta's method of order 4 to the following ODE
 dýx/dtý - (1-t/5)x = t      x(1)=2, x(3)=-1  }

var x,t,k1,k2,k3,k4,l1,l2,l3,l4,p:real;
begin
    clrscr;
    writeln('THE RUNGE-KUTTA OF ORDER 4 (RK4) METHOD GIVES :                 ');
    writeln('                   TIME                   THE DISTANCE                 VELOCITY  ');
    for i:=2 to cd+1 do
    begin
         dat[i,1]:= dat[1,1]+(i-1)*h;
         dat2[i,1]:=dat[i,1];
         t:=dat[i-1,1]; { t here is the independent variable }
         x:=dat[i-1,2]; p:=dat2[i-1,2];
         k1:= h * p;    l1:=h*((1-(t/5))*x+t);
         k2:= h * (p+l1/2);   l2:=h*((1-((t+h/2)/5))*(x+k1/2)+(t+h/2));
         k3:= h * (p+l2/2);   l3:=h*((1-((t+h/2)/5))*(x+k2/2)+(t+h/2));
         k4:= h * (p+l3);     l4:=h*((1-((t+h)/5))*(x+k3)+(t+h));
         dat[i,2]:= dat[i-1,2] + (1/6)*(k1+(2*k2)+(2*k3)+k4);
         dat2[i,2]:= dat2[i-1,2] + (1/6)*(l1+(2*l2)+(2*l3)+l4);
    end;
    writing(data,data2,card);
end;

procedure message;
begin
     write('PRESS ANY KEY...');
     ch:=readkey;
end;

procedure message2;
begin
     write('PRESS ANY KEY TO EXIT...');
     ch:=readkey;
end;

begin
     { find R1 }
     reading;
     RK4(data,data2,card,h);
     r1:=data[card+1,2];
     writeln;
     writeln('               R1 = ',r1);
     message;

     { find R2 }
     init(data,data2);
     data[1,1]:=t0;
     data2[1,1]:=data[1,1];
     data[1,2]:=X0;
     data2[1,2]:=-3.0;
     g2:=-3.0;
     RK4(data,data2,card,h);
     r2:=data[card+1,2];
     writeln;
     writeln('              R2 = ',r2);

     { extrapolate the value of X' }
     slope:= g1 + ((g2-g1)*(d-r1)/(r2-r1));
     writeln;
     writeln('        THE SLOPE IS : ',slope);
     writeln('                      ');
     message;

     { print the final and exact table of X }
     init(data,data2);
     data[1,1]:=t0;
     data2[1,1]:=data[1,1];
     write(' X0 : ');
     data[1,2]:=x0;   {X0=1}
     data2[1,2]:=slope;
     RK4(data,data2,card,h);
     writeln;
     writeln('          THE SLOPE IS : ',slope,'     HENCE X''(',t0:2:1,') = ',slope);
     writeln('                          ');
     message2;
end.]
You
Post:
Start New Discussion
View similar articles that have also been tagged: