
     CSc 398  Final Examination   Thursday 18 May 2000
     >>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<
     1.  (10 pts)  Assume that thread priority behaves the way the book
     says they do, and answer the following question TWICE, the first
     time assuming the underlying machine is a PC and the second time
     assuming the underlying machine is a Solaris workstation: What is
     the output of the following code?
     class One extends Thread{
       static String str;
       One(int n){
          super("Thread #"+n);
       }
      public synchronized void inc(String s){
           str+=s;
      }
      public void run(){
       inc(getName()+" starts\n");
       for(int j=0;j<4000000;j++);
       inc(getName()+" ends\n");
      }
      public static void main(String s[]){
        str="";
        One a=new One(1), b=new One(2), c=new One(3);
        c.setPriority(7);
        a.start();
        b.start();
        c.start();
        while(str.length()<90);
        System.out.println("Results:\n"+str);
      }
     }
    PC output: Results:         Work Station output:  Results:
               Thread #1 starts                       Thread #1 starts
               Thread #2 starts                       Thread #1 ends
               Thread #3 starts                       Thread #2 starts
               Thread #3 ends                         Thread #2 ends
               Thread #1 ends                         Thread #3 starts
               Thread #2 ends                         Thread #3 ends

     2.  (10 pts)  Explain the differences among the four levels of
     protection (public, private, etc.) of data members and member functions
     in Java.
       Members are public (qualified by the keyword "public"), packaage
       accessible (no qualifying keyword), protected (qualified by the
       keyword "protected"), or private (qualified by the keyword "private").
       Public members are accessible to any user of the class.  Protected
       members are accessible only to members of the class and subclasses
       of the class.  Package accessible members are accessible to any
       members of classes in the same package (subdirectory).  Private
       members are accessible only to members of the class.  Public members
       of a class are public members of a subclass, and protected members
       of a class are protected members of a subclass.

     3.  (20 pts)  Write a program which stores the inteers 2000, 3400, and
     4000 as ints in a Random Access File and then reads them from the
     Random Access File as bytes and displays the bytes on the screen (DOS
     window).
     import java.io.*;

     public class ShortInt{
       public static void main(String s[]){
         try{ RandomAccessFile f=new RandomAccessFile("TEMP.dat","rw");
           f.writeInt(2000);
           f.writeInt(3500);
           f.writeInt(4000);
           f.seek(0);
           do{
              byte b=f.readByte();
              System.out.println(b);
           }while(true);
         }catch(IOException eof){}
       }
     }


     4.  (25 pts)  Write the code for an interface "Delta", a class "Time"
     and a class "Date", where the three have the following properties and
     relationship. Delta has two functions inc() and reset().

     Date saves a number of days (say since January 1).  Calling reset()
     initializes the number.  Calling inc() increases the number by 1.  Date
     has some method which returns the number.

     Time is a subclass of Date and stores the number of seconds (say since
     midnight).  Calling reset() initializes the number of days and the
     number of seconds. Calling inc() increases the number of seconds by 1,
     except that if it exceeds 86400 (the number of seconds in a day), the
     number of seconds is set to 0 and the number of days is increased by 1.
     Time has some method which returns the number of seconds.

     Finally, and quite important, Time and Date implement Delta.
     interface Delta{
        public abstract void inc();
        public abstract void reset();
     }

     class Date implements Delta{
        protected int days;
        Date(){
          reset();
        }
        public void inc(){
          days++;
        }
        public void reset(){
          days=0;
        }
        public int day(){
          return  days;
        }
     }

     public class Time extends Date implements Delta{
       protected int secs;
       Time(){
          reset();
       }
       public void reset(){
         super.reset();
         secs=0;
       }
       public void inc(){
         secs++;
         if(secs>=86400){
           secs=0;
           super.inc();
         }
       }
       public int sec(){
         return secs;
       }

      public static void main(String s[]){//test out the classes
          //this function is not part of the answer
        Date d=new Date();
        Time t=new Time();
        d.inc();
        for(int j=0; j<90000;j++)
           t.inc();
        System.out.println("Date: "+d.day()+", Time: ["+t.day()+", "
              +t.sec()+"]");
      }
     }

     5. (20 pts) Assume that both class Ray and class Junk have public instance
     data member "a".  Further, assume that we have the array x defined as
     "Object x[]=new Object[10]" and that x[] contains 10 entries, where some
     entries are instances of Ray and other entries are instances of Junk.
     Write code which sorts x[] so that all instances of Junk precede all
     instances of Ray in x[] and then displays on the screen (DOS window)
     the values of "a" for each instance in x[].

     class Junk{
       public int a;
     }

     class Ray{
         public int a;

         static public void main(String s[]){
           Object x[]=new Object[10];
           for(int j=0;j<10; j+=2){
              x[j]=new Junk();
              ((Junk)x[j]).a=4;
              x[j+1]=new Ray();
               ((Ray)x[j+1]).a=6;
           }
           int top=9, bot=0;
           Object temp;

           while(top>bot){
             while(bot<top && x[bot] instanceof Junk)
                bot++;
             while(bot<top && x[top] instanceof Ray)
               top--;
             temp=x[top];
             x[top]=x[bot];
             x[bot]=temp;
           }
           for(int j=0;j<10;j++)
             if(x[j] instanceof Junk)
              System.out.println(((Junk)x[j]).a);
             else
              System.out.println(((Ray)x[j]).a);
         }
     }

     6.  (15 pts) Assume you want to define your own class for finding
     square roots so that you can deal with negative arguments with your own
     exception class.  Write the code for a class Sqrt and for a class
     NegException such that one can call Sqrt.sqrt(x) to find the square
     root of the double x and such that sqrt() throws a NegException if
     x<0. (You can use Math.sqrt() in your code.)
     public class Sqrt{
       static public double sqrt(double x) throws NegException{
          if(x<0)
             throw new NegException(x);
          else
            return Math.sqrt(x);
       }

       public static void main(String s[]){//test out the class.
          //this function is not part of the answer
        try{
          System.out.println(Sqrt.sqrt(7));
          System.out.println(Sqrt.sqrt(-4));
          System.out.println(Sqrt.sqrt(4));
        }catch(NegException e){
          System.err.println(e.getMessage());
          System.exit(1);
        }
       }
     }

    class NegException extends Exception{
      public NegException(double x){
        super("Negative Argument to Sqrt.sqrt(): "+x);
      }
    }

