-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharge.java
More file actions
33 lines (28 loc) · 944 Bytes
/
Copy pathCharge.java
File metadata and controls
33 lines (28 loc) · 944 Bytes
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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class Charge {
private final double rx, ry, q;
public Charge(double x0, double y0, double q0) {
rx = x0;
ry = y0;
q = q0;
}
public double potentialAt(double x, double y) {
double dx = rx - x;
double dy = ry - y;
double r = Math.sqrt(dx * dx + dy * dy);
double k = 8.99e09;
return k * q / r;
}
public String toString() {
return q + " at " + "(" + rx + ", " + ry + ")";
}
public static void main(String[] args) {
Charge c = new Charge(0.5, 0.5, 20.5);
System.out.println(c);
System.out.printf("%.2e\n", c.potentialAt(0.1, 0.98));
}
}