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.
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.
linux1[1]% cd ../pub/www/cs104 linux1[2]%
linux1[2]% mkdir labx linux1[3]% cd labx linux1[4]%
xemacs
to create the file compare.html
:
linux1[4]% xemacs compare.html
<!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>
"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.
compare.php
,
and then edit it with xemacs, as described below:
linux1[5]% cp compare.html compare.php linux1[6]% xemacs compare.php
<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>
"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.
compare.java
,
and then edit it with xemacs, as described below:
linux1[5]% cp compare.html compare.java linux1[6]% xemacs compare.java
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"); } } }
linux1[5]% javac compare.java linux1[6]% java compare
Guess what--you just wrote your first Java program, and ran your first compiler!
linux1[5]% cp compare.java compare.c linux1[6]% xemacs compare.c
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"); } }
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.
Be sure to logout completely when you have finished!