Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. /*
  2. Separate Classes must be in the same directory as main method or must invoke
  3. classpath
  4. */
  5. public class Charge {
  6.  
  7. //first declare instance variables which are usually private
  8. private final double rx;
  9. private final double ry;
  10. private final double q;
  11.  
  12.  
  13. /* A class contains constructors that are invoked to create objects from a
  14. class blueprint. Constructor declarations look like method declarations
  15. -except that they use the name of the class and have no return type.
  16. Constructor must be capitalized. Classes and Constructors are capitalized
  17. methods are not.
  18. */
  19.  
  20. //Constructor
  21. public Charge(double x0, double y0, double q0) {
  22.  
  23. rx = x0;
  24. ry = y0;
  25. q = q0;
  26.  
  27. }
  28.  
  29. /*
  30. The method to compute electrical potential which is defined by the equation
  31. V = kq/r
  32. */
  33. public double potentialAt(double x, double y){
  34.  
  35. double k = 8.99e09; //Electrostatic Constant that k=8.99 X 10^9 Nm^2/C^2 (N = Newtons, m = meters, C = Coloumbs)
  36.  
  37. //r = delta x - delta y
  38. double dx = x - rx; //delta x for distance
  39. double dy = y - ry; //delta y for distance
  40. return k*q/Math.sqrt(dx*dx + dy*dy);//computation using distance formula
  41.  
  42. }
  43.  
  44. }
  45.  
  46. Charge(double x0, double y0, double q0)
  47.  
  48. double potentialAt(double x, double y)
  49. String toSting()
  50.  
  51. 1. Create an object
  52.  
  53. ClassName object = new ClassName (invoke Constructor)
  54. --------- ------ --- --------- -----------------
  55. Charge C = new Charge (2.2, 3.4, 7.2)
  56.  
  57. 2. Use instance methods on object
  58.  
  59. C.potentialAt(2.3, 4.2)
  60.  
  61. import java.util.*;
  62.  
  63.  
  64. public class ChargeClient {
  65.  
  66. public static void main(String[] args) {
  67.  
  68.  
  69. //Using a scanner object to get values
  70. System.out.println("Please enter an X Value");
  71. Scanner in = new Scanner(System.in);
  72. // get values from command line for x and y
  73. double x = in.nextDouble();
  74.  
  75. System.out.println("Please enter a Y Value");
  76. double y = in.nextDouble();
  77.  
  78. /*
  79. 1. Instantiate objects C1 and C2
  80.  
  81. ClassName object = new ClassName (invoke Constructor)
  82. --------- ------ --- --------- -----------------
  83. Charge C = new Charge (2.2, 3.4, 7.2)
  84.  
  85. 2. we are invoking constructor from API
  86.  
  87. Charge(double x0, double y0, double q0)
  88. */
  89.  
  90. Charge c1 = new Charge(.51, .63, 21.3);
  91. Charge c2 = new Charge(.13, .94, 81.9);
  92.  
  93. //print out charge so we know what we are dealing with
  94. System.out.println(c1);
  95. System.out.println(c2);
  96.  
  97. /*
  98. Here we create variables to hold the return from our potential method
  99. which is enacted on our C1 and C2 objects.
  100.  
  101. 1. We call a method on an object by:
  102.  
  103. objectName.methodName(appropriate parameters)
  104.  
  105. */
  106. double v1 = c1.potentialAt(x, y);
  107. double v2 = c2.potentialAt(x, y);
  108.  
  109. //Concatenate results and print them out.
  110. System.out.println(v1+v2);
  111.  
  112. System.out.println("this is the printf statement");
  113. System.out.printf("%.2En", v1+v2);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement