-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resistor.java
98 lines (85 loc) · 2.33 KB
/
Resistor.java
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
// Group [J Team]: Joe Nathan Abellard, Joel Lapompe, and Joel Singleton
// CET 3640 - [Class Session]
// Final Project
public class Resistor
{
private double resistance; // the resistance of the resistor
private double current; // the current across the resistor
private double voltage; // the voltage drop across the resistor
private double conductance; // the conductance of the resistor [1 / resistance]
private double power; // the power delivered to the resistor
// default constructor
Resistor()
{
this.resistance = 1.0; // initial value of resistor is 1 ohm
this.conductance = 1.0 / this.resistance;
} // end default constructor
Resistor(double resistance)
{
if (resistance <= 0)
{
this.resistance = 1.0;
}
else
{
this.resistance = resistance;
}
this.conductance = 1.0 / this.resistance;
}
// accessor methods
double getResistance()
{
return resistance;
} // end method getResistance
double getConductance()
{
updateQuantities();
return conductance;
} // end method getConductance
double getCurrent()
{
return current;
} // end method getCurrent
double getVoltage()
{
updateQuantities();
return voltage;
} // end method getVoltage
double getPower()
{
updateQuantities();
return power;
} // end method getPower
// mutator methods
void setCurrent(double current)
{
this.current = current;
} // end method setCurrent
void updateQuantities()
{
conductance = 1.0 / resistance;
voltage = current * resistance; // set the voltage
power = current * voltage; // the the power
} // end method updateQuantities
public void setResitance(double resistance)
{
if (resistance <= 0)
{
System.out.println("WARNING: Resistance value must be greater than 0; setting value to 1.0 ohms");
this.resistance = 1.0;
}
else
{
this.resistance = resistance;
}
} // end method setResistance
void displayQuantities()
{
System.out.printf("Resistance = %.5f ohms%n", getResistance());
System.out.printf("Conductance = %.5f ohms^-1%n", getConductance());
System.out.printf("Current = %.5f amperes%n",getCurrent());
System.out.printf("Voltage = %.5f volts%n", getVoltage());
System.out.printf("Power = %.5f watts%n", getPower());
System.out.printf("----------------------------------------------" + "%n");
} // end method displayQuantities
} // end class Resistor