Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Find Dot Product of a 2D vector
- *
- * Recommend to Use Eclipse IDE for javadocs ready.
- * You will love it if you know it. Latest version
- * of NetBeans have this feature as well. I'm not sure.
- */
- /**
- * @author ak47suk1
- * http://kaibathelegacy.blogspot.com
- * *remove-words-between-asterisk*kaibathelegacy[at]gmail[dott]com
- * Copyleft Right To Copy 2010
- * Save as DotProduct.java
- */
- import java.util.*;
- public class DotProduct {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /**
- * @param Make sure to initialize Scanner class
- */
- Scanner s= new Scanner(System.in);
- /**
- * @param while not reaching end of file (EOF)
- * will continue retrieve input
- */
- while (s.hasNext()){
- /**
- * @param retrieve test case number
- */
- int testCase=s.nextInt();
- /**
- * @param start test Case loop according
- * to number of testcase given. Max 100
- */
- for (int A = 0;A<testCase; A++){
- /**
- * @param USE ArrayList instead of array? Why?
- * Clue: expendable array + can avoid 10,000 element
- * JAVA array limit. Faster execution time as well.
- *
- * Initialize two Integer ArrayList. Mind that you can't
- * use int, double, string, bleh, blah here.
- * Use ArrayList<Integer>, ArrayList<Double>, ArrayList<String>
- * and others.
- *
- * See below for example
- */
- ArrayList<Integer> vector1 = new ArrayList<Integer>();
- ArrayList<Integer> vector2 = new ArrayList<Integer>();
- /**
- * @param initialize dimension of the vector
- */
- int dimension = s.nextInt();
- /**
- * @param retrieve elements of vector 1
- */
- for (int B = 0;B<dimension;B++){
- vector1.add(s.nextInt());
- }
- /**
- * @param retrieve elements of vector 2
- */
- for (int C= 0;C<dimension;C++){
- vector2.add(s.nextInt());
- }
- /**
- * @param Case Counter for formatting.
- *
- * Example:
- *
- * Case #1: 14
- *
- * WTF with this kind of output BTW? Simply stupid for me
- * to think about it :D
- *
- * caseCounter to count case value :)
- */
- int caseCounter = (A+1);
- System.out.println("Case #" + caseCounter + ": " + dotProd(vector1,vector2));
- }
- }
- }
- /**
- * @param One of important aspect in software development.
- * Expandability and modularity. I separate dotProd for
- * faster execution and reduce complexibility.
- *
- * Revise your knowledge on Java Method, parse value and bla3.
- */
- public static Integer dotProd(ArrayList<Integer> vector1, ArrayList<Integer> vector2){
- /**
- * @param not really needed in ICPC but GOOD
- * programming practice. check vector size
- * and throw exception.
- */
- if(vector1.size() != vector2.size()){
- throw new IllegalArgumentException("The dimensions have to be equal!");
- }
- /**
- * @param vector dot product calculation
- */
- Integer sum = 0;
- for(int i = 0; i < vector1.size(); i++){
- sum += vector1.get(i) * vector2.get(i);
- }
- /**
- * @param return value to main method.
- */
- return sum;
- }
- }
- /**
- * @param good luck to all USIM Al-Khawarizmi.
- * Ermmm, luck is not a main factor BTW.
- * It's passion and spirit that count
- * I guess... :D
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement