CP2003 Tutorial - 3

  1. Abstraction is referred to as a ``powerful and useful tool''. Do you agree or disagree with this statement?

  2. Solution:

    Yes, agree with the statement. Use of abstraction allows us to
    concentrate on general ideas rather than on specific 
    manifestations of these ideas. In programming, abstraction
    refers to what a piece of code does rather that how it is
    implemented. For example, in C/C++ consider the difference 
    between a .h file (prototype declaration-what the program 
    does)and a .c file (how the program does it, i.e., its 
    implementation).
  3. 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?

  4. Solution:

      int a;  // global variable a
    
      void test() {
        cout << 2*a;
      }
      ...
      int main() {
        a = 2;
        test();
        a = 3;
        test();
        ...
      }
    In general, a parameterless abstraction may not perform exactly 
    the same computation every time it is called if it relies on the 
    exterior environment for at least one of the values that it uses.
  5. Consider the following C function:
  6.   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.

    Solution:

    Assuming the instructions are not executed sequentially:
    
    Using call-by-value:
      (a) "6,3"
      (b) "4,2"
    
    Using call-by-value-result:
      (a) "6,3"
      (b) "4,4"
    
    The different effect is that when calling using call-by-value-
    result, an alias is set up for the actual parameter, referred to 
    via the formal parameter. So, in part b using call-by-value-
    result, two aliases are set up for the variable i (one referred 
    to by m and one referred to by n), so when m is changed in the 
    line "m=m*n" this effectively changes the value of i (because of 
    the alias), which (because of the other alias) effectively 
    changes n.
  7. State the visibility, lifetime, initialized value and purpose of local, global and static variables in C/C++ program.
  8. Storage Type        Local           Static        Global
    -------------------------------------------------------------
    Visibility         function         function       file
    Lifetime           function         program        program
    Initialized Value  not initialized    0             0
    Purpose            variable used   same as local  variable used
                       by a single     local, but     by several
                       function        retains value  functions
  9. 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.
  10. Answer:
    On the left hand side of the equals sign. Following is a simple
    example.
    
            #include<iostream.h
            int x;                  // global variable
            int& setx();            //function declaration
    
            
            void main()
               {                    // set x to a value, using
                setx()=92;          // using function call on the left side
                cout <<"\nx=" <<x; //display new value in x
                }
    
            int& setx()
                {
                 return x;          // returns the value to be modified
                }

     

     

    In this program the function setx() is declared with a reference type, int&, as the return type: int& setx(). The function returns the values x by : return x. Now though it looks strange, you can put a call to this function on the left side of the equals sign: setx()=92. The result is that the variable returned by the function is assigned the value on the right side of the equals sign. The output of the program is x=92.
     
     

  11. (a) What is the principle reason for passing arguments by reference?
  12. (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.

            Answer:
              (a)To modify the original argument.
              (b)void person(char* name="unknown", int age=20);

     

  13.  Why modules are important in large programming environment?
  14. Answer: Modularity was introduced to support construction of
    large programs. A module is a large program units that can be 
    implemented as an independent entity. A well-designed module 
    typically has a single purpose and represents a narrow interface 
    to other modules. A module typically makes only a few components
    visible outside. Such components are said to be exported by the 
    module. Other modules remain hidden inside the module and used 
    for implementing the visible components. Modules are designed to 
    be resuable.

     

  15. 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.

Answer:

In case of eager evaluation: first 'p+q' is evaluated to 7 and formal parameter n is bound to 7. Finally 'n*n' is evaluated as 7x7 yielding 49.

In case of normal-order evaluation: formal parameter n is bound to 'p+q' itself. Each time the value of n is required during the evaluation of 'n*n', the espression 'p+q' is reevaluated. Thus the computation is (2+5)x(2+5)=49.

In case of lazy evaluation: formal parameter n is bound to 'p+q' itself. During the evaluation of 'n*n', the espression 'p+q' is evaluated to 7 once and copied that value for the calculation second 'p+q'.

  1. 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.
Answer:


class FahrToCel {
          public static void main(String arg[]) {
                int fahr, cel;
                int lower, upper, step;
                lower=0;
                upper=300;
                step=20;

                fahr=lower;
                while (fahr <=upper) {
            cel=5*(fahr-32/9);
            System.out.print(fahr);
            System.out.print("   ");
            System.out.println(cel);
            fahr=fahr+step;
                }
          }
}


Joarder Kamruzzaman, James Cook University
--------------AAB9D94A704E54259783E524 Content-Type: text/html; charset=us-ascii; 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

 


--------------AAB9D94A704E54259783E524--