Student

Currently I am a Computer Science major graduating in May 2016. I attend California State University, Long Beach.

Developer

I'm a front-end developer. I develop websites, mobile applications, desktop applications, and web applications.

Designer

I'm a web designer and a graphic designer with use of wordpress and blogger for web design and photoshop creative suite 5 for graphic design.

Business

Currently run a freelance business named Bell's Branding, Inc. writing websites and apps for new businesses and individuals.

Projects

Maximum Subsequence Algorithm

I thought this algorithm was important. It implements four algorithms for calculating the max subsequence sum based off freshman, sophomore, junior, and senior implementations. The program prompts the user as to how he or she prefers to provide the input array a to each of the algorithms: by being prompted to type a comma-delimited sequence of integers, specify the name of a fi le(consisting of comma-delimited integers) to be read in by the program, or give the length of the array to be randomly generated by the program, each element generated over the interval [-50,50].














import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;

public class MSS
{
    static Scanner input;
    static File filename;
    static int a1=0;
    static int an=-1;
    private static BufferedReader read;
    private static Random randomGenerator;
 
    private static int size;
    /**
     * This function displays the menu for the user to choose an option from
     */
    public void menu()
    {
    System.out.println("Menu: ");
    System.out.println("___________");
        System.out.println("Option 1: Type an array of integers: ");
        System.out.println("Option 2: Data file with an array integers: ");
        System.out.println("Option 3: Range with given intervals ");
        System.out.println("Option 4: Quit program");
    }
      /**
       * Constructor to handle the cases in the menu options
     * @throws IOException
       */
      public MSS() throws IOException
      {
           //Accepts user input
        Scanner in=new Scanner(System.in);
   
           //calls the menu method
     
           //Initializes the run variable making the program loop until the user terminates the program
        Boolean run=true;
     
           //While loop
           while(run)  
           {
          menu();
            int maxSum2;
int maxSum3;
int maxSum4;

switch (in.nextInt())
         
            {
                case 1:
               
              System.out.println("Option 1 selected");
              int maxSum;
          System.out.print("Enter list of comma-delimeted integers: ");
              //Read a line of user decimal input
              //BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
       
          Scanner scan = new Scanner(System.in);
          String input=scan.next();
            //  String input2=buffer.readLine();
              //handles the commas in the input
              String[] num = input.split(",");
              //puts the numbers into an array of doubles
              int[] a= new int[num.length];
              //for loop to find the length of the array given
              for (int i = 0; i < num.length; i++) {
           
                      a[i] = Integer.parseInt(num[i]);
              }
         
         
              Long tbefore=System.nanoTime();
           
  maxSum = freshman(a,a.length);
 
              Long ta=System.nanoTime();
              System.out.println( "Max sum freshman is " + maxSum );
              System.out.println("Freshman algorithm takes "+(ta-tbefore)/1000000+ " ms");
           
            tbefore=System.nanoTime();
  maxSum2=sophomore(a,a.length);
 
              Long ta2=System.nanoTime();
              System.out.println("Max sum sophomore is "+ maxSum2);
              System.out.println("Sophomore algorithm takes "+(ta2-tbefore)/1000000+ " ms");
           
           
              tbefore=System.nanoTime();
    maxSum3=junior(a,0,a.length-1);
    Long ta3=System.nanoTime();
              System.out.println("Max sum junior is "+maxSum3);
              System.out.println("Junior algorithm takes "+(ta3-tbefore)/1000000+ " ms");
           
              tbefore=System.nanoTime();
              maxSum4=senior(a,a.length);
              Long ta4=System.nanoTime();
              System.out.println("Max sum senior is "+ maxSum4);
             System.out.println("Senior algorithm takes "+(ta4-tbefore)/1000000+ " ms");
           
         
             break;
                case 2:
                System.out.println("Option 2 selected");
               
                BufferedReader reader = null;
                    Scanner sc = null;
                    String line = "";
                    try {
                       reader = new BufferedReader(new FileReader("integers.txt"));
                       sc =new Scanner(new BufferedReader(new FileReader("integers.txt")));

                       while (sc.hasNext()) {
                          line = sc.next();
                       }
                       String s[] = line.split(",");
                       a = new int[s.length];
                       for (int i = 0; i < s.length; i++) {
                          a[i] = Integer.parseInt(s[i]);
                       }
                    }
                    finally {
                       if (reader != null) {
                          reader.close();
                       }
                       
                    }
                  System.out.println(line);
                       
                       Long tbefore1=System.nanoTime();
              tbefore1=tbefore1/1000000;
                       
              maxSum = freshman(a,a.length);
             
                         ta=System.nanoTime();
                         ta=ta/1000000;
                         System.out.println( "Max sum freshman is " + maxSum );
                         System.out.println("Freshman algorithm takes "+(ta-tbefore1)+ " ms");
                       
                   
              maxSum2=sophomore(a,a.length);
             
             
                         ta2=System.nanoTime();
                         ta2=ta2/1000000;
                         System.out.println("Max sum sophomore is "+ maxSum2);
                         System.out.println("Sophomore algorithm takes "+(ta2-tbefore1)+ " ms");
                       
                     
                       
                maxSum3=junior(a,0,a.length-1);
               
                         ta3=System.nanoTime();
                         ta3=ta3/1000000;
                         System.out.println("Max sum junior is "+maxSum3);
                         System.out.println("Junior algorithm takes "+(ta3-tbefore1)+ " ms");
                       
                   
                         maxSum4=senior(a,a.length);
                     
                         ta4=System.nanoTime();
                        ta4=ta4/1000000;
                         System.out.println("Max sum senior is "+ maxSum4);
                        System.out.println("Senior algorithm takes "+(ta4-tbefore1)+ " ms");
        break;
                case 3:
                System.out.println("Option 3 selected");
                read = new BufferedReader(new InputStreamReader(System.in));
                   
                     randomGenerator = new Random();
                   
                 
                System.out.print("Please enter array size : ");
                     size = Integer.parseInt(read.readLine());
                   
                     System.out.print("Please enter the random range : ");
                     int random = Integer.parseInt(read.readLine());
                     // create array
                     a= new int[size];
                     int min=-17;
                     int max=17;
                     int range=max-min+1;
                     // fill array
                     for( int i=0; i<size; i++) {
                         a[i] = randomGenerator.nextInt()%range+min;
                     
                     }
                   
                     for(int  i=0; i<size; i++) {
                         System.out.println("a[" + i + "] = " + a[i]);
                     }
                    Long tbefore2=System.nanoTime();
            tbefore2=tbefore2/1000000;
                     
            maxSum = freshman(a,a.length);
           
                       ta=System.nanoTime();
                       ta=ta/1000000;
                       System.out.println( "Max sum freshman is " + maxSum );
                       System.out.println("Freshman algorithm takes "+(ta-tbefore2)+ " ms");
                     
               
            maxSum2=sophomore(a,a.length);
           
                     ta2=System.nanoTime();
                       ta2=ta2/1000000;
                       System.out.println("Max sum sophomore is "+ maxSum2);
                       System.out.println("Sophomore algorithm takes "+(ta2-tbefore2)+ " ms");
                     
                     tbefore2=System.nanoTime();
              tbefore2=tbefore2/1000000;
                     
              maxSum3=junior(a,0,a.length-1);
             
                      ta3=System.nanoTime();
                       ta3=ta3/1000000;
                       System.out.println("Max sum junior is "+maxSum3);
                       System.out.println("Junior algorithm takes "+(ta3-tbefore2)+ " ms");
                     
               
                       maxSum4=senior(a,a.length);
                     
                       ta4=System.nanoTime();
                      ta4=ta4/1000000;
                       System.out.println("Max sum senior is "+ maxSum4);
                      System.out.println("Senior algorithm takes "+(ta4-tbefore2)+ " ms");
                break;
                case 4:
                System.out.println("\nOption 4 selected");
               System.out.println("Program terminted");
                 run=false;
               break;
                  //default case if one of the options selected were not allowed
                default:
                    System.out.println("Not one of the options 1-3");
                    System.out.println("Program terminated.");
                  break;
                    }  
              }
      }
   
      /**
       * Main method
       * @param args
     * @throws IOException
       */
 
      public static void main(String[] args) throws IOException
      {
          //Calls the constructor
          new MSS();
          //static Scanner input;
       
      }
   
      public static int freshman(int [] a, int n)
      {
   
     int max_sum = 0;
          for (int i = 0; i < n; i++) {
             for (int j = i; j < n; j++) {
                int this_sum = 0;
                for (int k = i; k <= j; k++) {
                   this_sum += a[k];
                }
                if (this_sum > max_sum) {
                   max_sum = this_sum;
                }
             }
          }
          return max_sum;
      }
      public static int sophomore(int a[],int n) {
          int max_sum2 = 0;
          for (int i = 0; i < n; i++) {
             int this_sum = 0;
             for (int j = i; j < n; j++) {
                this_sum += a[j];
                if (this_sum > max_sum2) {
                   max_sum2 = this_sum;
                }
             }
          }
          return max_sum2;
       }

   
      public static int junior(int a[], int start, int end) {
       
     if (end - start <= 1) {
             return base(a, start, end);
          }
          int mid = (end + start) / 2;
          int mss_L = junior(a, start, mid);
          int mss_R = junior(a, mid + 1, end);
          int mss_mid = mss_mid(a, start, mid, end);
          return max(mss_L, mss_R, mss_mid);
       }
       //return sum of elements of array a from start to end
       public static int base(int a[], int start, int end) {
          if(end - start == 1){
            return (Math.max(Math.max(a[start],a[end]),(a[start]+a[end])));
          }
          return a[start];
       }
       //add the resultant of left and right and return the sum
       public static int mss_mid(int a[], int start, int mid, int end) {
          int right = 0;
          int left = 0;
          int max_sum=0;
          for (int i = mid + 1; i <= end; i++) {
             left +=a[i];
             if(max_sum < left)
            max_sum =left;
          }
          left=max_sum;
          max_sum=0;
          for (int i = mid; i >= start; i--) {
             right +=a[i];
             if( max_sum < right)
            max_sum =right;
           
          }
          right=max_sum;
          return (right + left);
       }
       //Calculate the max of left, right, and mid variables
       public static int max(int L, int R, int M) {
          return Math.max(Math.max(L, R), M);
       }
     
public static int senior( int a[],int n) {
    int max_sum = 0;
    int this_sum = 0;
    for (int i = 0; i < n; i++) {
       this_sum += a[i];
       if (this_sum > max_sum) {
          max_sum = this_sum;
       }
       else if (this_sum < 0) {
          this_sum = 0;
       }
    }
    return max_sum;
 }



}

No comments:

Post a Comment

Available Apps

Powered by Blogger.