Lab exercise #2:
For this and for all subsequent exercises, I assume you know how to compile, include an applet in an html page, read and create java
documentation and open an html file to view its source.
If you are confused or lost, ask me now.
- This exercise shows you how to pass parameters to an applet via the html code.
- Open the .html file in emacs and notice the parameters appearing after the applet tag. Then open the java source file
and read the code. Try to understand how these parameters are processed by the program.
- Do the exercises. Compile, run and make sure everything works as expected. If not, ask me.
- When done, everybody stops and we will have a discussion on what we learned from the first two examples we did so far:
- The use of class: everything in Java is inside some class. This is the object oriented programming style.
The classes we've seen so far extend an existing class from a library. Later in the semester, we will learn how to define
and use brand new classes, created by you and not imported from some library.
- The use of class variables: f and name.
- The declaration of variables, as being of a certain type, such as String and Font.
- The use of functions or methods in a class: init and paint
- The use of parameters in functions (methods): g in paint
- Calling a function (method): getparameter, setFont, setColor, drawString. All these methods are already implemented in Java.
We call them from a library. Later in the semester we will define our own classes and methods.
- Applying a method on a variable: the dot notation.
Notice two types of function calls:
- getparameter is not applied on any variable. It is in fact applied on the current object, the applet which
we are just defining.
- setFont, etc. are applied on the variable g, the Graphics parameter of function paint. This is how you will
in general encounter function calls in Java.