CMSC104 Fall 2009 Comparative Languages Lab Exercise

Objectives

-To compare and contrast several different programming languages

Note: There is nothing that you must turn in associated with this exercise. If you do not finish the exercise during the lab session, it is to your benefit to finish it later on your own.

The Assignment

You will start by entering a basic JavaScript program. You will then incrementally modify it into an equivalent program in other, related languages, following the templates and instructions below.

It is important to follow the stages below in order. If you get lost, ask the professor to help you get a fresh copy of the code for the next stage, so that you don't get completely lost and left behind.

Getting Started, & Creating a Directory to Use for this Exercise

  1. Log into your gl account.

  2. Change into the directory you have been using for all your class projects:

    linux1[1]% cd ../pub/www/cs104
    linux1[2]%
    
    

  3. Make a new directory for this lab, and change into it:

    linux1[2]% mkdir labx
    linux1[3]% cd labx
    linux1[4]%
    
    

  4. Use xemacs to create the file compare.html:
    linux1[4]% xemacs compare.html
    
    
    ... and enter the following code exactly as it is written:

    <!DOCTYPE html>
    <html>
      <head>
      </head>
      <body>
        <script type="text/javascript">
          <!--
        var a, b, i;
        var my_array = new Array(5);
        var f;
        var prefix = "My name is ";
        var my_name;
     
        a = 1;
        b = 2;
     
        for (i = 0; i < 5; i++) {
            my_array[i] = i;
        }
     
        my_name = prompt("Enter name: ");
     
        if (a < b) {
            document.write(prefix + my_name + "<br />");
        }
     
        if (a / b > 0) {
            document.write("a / b > 0");
        }
        else {
            document.write("a / b is not > 0");
        }
          //-->
        </script>
      </body>
    </html>
    
    


  5. Now, go to a new browser window (don't clobber the one you are reading this page on! :-)), and load the page you just created, using a URL like "http://userpages.umbc.edu/~myname/cs104/labx/compare.html". Make sure it loads fine, without any errors. You will be using this file as the base for your other programs.

  6. Now, copy the file you just worked on to the file compare.php, and then edit it with xemacs, as described below:

    linux1[5]% cp compare.html compare.php
    linux1[6]% xemacs compare.php
    
    
    ... and change the code, so it looks exactly like the following; the main changes are: By visually comparing the following to what's in your xemacs window, you should be able to do the changes easily.

    <DOCTYPE html>
    <html>
      <head>
      </head>
      <body>
        <?php
        $my_array = array();
        $prefix = "My name is ";
     
        $a = 1;
        $b = 2;
     
        for ($i = 0; $i < 5; $i++) {
            $my_array[$i] = $i;
        }
     
        // PHP is strictly server-side -- cannot get input directly from user!
        $my_name = "John Doe";
     
        if ($a < $b) {
            echo ($prefix . $my_name . "<br />");
        }
     
        if ($a / $b > 0) {
            echo ("a / b > 0");
        }
        else {
            echo ("a / b is not > 0");
        }
        ?>
      </body>
    </html>
    
    


  7. Now, go to the same browser window you used for your JavaScript test, and load the page you just created, using a URL like "http://userpages.umbc.edu/~myname/cs104/labx/compare.php" (IMPORTANT: note the file extension is now "php", not "html". Make sure it loads fine, without any errors.

  8. Now, copy the JavaScript file again, this time to the file compare.java, and then edit it with xemacs, as described below:

    linux1[5]% cp compare.html compare.java
    linux1[6]% xemacs compare.java
    
    
    ... and change the code, so it looks exactly like the following; the main changes this time are:

    import java.io.*;
     
    class compare {
    public static void main(String argv[]) {
        int a, b, i;
        int my_array[] = new int[5];
        float f;
        String prefix = "My name is ";
        String my_name;
     
        Console cin = System.console();
     
        a = 1;
        b = 2;
     
        for (i = 0; i < 5; i++) {
            my_array[i] = i;
        }
     
        my_name = cin.readLine("Enter name: ");
     
        if (a < b) {
            System.out.println(prefix + my_name);
        }
     
        if (a / b > 0) {
            System.out.println("a / b > 0");
        }
        else {
            System.out.println("a / b is not > 0");
        }
    }
    }
    

  9. Now, you have to compile and execute the code. To do this for Java programs, do:

    linux1[5]% javac compare.java
    linux1[6]% java compare
    
    

    Guess what--you just wrote your first Java program, and ran your first compiler!

  10. Now, let's write some C. This time, we are going to copy the Java program we just wrote (not the JavaScript program!), as follows: and then edit it with xemacs, as described below:

    linux1[5]% cp compare.java compare.c
    linux1[6]% xemacs compare.c
    
    
    ... and change the code, so it looks exactly like the following; the main changes should be pretty clear.

    main() {
        int a, b, i;
        int my_array[5];
        float f;
        char *prefix_str = "My name is ";
        char message[100];
        char my_name[100];
     
        a = 1;
        b = 2;
     
        for (i = 0; i < 5; i++) {
            my_array[i] = i;
        }
     
        puts("Enter name:");
        gets(my_name);
     
        if (a < b) {
            strcpy(message, prefix_str);
            strcat(message, my_name);
            puts(message);
        }
     
        if (a / b > 0) {
            puts("a / b > 0");
        }
        else {
            puts("a / b is not > 0");
        }
    }
    

  11. As with Java, you have to compile and execute the code. To do this for C programs, do:

    linux1[5]% cc compare.java -o compare
    linux1[6]% ./compare
    
    

    You are now a C expert, too! Pretty cool...

    In one lab slot, you just wrote a relatively sophisticated program in four different, important languages. Pat yourself on the back.

  12. If you do not complete the lab today and would like to finish later, you can continue to work on it from home. Ask the professor for copies of the intermediate files to check your work against.

Be sure to logout completely when you have finished!


Last Modified: Wednesday, 09-Dec-2009 15:49:03 EST
Last Modified: Wednesday, 09-Dec-2009 15:49:03 EST