Friday, November 18, 2016

Inheritance and Polymorphism Revisited - Challenge



Start with the UML below.  You should try to implement this UML with a simple code not even looking at the code below.  See if you can successfully interpret the UML.


You should interpret the UML above and write the problem statement this UML solves.

You can compare your code to the code below or use it as a reference if you get stuck.

/** Animal class that is used as the super class with different modifiers to help understand inheritance
* @version 1.0.0.0
* @author Zoltan Szabo
*/
public class Animal{
    public int x=50;
    private int y=60;
    protected int z=70;
    int k=80;
  
    public static int XX=150;
    private static int YY=160;
    protected static int ZZ=170;
    static int KK=180;
  
/** Super class no-arg constuctor*/
   public Animal(){
      System.out.println("This is the superclass (Animal) constructor.");
   }

/** Public method in the superclass
* @return x public field from superclass
*/
   public int func1(){
        return x;  
   }

/** Private method in the superclass
* @return y private field from superclass
*/   private int func2(){
        return y;
   }

/** Protected method in the superclass
* @return z protected field from superclass
*/   protected int func3(){
        return z;
   }

/** Public method in the superclass
* @return k default field from superclass
*/   int func4(){
        return k;
   }
}

/**
*Cat class that is a child of the Animal class inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/ public class Cat extends Animal{
/** SubSubClass1 class no-arg constuctor*/
   public Cat(){
      System.out.println("I\'m a Cat constructor.");
   }
}

/**
*Class that is a child of the Animal class inherits public, protected, and default attributes and methods
*@version 1.0.0
*@author Zoltan Szabo
*/

public class Dog extends Animal{

/** SubClass1class no-arg constuctor*/
   public Dog(){
      System.out.println("I'm a Dog constructor.");
   }
   public void doTrick(){
       System.out.println("I can roll over.");
    }
}

/** This simple application will help you understand polymorphism
* @version 1.0.0
* @author Zoltan Szabo
*/

public class AnimalsDemo{
    final static int INDEX=2;

/** application entry point to test inheritance of modifiers
* @param args as a String array for command argument input
*/

   public static void main(String[] args)   {

      Animal obj[]=new Animal[5];
      obj[0]=new Animal();
      obj[1]=new Dog();
      obj[2]=new Cat();
     /*
      * according to the java language specification, 3rd edition:
      * 6.6.8 Example: private Fields, Methods, and Constructors
      * A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
      * It is not inherited by subclasses.
      */
     //Instantiate one of each objects in an Animal array of objects
     //Display collar color for each animals and the length of the leash where it applies.
   }
}

Now take the initial code and extend the application to implement this UML.

 The output should look like this.



Wednesday, November 2, 2016

UML in BlueJ and Eclipse

Unified Modeling Language ( UML ) is the best way to design an application and to analyze an object oriented code.

Taking a code and easily turn it into a UML can be done with an IDE, but there are extensions for IDEs that do not support UML generation by default.  BlueJ has two extensions that can be used for this purpose.

BlueJ UML extensions video if you need it
https://youtu.be/e5h15mTvEP4

Copy the jar files into folder <BLUEJ_HOME>\lib\extensions

Class Card - A Better UML Extension - http://klassenkarte.steinhuber.de/
Con: No datatypes for attributes and parameters
Pro: Shows attributes

UML Extensionhttp://www.bluej.org/extensions/extensions.html 

Con: No attributes listed
Pro: Parameter and return value data types


 After adding the extensions, they will be available by right-clicking on the class you need to see its UML.


You can see the difference between the two extensions and decides witch one works for you.


At the end, you'll see that Eclipse does a much better job showing the UML based outline of the class and if you don't really need pretty graphics based UML, it will be the most complete UML with the least effort.