Advertisement
ak47suk1

Vector Dot Product

Oct 1st, 2010
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. /**
  2.  * Find Dot Product of a 2D vector
  3.  *
  4.  * Recommend to Use Eclipse IDE for javadocs ready.
  5.  * You will love it if you know it. Latest version
  6.  * of NetBeans have this feature as well. I'm not sure.
  7.  */
  8.  
  9. /**
  10.  * @author ak47suk1
  11.  * http://kaibathelegacy.blogspot.com
  12.  * *remove-words-between-asterisk*kaibathelegacy[at]gmail[dott]com
  13.  * Copyleft Right To Copy 2010
  14.  * Save as DotProduct.java
  15.  */
  16. import java.util.*;
  17. public class DotProduct {
  18.  
  19.  
  20.     /**
  21.      * @param args
  22.      */
  23.  
  24.     public static void main(String[] args) {
  25.         // TODO Auto-generated method stub
  26.         /**
  27.          * @param Make sure to initialize Scanner class
  28.          */
  29.         Scanner s= new Scanner(System.in);
  30.         /**
  31.          * @param while not reaching end of file (EOF)
  32.          * will continue retrieve input
  33.          */
  34.  
  35.         while (s.hasNext()){
  36.             /**
  37.              * @param retrieve test case number
  38.              */
  39.             int testCase=s.nextInt();
  40.            
  41.             /**
  42.              * @param start test Case loop according
  43.              * to number of testcase given. Max 100
  44.              */
  45.  
  46.            
  47.             for (int A = 0;A<testCase; A++){
  48.                 /**
  49.                  * @param USE ArrayList instead of array? Why?
  50.                  * Clue: expendable array + can avoid 10,000 element
  51.                  * JAVA array limit. Faster execution time as well.
  52.                  *
  53.                  * Initialize two Integer ArrayList. Mind that you can't
  54.                  * use int, double, string, bleh, blah here.
  55.                  * Use ArrayList<Integer>, ArrayList<Double>, ArrayList<String>
  56.                  * and others.
  57.                  *
  58.                  * See below for example
  59.                  */
  60.                 ArrayList<Integer> vector1 = new ArrayList<Integer>();
  61.                 ArrayList<Integer> vector2 = new ArrayList<Integer>();
  62.                
  63.                 /**
  64.                  * @param initialize dimension of the vector
  65.                  */
  66.                 int dimension = s.nextInt();
  67.                
  68.                 /**
  69.                  * @param retrieve elements of vector 1
  70.                  */
  71.                 for (int B = 0;B<dimension;B++){
  72.                     vector1.add(s.nextInt());
  73.                 }
  74.                 /**
  75.                  * @param retrieve elements of vector 2
  76.                  */
  77.                 for (int C= 0;C<dimension;C++){
  78.                     vector2.add(s.nextInt());
  79.                 }
  80.                 /**
  81.                  * @param Case Counter for formatting.
  82.                  *
  83.                  * Example:
  84.                  *
  85.                  * Case #1: 14
  86.                  *
  87.                  * WTF with this kind of output BTW? Simply stupid for me
  88.                  * to think about it :D
  89.                  *
  90.                  * caseCounter to count case value :)
  91.                  */
  92.                 int caseCounter = (A+1);
  93.                 System.out.println("Case #" + caseCounter + ": " + dotProd(vector1,vector2));
  94.            
  95.             }
  96.            
  97.         }
  98.  
  99.     }
  100.     /**
  101.      * @param One of important aspect in software development.
  102.      * Expandability and modularity. I separate dotProd for
  103.      * faster execution and reduce complexibility.
  104.      *
  105.      * Revise your knowledge on Java Method, parse value and bla3.
  106.      */
  107.     public static Integer dotProd(ArrayList<Integer> vector1, ArrayList<Integer> vector2){
  108.        
  109.         /**
  110.          * @param not really needed in ICPC but GOOD
  111.          * programming practice. check vector size
  112.          * and throw exception.
  113.          */
  114.        
  115.         if(vector1.size() != vector2.size()){
  116.             throw new IllegalArgumentException("The dimensions have to be equal!");
  117.         }
  118.        
  119.         /**
  120.          * @param vector dot product calculation
  121.          */
  122.         Integer sum = 0;
  123.         for(int i = 0; i < vector1.size(); i++){
  124.             sum += vector1.get(i) * vector2.get(i);
  125.         }
  126.        
  127.         /**
  128.          * @param return value to main method.
  129.          */
  130.         return sum;
  131.        
  132.     }
  133.  
  134. }
  135. /**
  136.  * @param good luck to all USIM Al-Khawarizmi.
  137.  * Ermmm, luck is not a main factor BTW.
  138.  * It's passion and spirit that count
  139.  * I guess... :D
  140.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement