Repository navigation
Expand file tree
/
Copy pathJavaVectorDemo.java
More file actions
125 lines (95 loc) · 3.25 KB
/
Copy pathJavaVectorDemo.java
File metadata and controls
125 lines (95 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.Vector;
// Custom Exception
class VectorException extends Exception {
public VectorException(String msg) {
super(msg);
}
}
// 3D Vector Class
class Vector3D {
private double x, y, z;
// Default Constructor
public Vector3D() {
x = y = z = 0;
}
// Parameterized Constructor
public Vector3D(double x, double y, double z) throws VectorException {
if (Double.isNaN(x) || Double.isNaN(y) || Double.isNaN(z))
throw new VectorException("Vector components cannot be NaN");
this.x = x;
this.y = y;
this.z = z;
}
// Addition
public Vector3D add(Vector3D v) throws VectorException {
if (v == null)
throw new VectorException("Null vector cannot be added");
return new Vector3D(
this.x + v.x,
this.y + v.y,
this.z + v.z
);
}
// Subtraction
public Vector3D subtract(Vector3D v) throws VectorException {
if (v == null)
throw new VectorException("Null vector cannot be subtracted");
return new Vector3D(
this.x - v.x,
this.y - v.y,
this.z - v.z
);
}
// Dot Product
public double dotProduct(Vector3D v) throws VectorException {
if (v == null)
throw new VectorException("Null vector for dot product");
return (this.x * v.x) +
(this.y * v.y) +
(this.z * v.z);
}
// Display
public void display() {
System.out.println("(" + x + ", " + y + ", " + z + ")");
}
}
// Main Class
public class JavaVectorDemo {
public static void main(String[] args) {
// Java Vector Collection
Vector<Vector3D> vectorList = new Vector<>();
try {
// Creating 3D vectors
Vector3D v1 = new Vector3D(2, 3, 4);
Vector3D v2 = new Vector3D(1, 5, 2);
Vector3D v3 = new Vector3D(7, 1, 6);
// Adding to Java Vector collection
vectorList.add(v1);
vectorList.add(v2);
vectorList.add(v3);
System.out.println("Stored 3D Vectors:");
for (Vector3D v : vectorList) {
v.display();
}
// Operations using elements from Java Vector
System.out.println("\nOperations on Vectors:");
Vector3D add = vectorList.get(0).add(vectorList.get(1));
System.out.print("Addition: ");
add.display();
Vector3D sub = vectorList.get(0).subtract(vectorList.get(1));
System.out.print("Subtraction: ");
sub.display();
double dot = vectorList.get(0).dotProduct(vectorList.get(1));
System.out.println("Dot Product: " + dot);
// Exception Demo
Vector3D nullVector = null;
vectorList.get(0).add(nullVector);
}
catch (VectorException e) {
System.out.println("Vector Exception: " + e.getMessage());
}
catch (Exception e) {
System.out.println("General Exception: " + e);
}
}
}