Advertisement
zeneksilant

Vector Class

Jan 18th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. public class Application {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. Vector a = new Vector(13, 25, 4);
  6. Vector b = new Vector(43, -7, 11);
  7.  
  8. ViewResult.showScalarResult(a.scalarProduct(b));
  9. ViewResult.showVectorResult(a.vectorProduct(b));
  10. }
  11. }
  12.  
  13. class ViewResult{
  14. public static void showVectorResult(double[] result) {
  15. System.out.print("Векторное произведение векторов:[a,b]=<");
  16. for (int i = 0; i < result.length; i++) {
  17. System.out.print(result[i] + (i != 2 ? ";" : ">\n"));
  18. }
  19. }
  20.  
  21. public static void showScalarResult(double d) {
  22. System.out.println("Скалярное произведение векторов: " + d);
  23. }
  24. }
  25.  
  26. class Vector {
  27. private double[] array = new double[3];
  28.  
  29. public Vector(double... x) {
  30. for (int i = 0; i < array.length; i++) {
  31. array[i] = x[i];
  32. }
  33. }
  34.  
  35. public double[] vectorProduct(Vector vector) {
  36. double[] result = new double[3];
  37. for (int i = 0; i < 3; i++) {
  38. result[i] = this.array[(i + 1) % 3] * vector.array[(i + 2) % 3] - this.array[(i + 2) % 3] * vector.array[(i + 1) % 3];
  39. }
  40. return result;
  41. }
  42.  
  43. public double scalarProduct(Vector vector) {
  44. double result = 0;
  45. for (int i = 0; i < 3; i++) {
  46. result += this.array[i] * vector.array[i];
  47. }
  48. return result;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement