Advertisement
Guest User

Lab 1 Som

a guest
Sep 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. /**
  2. * A Resistor models an ideal resistor that obeys Ohm's Law.
  3. *
  4. * @author Soumya Chaudhari
  5. */
  6. package coe318.lab1;
  7.  
  8. public class Resistor {
  9. //Instance (state) variables
  10. //TODO Add instance variables (Hint: you only need 2!)
  11. private double r;// r is for resistance
  12.  
  13.  
  14. /**
  15. * Create an ideal Resistor. The initial current through and voltage across
  16. * the Resistor are zero.
  17. *
  18. * @param resistance resistance in Ohms
  19. */
  20. public Resistor(double resistance) {
  21. //Set values of state variables
  22. r = resistance;
  23. }
  24.  
  25. /**
  26. * Returns the value of the resistor in Ohms.
  27. *
  28. * @return the resistance
  29. */
  30. public double getResistance() {
  31. //FIX THIS so that it returns the actual resistance
  32. return r;
  33. }
  34.  
  35. /**
  36. * Returns the voltage across the resistor.
  37. *
  38. * @return the voltage
  39. */
  40. public double getVoltage() {
  41. //FIX THIS so that it returns the actual voltage
  42. return 0.0;
  43. }
  44.  
  45. /**
  46. * Sets the value of the voltage across the resistor.
  47. *
  48. * @param voltage the voltage to set
  49. */
  50. public void setVoltage(double voltage) {
  51. //FIX THIS
  52. }
  53.  
  54. /**
  55. * Returns the current through the Resistor.
  56. *
  57. * @return the current
  58. */
  59. public double getCurrent() {
  60. //FIX THIS
  61. return 0.0;
  62. }
  63.  
  64. /**
  65. * Sets the value of the current through the resistor.
  66. *
  67. * @param current the current to set
  68. */
  69. public void setCurrent(double current) {
  70. //FIX THIS
  71. }
  72.  
  73. /**
  74. * Returns the power (in Watts) dissipated by the Resistor.
  75. *
  76. * @return the power
  77. */
  78. public double getPower() {
  79. //FIX THIS
  80. return 0.0;
  81. }
  82.  
  83. /**
  84. * A simple example of using a Resistor. <p> The output should be:
  85. * <pre>
  86. * Creating a 50 Ohm resistor (r1)
  87. * Its resistance is 50.0 Ohms
  88. * Its current is 0.0 Amps
  89. * Its voltage is 0.0 Volts
  90. * Its power is 0.0 Watts
  91. * Creating a 100 Ohm resistor (r2)
  92. * Its resistance is 100.0 Ohms
  93. * Setting r1's current to 2 Amps
  94. * Its current is 2.0 Amps
  95. * Its voltage is 100.0 Volts
  96. * Its power is 200.0 Watts
  97. * Setting r1's voltage to 50 Volts
  98. * Its current is 1.0 Amps
  99. * Setting r2's current to 3 Amps
  100. * Its voltage is 300.0 Volts
  101. * </pre>
  102. *
  103. * @param args (Command line arguments not used.)
  104. */
  105. public static void main(String[] args) {
  106. Resistor r1 = new Resistor();
  107. Resistor r2 = new Resistor();
  108.  
  109. System.out.println("Creating a 50 Ohm resistor (r1)");
  110. r1 = new Resistor(50.0);
  111. System.out.println("Its resistance is "
  112. + r1.getResistance() + " Ohms");
  113. System.out.println("Its current is "
  114. + r1.getCurrent() + " Amps");
  115. System.out.println("Its voltage is "
  116. + r1.getVoltage() + " Volts");
  117. System.out.println("Its power is "
  118. + r1.getPower() + " Watts");
  119. System.out.println("Creating a 100 Ohm resistor (r2)");
  120. r2 = new Resistor(100.0);
  121. System.out.println("Its resistance is "
  122. + r2.getResistance() + " Ohms");
  123. System.out.println("Setting r1's current to 2 Amps");
  124. r1.setCurrent(2.0);
  125. System.out.println("Its current is "
  126. + r1.getCurrent() + " Amps");
  127. System.out.println("Its voltage is "
  128. + r1.getVoltage() + " Volts");
  129. System.out.println("Its power is "
  130. + r1.getPower() + " Watts");
  131. System.out.println("Setting r1's voltage to 50 Volts");
  132. r1.setVoltage(50.0);
  133. System.out.println("Its current is "
  134. + r1.getCurrent() + " Amps");
  135. System.out.println("Setting r2's current to 3 Amps");
  136. r2.setCurrent(3.0);
  137. System.out.println("Its voltage is "
  138. + r2.getVoltage() + " Volts");
  139.  
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement