SHARE
TWEET

VectorAdder

Mary_99 Apr 3rd, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. class DifferentVectorsLengthException extends Exception
  4. {
  5.     private static final long serialVersionUID = 1L;
  6.     /* SerialVersionUID is used for version control of object.
  7.     To have possibility to modify any field in class and be able to recover it */
  8.     public DifferentVectorsLengthException(Vector vect1, Vector vect2)//read the lengths of vectors
  9.     {
  10.         super();//calls the parent constructor with no arguments.
  11.         System.out.println("VECTORS ARE NOT EQUAL!");
  12.         vect1.setComponents();
  13.         vect2.setComponents();
  14.     }
  15. }
  16.  
  17. class Vector
  18. {
  19.     int xVariable;
  20.     int yVariable;
  21.     int zVariable;
  22.  
  23.     void setComponents()
  24.     {
  25.         @SuppressWarnings("resource")//ignore wornning about unuse variable
  26.         Scanner variablesScanner = new Scanner(System.in);
  27.         String input;
  28.  
  29.         while (true)
  30.         {
  31.             System.out.println("INPUT X:");
  32.             input = variablesScanner.nextLine();
  33.             try
  34.             {
  35.                 xVariable = Integer.parseInt(input);
  36.                 break;
  37.             }
  38.             catch (NumberFormatException exception)
  39.             {
  40.                 System.out.println("WRONG INPUT!");
  41.             }
  42.  
  43.         }
  44.         while (true)
  45.         {
  46.             System.out.println("INPUT Y:");
  47.             input = variablesScanner.next();
  48.             try
  49.             {
  50.                 yVariable = Integer.parseInt(input);
  51.                 break;
  52.             }
  53.             catch (NumberFormatException exception)
  54.             {
  55.                 System.out.println("WRONG INPUT!");
  56.             }
  57.         }
  58.  
  59.         while (true)
  60.         {
  61.             System.out.println("INPUT Z:");
  62.             input = variablesScanner.next();
  63.             try
  64.             {
  65.                 zVariable = Integer.parseInt(input);
  66.                 break;
  67.             }
  68.             catch (NumberFormatException exception)
  69.             {
  70.                 System.out.println("WRONG INPUT!");
  71.             }
  72.         }
  73.     }
  74.  
  75.     void DisplayValues()
  76.     {
  77.         System.out.println(xVariable + ", " + yVariable + ", " + zVariable);
  78.     }
  79.  
  80. }
  81.  
  82. public class VectorAdder
  83. {
  84.         static void AddVectors(Vector vect1, Vector vect2)
  85.         {
  86.             int sumVector1;
  87.             int sumVector2;
  88.             sumVector1 = vect1.xVariable + vect1.yVariable + vect1.zVariable;
  89.             sumVector2 = vect2.xVariable + vect2.yVariable + vect2.zVariable;
  90.  
  91.             if (sumVector1 == sumVector2)
  92.             {
  93.                 int xSum = vect1.xVariable  + vect2.xVariable,
  94.                         ySum = vect1.yVariable  + vect2.yVariable,
  95.                         zSum = vect1.zVariable  + vect2.zVariable;
  96.                 System.out.printf("The product vector is: %d, %d, %d", xSum, ySum, zSum);
  97.             }
  98.             else
  99.             {
  100.                 try
  101.                 {
  102.                     throw new DifferentVectorsLengthException(vect1, vect2);
  103.                 }
  104.                 catch (DifferentVectorsLengthException e)
  105.              {
  106.                     AddVectors(vect1, vect2);
  107.              }
  108.             }
  109.         }
  110.  
  111.     public static void main(String[] args)
  112.   {
  113.  
  114.         Vector vector1 = new Vector();
  115.         Vector vector2 = new Vector();
  116.         vector1.setComponents();
  117.         vector2.setComponents();
  118.         AddVectors(vector1, vector2);
  119.     }
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top