Friday, November 17, 2017

Package-private (no explicit modifier)

In recent courses, I was privileged to conduct, I noticed a disturbing trend of student skill degradation. These skill disappearance I trace back to the popularity of mobile devices and the wide use of YouTube videos that skip details.  Video lectures especially lectures by non-lecturers lead to a business venture driven by profit and the aim for keeping within current attention span of young adults create a skill gap that is very hard to reverse.

Mobil devices eliminate the cognitive process of thinking about file names, directory names, proper naming conventions, command line utility usage, utility help interpretation, and security right considerations essential for anyone interested to learn technology related fields like Computer Science. 

YouTube video and online courses aim to restrict video lengths to short precise videos that focus on how to perform a single task without explaining "why" the task is performed or "how" the task, to be performed, was researched by the presenter.

One of this trend I see in Object Oriented Programming (OOP) like Java is the aim to keep all source code in a single file for convenience of not handling multiple source files.  The aim of OOP in a learning environment is to lean problem solving by dividing the problem into specific objects that perform specific tasks for re-usability and sharing. 

In Java, source code can contain many classes, but classes are defined by class access modifiers and only one class can be a public class.  No modifier in class definition makes the class private to its package level, thus other packages can not instantiate those classes.  The compiled code will create a separate byte code file that can be shared, but other than the specified package would not be able to instantiate the class.

Like in the code below, the MyDefaultClass class has no access modifier, thus it is private to the package testing and no other package can instantiate it. This code is easy to talk about, easy to present in a single screen video tutorial, but it is damaging to skill development and to practice file management.  This example should be saved into two separate source files and MyDefaultClass should be made public so every package could instantiate the class in other application if needed.

package testing;

class MyDefaultClass{
    private String msg;
    public MyDefaultClass(String m){
        msg=new String(m);
    }
    public String getMsg(){
        return msg;
    }
}

public class Driver {
    public static void main(String[] args) {
        MyDefaultClass t=new MyDefaultClass("Hello");
        System.out.println("Hello World");
    }
}


As you can see below, we have one source code, but the compiled program produced two byte codes.  So, one of them can be shared.  In this case, MyDefaultClass cold be used in other applications since there is nothing indicating that it is tied to a package, as we'll see later.
 


In order to test this, we can create another package and see if we can instantiate the class in that package. (Note: This is another skill gap I see in courses, students are not taught and not driven to learn how to test concepts or theories. )

So, start a new package, in this case it is testAgain package and create a simple "Hello World" application in the package ( in this case, Driver ).  Now, just copy the MyDefaultClass.class from package testing to package testingAgain.

The source code, in this case, only has one single public class, so instantiating the class MyDefaultClass will happen from the bytecode directly ( if it will work )


 Adding the statement to instantiate the class MyDefaultClass will not work since the class is private to testing package.


 Now, let's create a separate source code for MyPublicClass in package testing. In this case, the class can be set to public, so regardless of the package, the class can be used in any program.


So, copy the MyPublicClass to testAgain package to test in that package.


Importing the class path and adding the statement to instantiate the class woks just fine since the class MyPublicClass access modifier is set to public this time.


Conclusion
Always seek to answer the "WHYs", in coding, in order to become a problem solver.  Video lectures are created for a quick lesson not as an educational resource, don't think that a quick video will be able to teach you what you need to know about the code, they are only a tool to help you overcome simple concept confusions. Videos will never replace search, reading, and critical thinking through collaboration. 


Appendix A - Access level modifiers
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:
  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—public, private, protected, or package-private (no explicit modifier).

Thursday, July 6, 2017

Exception - Can not Get any Simpler than This

Exception handling by custom exceptions is as easy as extending the Exception class. 


java.lang.Object
     java.lang.Throwable
         java.lang.Exception

Look at the API documentation to learn what you can do like what constructors are available and what methods you will have access to by extending this class.

Method Summary

  The most useful constructor for this purpose is the constructor that takes a string as its parameter since it will allow you to set a custom message for your custom exception class.  To get this message later, you will use the getMessage() method that is inherited from Throwable ( as seen above ).

To practice this concept, implement the following code and make modifications as you see it fit.  Create more exceptions and change the messages until it makes sense and you feel comfortable with this easy and useful object oriented concept.  Have fun!




public class HelloDriver{
    public static void main(String args[])throws MyException{  //the other is passed to JVM
         try{
             Hello msg=new Hello("Hello"," World");
             System.out.println(msg.getHello());         
         }
         catch(MyException2 e){                                        //one of the exceptions are handled here
             System.out.println("Zoltan: "+e.getMessage());
         }
    }
}

==============================================================

public class Hello extends World{
private String hello;

//because the parent constuctor with string parameter was called, the exception must be handled
//coming from the parent's constructor and also from this constructor
public Hello(String h, String w)throws MyException2,MyException{
    super(w);
    if(h.equals("He11o"))
       hello=h;
    else
        throw new MyException2();
}

public String getHello(){
    return hello+getWorld();
}
}

===============================================================

public class World{
private String world;

public World(String msg)throws MyException{   //pas the exception out of the constuctor
    if(msg.equals(" World"))
       world=msg;
    else
       throw new MyException();     //create exception if condition is met
}

public String getWorld(){
    return world;
}
}

===============================================================

public class MyException extends Exception
{
public MyException(){
    super("Your message was cool!!!");   //call the parent class Exception constructor with string parameter
}
}

================================================================

public class MyException2 extends Exception
{
public MyException2(){
    super("Your message was the coolest!!!"); //call the parent class Exception constructor with string parameter
}
}

Wednesday, June 28, 2017

Simple Inheritance






public class HelloWorld{
    public static void main(String args[]){
        Greet msg=new Greet();
        System.out.println(msg.getMessage());
        System.out.println(msg.getThankYou());
        msg.getMyMessage();
    }
}
public class Greet extends Message{
       private String msg=new String("Computer Science - ");
       public Greet(){
           super();
        }
    public String getMessage(){
        return msg+getWord();
    }
}

public class Message extends Goodbye implements Welcome {
       private String word;
       public Message(){
           System.out.println("Did you just call me?");
           word="Hello World";
        }
    public final String getWord(){   //try to override this in message
        return word;
    }
    public void greetMe(String m){
        System.out.println("Welcome to my application");
    }
    public String getThankYou() {
        return thanks;
    }
}
public abstract class Goodbye{
    protected String thanks="Thank you for visiting us";
    public abstract String getThankYou();
    public void getMyMessage(){
        System.out.println("This is from the abstract class");
    }
}

public interface Welcome{
    void greetMe(String m);
    //only default and public class modifier can be used
    //try to copy a method with body - fail
}

Tuesday, June 20, 2017

Aggregate class - Tutorial






public class SentenceDemo{
   public static void main(String[] args){
      
       Sentence s1=new Sentence();
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getMessage());
       System.out.println(s1.getCounter());
    }
}

public class Sentence{
  private String message;
  public static final int SIZE=5;
  private static int counter;
  private String messages[]={
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
      new Pronoun().getWord()+new Verb().getWord()+new Adjective().getWord()+new Adverb().getWord(),
    };
  public Sentence(){
      java.util.Random ran=new java.util.Random();
      message=messages[ran.nextInt(SIZE)]+".";
     counter++;
    }
  private Sentence(String message){
      super();
      this.message=message;
      counter++;
    }
  public String getMessage(){
      java.util.Random ran=new java.util.Random();
      message=messages[ran.nextInt(SIZE)]+".";
      message=message.replaceFirst("[a-zA-Z]", ""+Character.toUpperCase(message.charAt(0)));
      return message;
    }
    public Sentence deepCopy(Sentence s){
        return new Sentence(s.message);
    }
    public boolean equals(Sentence s){
        return (message.equals(s.message))?true:false;
    }
    public int getCounter(){
        return counter;
    }
}

public class Verb{
  private String word;
  private String words[]={" write "," run "," drive "," walk ","crawl"};
  public Verb(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Pronoun{
  private String word;
  private String words[]={"I ","you ","me ","we ","us "};
  public Pronoun(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Adjective{
  private String word;
  private String words[]={" very "," quiet ","  "," short "," creepy "};
  public Adjective(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}

public class Adverb{
  private String word;
  private String words[]={" quickly "," very "," slowly "," beautifully "," happily "};
  public Adverb(){
      java.util.Random ran=new java.util.Random();
      word=words[ran.nextInt(Sentence.SIZE)];
    }
  public String getWord(){
      return word;
    }
}