Class Design

 

  1. The object-oriented paradigm is a development strategy based on the concept that systems should be built from a collection of reusable components called objects.

     

     

     

    The Game of Life
       
    • each cell has 8 neighbors
    • if 1 or 0 neighbors are alive, a cell dies from loneliness
    • if 2 neighbors are alive, cell remains unchanged
    • if 3 neighbors, cell comes alive
    • if 4 or more, a cell dies from overcrowding

     

     

  2. What is an object?

     

     

     

    Can we define an object as object = data + methods?

     

     

     

  3. What is encapsulation?
    1. Data hiding
    2. Implementation hiding
    3. All of the above
    4. None of the above

     

     

    Objects are defined by what they DO, not by what they contain.

     

     

    Do not ask object for data, but rather for work to do over that data

    An example, ATM.

     

     

    In many ways, designing good programs is more challenging that writing the code itself.

     

     

    Bathroom Effect.

    Suppose we built houses the same way we build software...


    Owner: Where are the bathrooms?
    Contractor: What do you mean? There are no bathrooms in your specification.
    Owner: Who would be dumb enough to build a house without a bathroom?? It never occurred to me to require them.

     

     

  4. Learning OOP:

     

    You will need to know

    1. how to define a class (and the methods and fields in that class),
    2. how to define a class by composition
    3. how to define a class using an interface
    4. how to define a class by inheritance
    5. and to understand polymorphism.

     

     

     

     

     

  5. Constructors:

     

    What are three differences between a constructor and a method?

     

     

     

  6. Method overloading:

     

    What is a method's signature?
    a)   the name of the method along with the number of its parameters.
    b)   the name of the method along with the number and type of its parameters.
    c)   the name of the method along with the number, type and order of its parameters.
    d)   the name of the method, its return type and the number, type, order of its parameters.

     

     

    public void foobar()
    {}
    public void foobar(int z)
    {}
    public void foobar(int z, String str)
    {}
    public void foobar(String str, int z)
    {}
    

     

     

     

  7. Scope:

     

    Examine the following code segment, find an error and fix it

    int k = 1;
    for(int sum = 0; k ‹ 2; k++)
    {
       sum += k;
    }
    
    while(k ‹ 3)
    {
       sum += k;
       k++;
    }
    

     

     

     

    Examine the following Java classes and predict the output

    public class Scope
    {
       public static void main(String[] args)
       {
          Test t = new Test();
          t.methodA();
          t.methodB();
       }
    }
    public class Test
    {
       private int x = 21;
    
       public Test()
       {
          x = 15;
       }
       public void methodA()
       {
          int x = 5;
          System.out.println( x );
          System.out.println( this.x );
       }
       public void methodB()
       {
          System.out.println( x );
          x = 7;
          System.out.println( this.x );
       }
    }
    

     

     

  8. Static and instance members:

     

    Can static methods call non-static methods in the same class? Explain your answer.

     

     

     

    Examine the following code segment. What is the output if this code is compiled?

    public class Static
    {
       public static void main(String[] ags)
       {
          Test obj1 = new Test();
          Test obj2 = new Test();
          Test obj3 = obj2;
          System.out.println(obj3.number);
          System.out.println(obj3.var);
       }
    }
    
    public class Test
    {
       public static int number = 0;
       public int var = 0;
    
       public Test()
       {
          number++;
          var++;
       }
    }
    

     

     

     

  9. Pass-by-value:

     

    Examine the following code segment.

    public class Demo
    {
       public static void main(String[] ags)
       {
          String s = "change me";
          changeIt(s);
          System.out.println(s);
       }
    
       public static void changeIt(String x)
       {
          x = "changed";
       }
    }