Page 1 of 7 123 ... LastLast
Results 1 to 20 of 121
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Translating Pascal into java

    Pascal is one of the respected computer grandfathers, but died long ago.

    C is a kind of “dirty-Pascal”,
    C++ is the oo version of C,
    and java is a dialect of C++
    Concluding: java = dialect of a oo-“dirty.Pascal”

    I know, this statement might already trigger a 2000 pagina thread, but lets by practical!

    In pascal this makes since, or is the double declaration of "c" a twenty year old mistake (found in my twenty year old pascal program...the crime is "over-yeard" anyway)?


    Java Code:
    procedure calc(var a, b, c:integer);
    begin
    	c:= a + b;
    end;
    
    procedure docalc();
    var c: integer;
    
    begin
    	c:=4;
    	clac(2,3,c);
    {what would be the value of c here? I believe 5}
    
    end;
    The equivalent in java would be (looks silly, I know)

    public void calc (int a, int b, int c) {
    c=a+b;
    }

    public void docalc () {
    int c=4;
    calc(2,3,c);
    System.out.println("the value of c is: " + c);

    }

    The call docalc prints 4 and not 5
    What deeper issues are involved compairing the two versions?

  2. #2
    bukake is offline Member
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    Hahaha goodone.
    Good old times on the college.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    bukake,

    it is not a joke, when you do remember some of the old days please do respond.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Java passes values, not references. You will NOT be able to change the value of c outside of calc inside calc.
    calc would have to return the value for it to be known outside of calc.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    That is true for java, I know, but what about pascal? It seams that "c" goes in and ALSO OUT OF the procedure, right?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Sorry, don't remember enough about Pascal to answer that.
    Your example implies that Pascal uses call by reference.

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    I am looking into an old pascal manual. There are indeed two kind of parameter declarations of parameters concerning procedures. One begins with "var" the other omits "var". Maybe I should stop this thread? Nobody is interested in pascal now a days (me included). I´ll find a way out of here.

  8. #8
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    I think Pascal did also call by value, but I not sure (loooonnnggg time ago). But if you will make a function outa that procedure and return that value you calculated you will get the right value I quess

    Java Code:
    function calc(var a, b):Integer;
    begin
    	c:= a + b;
            return c;
    end;
    
    procedure docalc();
    var c: integer;
    
    begin
    	c:=4;
    	c= calc(2,3);
    {what would be the value of c here? I believe 5}
    
    end;
    It's not tested but in such a way it might work.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    Okay that works I know, but lets be a little more specific. When declaring a parameter as var, the parameter only accepts a reference. When var is omitted, one can feed numbers and or references. Your example is a function so the return statement works fine for one single result. But what happens in the next case (which is the case of my program). The previous example was not okay, this one is better:

    Java Code:
    procedure calc(a, b:integer; var c1, c2, c3:integer);
    begin
    	c1:= a + b;
    c2:= a - b;
    c3:= a * b;
    end;
    procedure docalc;
    var c1, c2, c3: integer;
    c1:=0; c2:=0; c3:=0;
    begin

    calc(2,3,c1,c2,c3);
    write (c1,c2,c3);
    {what values have c1-3 here}
    end;

    do not have pascal compiler

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    To return > 1 value from a method, put the values into an object

  11. #11
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    This with the Object is not possible. I think there are no objects in pascal, but I don't know exactly. I worked, several years ago with Turbo Pascal 7.0 and I think it's the same what you do, and there where not Objects. But there were records with is nearly the same only that a record does not contain any methods. But it will work for you. Or use an array as return value. Or what comes in my mind just as I write try this.

    Java Code:
    var c1, c2, c3:integer;
    procedure calc(a, b:integer);
    begin
    c1:= a + b;
    c2:= a - b;
    c3:= a * b;
    end;
    
    procedure docalc;
    c1:=0; c2:=0; c3:=0;
    begin
    calc(2,3);
    write (c1,c2,c3);
    End;
    I think that will work.

    A little bit smaller example of my suggestion

    Java Code:
    procedure assignValue(a:integer);
    begin
    v1 = a;
    end;
    
    MAIN
    Begin
    v1:Integer; LOOK FORWARD //There was someting that the compiler says that he should look forward so that you could use the var v1 in a procedure or anything else before it was declared! Do you no what I mean????
    v1 = 0;
    assignValue(5);
    write(v1); //v1 should have the value 5
    end;
    If you declare the variables not in the procedure signatur rather than above you will be able to assign a value, I guess. But you will have to set this LOOK FORWARD or LOOK BACKWARD thing after the var. I don't know the exactly spelling of that, but there was something like that.
    Last edited by hannehomuth; 07-30-2008 at 12:00 AM. Reason: Forgot something

  12. #12
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    Only a little post to be able to submit links. (Who decided that silly postcount of 20 post to submit links) Damm it.

  13. #13
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    So now i have postcount of twenty.
    It was not FORWARD to the vars it was forwarding to the procedures.

    You can see it here
    http://www.devq.net/pascal/src/asciitbl.pas

    This might work.

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    I am trying to understand an old pascal code in order to change it into java.
    What really puzzled me was this code fragment:

    Java Code:
    procedure syn2(k:kar_array; kan,w:integer; dt:data_array; op:integer;
                    var p1,p2,h_b,l_b:integer);
        begin
          p2:=1;                              
          if k='ar ' then
          begin 
            p1:=4+op;
            p2:=2;
            h_b:=((dt[kan,p1,1]*64)+w)div 16;
            l_b:=((dt[kan,p1,1]*64)+w)mod 16;
          end;  
          if k='d1r' then 
          begin
            p1:=5+op;
            p2:=3;
            h_b:=((dt[kan,p1,1]*128)+(dt[kan,p1,2]*31)+w)div 16;
            l_b:=((dt[kan,p1,1]*128)+(dt[kan,p1,2]*31)+w)mod 16;
          end;
    etc. (it is a quite long procedure)
    the var p1,p2,h_b,l_b:integer get their values inside the block! To declare them as parameters makes no since in java. So I toke them out and declared them as field var. (global in pascal) Like this:

    Java Code:
    public void syn1(String k, int kan, int w, int[][][] dt)  { //data voor lfo alg etc, 
                                                    // niet voor de operators 
                                                          
          p2=1;
          if (k.equalsIgnoreCase("alg")) {
            p1=12;
            p2=2;
            h_b=((8*dt[kan][12][1])+w)/16;
            l_b=((8*dt[kan][12][1])+w)% 16;
          } 
          else
          if (k.equalsIgnoreCase("fdb")) {
            p1=12;
            p2=1;
            h_b=(dt[kan][12][2]+(8*w))/ 16;
            l_b=(dt[kan][12][2]+(8*w))% 16;
          }
    I might have to do that a lot more for conceptual reasons

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    oh thanks for the link (did know about that poatcount thing)

  16. #16
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    Your right the vars p1,p2,h_b,l_b:integer will get a value inside the block but will they accessed outa that procedure? And what you did (declare them as fields) is that what i would do too.

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    Some latter in the pascal program that mentioned procedure syn interacts with the following:

    Java Code:
    procedure midi_data(op:integer; k:kar_array; w,k1,k2:integer; 
                          var dt:data_array);
                  {k=lfo/oper. etc naam (3 kar), w=parameterwaarde}
                  {op=algemeen of operator3 of operator4, k1/ k2 kanaal tot kanaal}
                  {alg=algemeen (0) operator3 (24) operator4 (16)}
                  {dt=data overdracht voor waardengeheugen} 
       var p1, p2, hb ,lb, i: integer;   
        begin
          if op=0 then
          begin 
            syn1(k,k1,w,dt,p1,p2,hb,lb);
            for i:=k1 to k2 do
            begin
              midi_aansturing1(1,i,p1,hb,lb); {algemeen}       
              dt[i,p1,p2]:=w;
            end;
          end;
          if (op>=16) and (op<=24) then
          begin 
            syn2(k,k1,w,dt,op,p1,p2,hb,lb);
            for i:=k1 to k2 do
            begin
              midi_aansturing1(1,i,p1,hb,lb); {operator 3 of 4}      
              dt[i,p1,p2]:=w;
            end;
          end;
       end;
    The question is what happens with the two var. called hb, lb, called in the h_b and l_b (stands for high byte/ low byte). You get my problem?

  18. #18
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default

    (sorry my brain goes faster as my typing)

    The question is what happens with the two var. called hb, lb, called in the syn procedure h_b and l_b (stands for high byte/ low byte). You get my problem?

  19. #19
    hannehomuth's Avatar
    hannehomuth is offline Member
    Join Date
    Jul 2008
    Location
    Sommerfeld (Brandenburg, Germany)
    Posts
    36
    Rep Power
    0

    Default

    Yeah I think I've got it. But no matter of that I think we got the solution of your problem if we decalare the vars as fields in your java class or not?

    Sorry but my english is not so good :-)

  20. #20
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post found a pascal translator

    see: Micro-Processor Services, Inc.

    they will know about control loop and system issues, the stuff that Norm and I will be worried about.

    Norm:
    Java Code:
    class PassTheCode{
    int[] testValue = new int[1];
    testValue[0] = 27;
    static String method(int[] intArray)
    {
        intArray[0]=34;
        String returnValue = new String(Integer.toString(intArray[0]));
        return returnValue;
    }
    publc static void main(String[] args)
    {
         int[] mainArray = new int[1];
         intArray[0] = Integer.parseInt(args[0]);
         System.out.println(method(mainArray));
    Opie's gonna waste time on this, primitives pass a register load and store, [] (arrarys) in Java are a first class object, thus are passed by indirect addressing. Dis-entangling copy of reference from actual data location is obviously not beyond the skills of original poster. Also, looks like we picked up a pascal coder. For an overview of how pascal works from a computer scientists perspective I suggest ( for Norm ) a reading of Compiler Construction - Principles and Practice - Kenneth C. Louden PWS Publishing. The sample code these two workers are talking in is almost straight out of that work and the cited work is mostly computer science that you are already familiar with.
    Last edited by Nicholas Jordan; 07-31-2008 at 12:35 AM. Reason: make return type match println() in main
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Page 1 of 7 123 ... LastLast

Similar Threads

  1. PASCAL & JAVA? for senior members
    By willemjav in forum Forum Lobby
    Replies: 2
    Last Post: 07-13-2008, 12:11 PM
  2. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •