Object Oriented Design Revisited
/*************************************************************************
* Compilation: javac Charge.java
* Execution: java Charge x y
*
*************************************************************************/
public class Charge {
private double rx, ry; // position
private double q; // charge
public Charge(double x0, double y0, double q0) {
rx = x0;
ry = y0;
q = q0;
}
public double potentialAt(double x, double y) {
double k = 8.99e09;
double dx = x - rx;
double dy = y - ry;
return k * q / Math.sqrt(dx*dx + dy*dy);
}
public String toString() {
return q + " at " + "(" + rx + ", " + ry + ")";
}
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
Charge c1 = new Charge(.51, .63, 21.3);
Charge c2 = new Charge(.13, .94, 81.9);
System.out.println(c1);
System.out.println(c2);
double v1 = c1.potentialAt(x, y);
double v2 = c2.potentialAt(x, y);
StdOut.println(v1+v2);
}
}
Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:07:43 EST 2011.
Basic elements of a data type.
- API.
- Class.
- Access modifiers.
- Instance variables.
- Constructors.
- Instance methods.
- Variable names in methods.
Basic components that you need to understand to be able to build data types in Java.
NOTE:
You should be working on the programming assignment and gradient assignments before you start working on OOD programs.

