CP2003 Tutorial - 3

  1. Abstraction is referred to as a ``powerful and useful tool''. Do you agree or disagree with this statement?
  2. Give an example of a parameterless abstraction (i.e. a procedure, or a function which has no parameters) that does not perform exactly the same computation every time it is called. Under what circumstances, in general, will a parameterless abstraction behave like this?
  3. Consider the following C function:
  4.   void multiply (int m, int n) {
        m = m*n;
        cout <<m<< "," <<n<< endl;
      }

    Suppose the function is called with actual parameters i,j where i=2,j=3. If we are using call-by-value show what is printed when called with:

      (a) multiply (i,j) and (b) multiply (i,i).

    Now suppose that the parameters could be replaced by call-by-value-result parameters. Repeat parts (a) and (b) and explain the different effect (if any). Assume that the instructions are not executed sequentially.

  5. State the visibility, lifetime, initialized value and purpose of local, global and static variables in C/C++ program.
  6. In what unusual place can you use a function call when a function returns a values by reference? (hint: function call is usually used at the right hand side of equal sign =). Give a simple example.
  7.  

  8. (a) What is the principle reason for passing arguments by reference?
  9. (b) Suppose a function has the following declaration: void person(char* name, int age); Modify the declaration so that default value for name is unknown and age 20.

     

  10.  Why modules are important in large programming environment?
  11.  

  12. What are eager, normal-order and lazy evaluation? Consider the following ML function:
fun sqr(n: int)= n*n;
and function call sqr(p+q)with p=2 & q=5

Show the binding and evaluation of the function in case of eager, normal-order and lazy evaluation.

  • Write a simple Java program that converts Fahrenheit temperature between 0 to 300 degree into equivalent Celsius. Here is how to write a Java program.

  • Joarder Kamruzzaman, James Cook University
    name="java-how.html" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="java-how.html" My first applet:

    How to A Java Program

     

    There are two kinds of Java programs:

       javac programName.java  ---generates a programName.class
       java  programName        --No need the class extension 

    Our First Java Program

    // HelloJava.java prints "Hello from Java".
    class HelloJava {
          public static void main(String args[]) {
             System.out.println("Hello from Java!");
          }
    }

    Put the program in file "HelloJava.java" in say classes directory Then Then compile it with:

    javac HelloJava.java

    This creates HelloJava.class file. This is the byte-code. Then run Java interpreter to execute it:

    java HelloJava
    this will output: Hello from Java!

    Our First Java Applet

    import java.awt.Graphics;
      public class HelloJavaApplet extends java.applet.Applet {
         public void paint(Graphics g) {
           g.drawString("Hello from a Java applet!", 150, 50);
         }
       }

    Store the program in file HelloJavaApplet.java.

    Compile it: javac HelloJavaApplet.java

    Notice it does not have a main() method, can't be executed alone. Needs to be embedded in a homepage and view it with Netscape 2.0 (or above), HotJava, Explorer 3.0, or use appletviewer. java.awt is a Java Abstract Window Toolkit package, Graphics is a class in it. java.applet is applet package, Applet is a class in it. So you need to write a HTML file like as follows:

    <html>
    <applet code="HelloJavaApplet.java" width=275 height=55>
    </applet>
    <html>

    Name this html file as HelloJava.html. Then run the following commnad

          appletviewer  HelloJava.html 

    or run HelloJava.html file on netscape (give you home Web page path to the location field of netscape)

    will display Hello from a Java applet! on screen.

    Java Tutorial site

    Java Classes reference site