Saturday, November 19, 2016

Object Oriented Programming for Modularity - ROT-13

Many students starting out in Java try to solve a problem using a single class and thinking only about the application not its components and "building blocks".

In this example, writing an application to encrypt a string is simple to do in a single application, but the ROT-13 algorithm is not just useful in this application, but it should be used in many other applications as well. While the application interface and driving code are application specific, the ROT-13 algorithm is not and should be separated into a modular structure.




import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/** This simple application will help you understand polymorphism
 * @version 1.0.0
 * @author Zoltan Szabo
 */
public class Rot13Demo{
      public static void main(String[] args) {
            try{
                  File inFile=new File("c:\\temp\\infile.txt");
                  File outFile=new File("c:\\temp\\outfile6.txt");
                  Scanner input=new Scanner(inFile);
                  PrintWriter output=new PrintWriter(outFile);
                  while(input.hasNext()){
                        String temp=input.nextLine();
                        for(int i=0;i<temp.length();i++){
                              char ch=temp.charAt(i);
                              if(Character.isLowerCase(ch)){
                                    if(ch<='m')
                                          output.print((char)(ch+13));
                                    else
                                          output.print((char)(ch-13));
                              }
                              else if(Character.isUpperCase(ch)){
                                    if(ch<='M')
                                          output.print((char)(ch+13));
                                    else
                                          output.print((char)(ch-13));
                              }
                              else
                                    output.print(ch);
                        }
                        output.println();
                  }
                  input.close();
                  output.close();
            }

            catch(IOException e){
                  System.out.println("Error: Check your input and output files and locations.");
            }
      }
}

If you are more adventurous, you can simplify the decision making and the "heart" of the algorithm with ternary operator, like this:


output.print((Character.isLowerCase(ch))?(ch<='m')?(char)(ch+13):(char)(ch-13):(Character.isUpperCase(ch))?(ch<='M')?(char)(ch+13):(char)(ch-13):ch);

Look at the simplicity of this driver class where the programmer can focus on the interface and how to handle the input and output files, while the algorithm can be used in any other applications.   


import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/** This simple application will help you understand object oriented programming and an introduction to encryption
* @version 1.0.0
* @author Zoltan Szabo
*/
public class Rot13Demo{
   public static void main(String[] args)  throws IOException {
       File inFile=new File(args[0]);
       File outFile=new File(args[1]);
       Scanner input=new Scanner(inFile);
       PrintWriter output=new PrintWriter(outFile);
    while(input.hasNext()){
        String temp=input.nextLine();
        output.println(Rot13.encode(temp));
    }
    input.close();
    output.close();
   }
}

Since there are no attributes, the static method is a better choice for this method than a method belonging to an instantiated object.

/** This simple class helps you understand the static methods as a helper class for a collection of encryption algorithms.
* @version 1.0.0
* @author Zoltan Szabo
*/

public class Rot13{
/** This simple method will encode the temp string using ROT-13
* @param temp string to be encoded that can be a string encoded using ROT-13 to decode since ROT-13 is circular encryption algorithm
* @return The 13 character shifted string result
*/
   public static String encode(String temp) {
    StringBuilder encoded=new StringBuilder();
        for(int i=0;i<temp.length();i++){
            char ch=temp.charAt(i);
            if(Character.isLowerCase(ch)){
                if(ch<='m')
                    encoded.append((char)(ch+13));
                else
                    encoded.append((char)(ch-13));
            }
            else if(Character.isUpperCase(ch)){
                if(ch<='M')
                    encoded.append((char)(ch+13));
                else
                    encoded.append((char)(ch-13));
            }
            else
                  encoded.append(ch);
    }
    return encoded.toString();
   }
}

So, think about the generic and blocks in your code that should be independent from a specific application and design your code with objects in mind for mobility and modularity.

No comments:

Post a Comment