CS670K Homework Set 3 - Fall 1998

Due: Thursday, October 15

© Copyright University of New Haven 1998

  1. class R                                       class S
    {                                             {
         static int i;                                 static int a;
    
         static int j;                                 static int b;
    
         static S y= new S( );                         static R x= new R();
    
         public static void main(String s[])	          S()
         {	                                            {
                 S.a= 1;                                       R.i= 1;
    //INVISIBLE  S.b= 2;                          //INVISIBLE  R.j= 2;
                 S.x.i= 3;                        //INVISIBLE  R.y.a= 3;
                 S.x.j= 4;                        //INVISIBLE  R.y.b= 4;
                 y.a= 5;                               }
    //INVISIBLE  y.b= 6;                          }
         }
    }
    
    Apply the private visibility modifier to the variable declarations in classes R and S so as to render the assignments marked INVISIBLE invalid, BUT LEAVING THE REMAINING ASSIGNMENTS VALID.

    
    class A  // Classes for questions 2 - 6...
    {
         B x= new B(12L);
         int k= 33;
    }
    
    class B
    {
         void method(int i)   { System.out.println(2*i); }
         void method(float f) { System.out.println(f); }
         B(int i)             { System.out.println(i); }
         B(long l)            { System.out.println(new A().k); }
    }
    

    Put what is printed, or COMPILE ERROR, or RUN-TIME ERROR as appropriate:

  2. new A().x.method(-5);

  3. new B(99).method(-17.32);

  4. new A(36).x.method(2);

  5. new B().method(-101);

  6. new B(13).method(0.45F);

  7. Home Page