fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.math.BigDecimal;
  5. import java.math.RoundingMode;
  6.  
  7. class MyVector {
  8. private double x;
  9. private double y;
  10. public MyVector(double x, double y) {
  11. this.x = x;
  12. this.y = y;
  13. }
  14. public double vLength() {
  15. double tempLen = Math.sqrt(x*x + y*y);
  16. double newLen = new BigDecimal(tempLen).setScale(3, RoundingMode.UP).doubleValue();
  17. return newLen;
  18. }
  19. public static MyVector sum(MyVector a, MyVector b) {
  20. return new MyVector(a.x + b.x, a.y + b.y);
  21. }
  22. public static MyVector sub(MyVector a, MyVector b) {
  23. return new MyVector(a.x - b.x, a.y - b.y);
  24. }
  25. public static MyVector mul(MyVector a, double c) {
  26. return new MyVector(a.x * c, a.y * c);
  27. }
  28. public static double scalar(MyVector a, MyVector b) {
  29. double tempScalar = a.x * b.x + a.y * b.y;
  30. double newScalar = new BigDecimal(tempScalar).setScale(3, RoundingMode.UP).doubleValue();
  31. return newScalar;
  32. }
  33. public static double cos(MyVector a, MyVector b) {
  34. double tempCos = scalar(a, b) / (a.vLength() * b.vLength());
  35. double newCos = new BigDecimal(tempCos).setScale(3, RoundingMode.UP).doubleValue();
  36. return newCos;
  37. }
  38. public static double projection(MyVector a, MyVector b) {
  39. double tempProj = scalar(a, b) / b.vLength();
  40. double newProj = new BigDecimal(tempProj).setScale(5, RoundingMode.UP).doubleValue();
  41. return newProj;
  42. }
  43. public static boolean isCollinear(MyVector a, MyVector b) throws ArithmeticException {
  44. double eps = 0.0001;
  45. if (b.x == 0 || b.y == 0) {
  46. throw new ArithmeticException("Zero divider");
  47. }
  48. return (Math.abs(a.x / b.x - a.y / b.y) < eps);
  49. }
  50. public static double vecProduct(MyVector a, MyVector b) {
  51. double tempProduct = a.vLength() * b.vLength() * Math.sqrt(1 - cos(a,b) * cos(a,b));
  52. double newProduct = new BigDecimal(tempProduct).setScale(5, RoundingMode.UP).doubleValue();
  53. return newProduct;
  54. }
  55. public String toString() {
  56. return new String(x + " ; " + y );
  57. }
  58. }
  59.  
  60. class Main {
  61. public static void main (String[] args) {
  62. MyVector a = new MyVector(10.1,-19);
  63. MyVector b = new MyVector(13,-2.09);
  64. System.out.println(a.vLength());
  65. System.out.println(b.vLength());
  66. System.out.println(MyVector.sum(a,b));
  67. System.out.println(MyVector.sub(a,b));
  68. System.out.println(MyVector.mul(a,8));
  69. System.out.println(MyVector.scalar(a,b));
  70. System.out.println(MyVector.cos(a,b));
  71. System.out.println(MyVector.projection(a,b));
  72. System.out.println(MyVector.isCollinear(a,b));
  73. System.out.println(MyVector.vecProduct(a,b));
  74. }
  75. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
21.518
13.167
23.1 ; -21.09
-2.9000000000000004 ; -16.91
80.8 ; -152.0
171.01
0.604
12.98778
false
225.80758