| CIS 425 Home Page | Last updated 2005/10/09 11:57:29 |
Compile: % javac "sourceFile"
Run: % java "classFile"
Notes:
package uoregon.cs.cis425.prog1;
public class Foo extends Object implements Comparable
{
private Goo g1, g2;
public Foo(Goo g1, Goo g2){...}
...
}
Then the sourceFile would have to be called "Foo.java" and be located
in the directory "./uoregon/cs/cis425/prog1/".
Don't worry about packages, as you probably won't have to use them for
this course.
They are interesting and sometimes helpful for organizing your work, though.
% javac Foo.javafor the above class definition, the Java compiler would also compile Goo.java if no Goo.class file existed. You will likely have a single directory for each project that contains all the .java and .class files for that project. When you want to compile the project, just type:
% javac *.java
public class MainApp
{
public static void main(String[] argv)
{
...
}
}
The important part here is the "public static void main(String[] argv)".
This has to be exactly as above since Java expects that type signature
for the entry function for the class. When the program is invoked by:
% java MainApp arg1 arg2
the Java Virtual Machine (java) calls MainApp.main(["arg1", "arg2"]).
That is, the JVM looks for a static method called main and passes it the
command-line arguments as an array of Java Strings.
Compile C source: % cc -o "outputFile" "sourceFile" or % gcc -o "outputFile" "sourceFile"
Compile C++ source: % CC -o "outputFile" "sourceFile" or % g++ -o "outputFile" "sourceFile"
Run: "outputFile"
Notes:
% g++ -o fooRunnable Foo.C
will generate a file called "fooRunnable" that you can execute by typing:
% fooRunnable
Although you may have to type:
% ./fooRunnable
depending on how your PATH variable is set.
int main(int argc, char *argv[])
{
...
}
It is a convention that main returns 0 on successful termination or some
non-zero error code (-1, etc) on unsuccessful termination. You can also use
declare main to have a void return, but you may get a warning.
int array_sum(int *array, int n)
{
int sum = 0;
for(int i=0; i'<'n i++)
sum += array[i];
}
#ifndef _FOO_H_
#define _FOO_H_
class Foo
{
public:
Foo(int v);
int getVal() const;
void setVal(int val);
private:
int value;
};
#endif
(Actually a "header file" is just any C/C++ code to be included in the
program compilation and the "#incude" directive just means to compile
the contents of the named file as if it were part of the source file.
Everything starting with "#" is a preprocessor directive. The pre-processor
is used (and abused) for all types of things, the most common being the
inclusion of class/struct definitions or function declarations used
by the implementation.)
#include "Foo.h"
Foo::Foo(int v){ value = v; }
int Foo::getVal() const { return value; }
void Foo::setVal(int val){ value = val }
Notice the syntax of the methods: they are prefixed with "Foo::", the scope
resolution operator, to indicate that they are methods of the class Foo.
The compiler will check that these method signatures are indeed in the
class definition. But otherwise, the code in the method body is treated just
as if it were within the class definition (as in Java) and so can refer to
any variables or methods in the class Foo.
Chez Scheme is installed on the department Solaris network under /local/apps/Lang/chez. The following instructions describe how to use the Chez Scheme command line interpreter in the department Solaris environment, but also apply to the command line interpreter under Windows. The Scheme code would be (nearly) the same in the GUI environment of DrScheme.
Start Scheme interpreter: /local/apps/Lang/chez/bin/scheme
Open Scheme source file for interpretation: First start the scheme interpreter, then type: (load "foo.scm") at the '>' prompt.
(This assumes your source file is called foo.scm.)
To exit the interpreter, type in: (exit).
Notes:
;; this is a scheme comment
;; function f adds two integers
(define f (lambda (x y) (+ x y)))
;; function g takes a list y and an integer x and
;; adds the integer to each element in the list
(define g (lambda (x y)
(if (null? y)
y
(cons (f x (car y)) (g x (cdr y)))
)
)
)
Start the scheme interpreter:
% /local/apps/Lang/chez/bin/scheme
You will get the prompt:
>
Now type:
> (load "foo.scm")
> (g 2 '(1 2 3))
The interpreter should print:
(3 4 5)
and then give you back the prompt.
The following instructions describe how to use the SML interpreter in the department Unix environment, but also apply to the interpreter under Windows.
Start ML interpreter: sml
Open ML source file for interpretation: First start
the ML interpreter, then type: use "foo.sml"; at the '-' prompt,
(assuming your source file is called foo.sml). To exit the interpreter,
enter Control-D from the keyboard (end of input).
Notes:
(* This is an sml comment line *)
(* this defines a datatype for arithmetic expressions *)
datatype exp = plus of exp * exp | minus of exp * exp |
times of exp * exp | divide of exp * exp |
value of int
(* this defines a divide by zero error *)
exception DivideByZero
(* this function interprets arithmetic expressions *)
fun run (value(x)) = x
| run (plus(x,y)) = run(x) + run(y)
| run (minus(x,y)) = run(x) - run(y)
| run (times(x,y)) = run(x) * run(y)
| run (divide(x,y)) =
let val v = run(x)
val w = run(y)
in
if (w = 0)
then raise DivideByZero
else v div w
end
Start the ML interpreter:
% sml
At the '-' prompt, type:
- use "foo.sml";
val run = fun : exp -> int
val it = () : unit
-
and notice the interpreter responds with the types of the values
you have defined. To execute your function enter:
- run(plus(value(1),value(2)));
val it = 3 : int
-