-
Notifications
You must be signed in to change notification settings - Fork 0
/
LadderCircuit.java
164 lines (139 loc) · 4.93 KB
/
LadderCircuit.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Group [J Team]: Joe Nathan Abellard, Joel Lapompe, and Joel Singleton
// CET 3640 - [Class Session]
// Final Project
import java.util.Scanner;
public class LadderCircuit
{
private double sourceVoltage;
private double sourceCurrent;
private double [] totalResistance;
private Resistor [] resistors; // array of Resistor objects
// constructors
LadderCircuit()
{
sourceVoltage = 1.0; // sourceVoltage = 1.0 Volts
resistors = new Resistor[4]; // build a circuit consisting of 4 resistors
// initialize resistor values
for (int i = 0; i < resistors.length; i++)
{
resistors[i] = new Resistor();
} // end for
totalResistance = new double[3];
}
LadderCircuit(double sourceVoltage, int numbOfResistors)
{
if (sourceVoltage <= 0)
{
System.out.println("WARNING: Source Voltage must be greater than 0.0V; setting SourceVoltage to 1.0V");
sourceVoltage = 1.0; // sourceVoltage = 1.0 Volts
} // end if
else
{
this.sourceVoltage = sourceVoltage;
} // end else
if (numbOfResistors < 4 || (numbOfResistors > 4 && numbOfResistors % 2 != 0))
{
System.out.println("WARNING: Number of resistors must be >= 4 and even; setting numbOfResitors to 4");
resistors = new Resistor[4]; // build a circuit consisting of 4 resistors
// initializing resistor values
for (int i = 0; i < resistors.length; i++)
{
resistors[i] = new Resistor();
} // end for
totalResistance = new double[3];
} // end if
else
{
resistors = new Resistor[numbOfResistors];
// initializing resistor values
for (int i = 0; i < resistors.length; i++)
{
resistors[i] = new Resistor();
} // end for
totalResistance = new double[numbOfResistors - 1];
}
}
// accessor methods
double getSourceVoltage()
{
return sourceVoltage;
} // end method getSourceVoltage
double getSourceCurrent()
{
return sourceCurrent;
} // end method getSourceCurrent
public void setResitorValues()
{
Scanner input = new Scanner(System.in);
double recievedResistance;
for (int i = 0; i < resistors.length; i++)
{
System.out.println("Enter the resistance (in ohms) for resitor # " + (i + 1) + ": ");
recievedResistance = input.nextDouble();
resistors[i].setResitance(recievedResistance); // set the resistance
} // end for
//r1.setResitance(15.5);
//System.out.println(r1.getResistance());
} // end method setResitorValues
void calculateTotalResitance()
{
totalResistance[0]= resistors[resistors.length - 1].getResistance()
+ resistors[resistors.length - 2].getResistance();
for (int i = 1; i < resistors.length - 1; i++)
{
if (i % 2 != 0)
{
totalResistance[i] = (totalResistance[i - 1] * resistors[resistors.length - (i + 2)].getResistance()) /
( totalResistance[i - 1] + resistors[resistors.length - (i + 2)].getResistance() );
} // end if
else
{
totalResistance[i] = totalResistance[i - 1] + resistors[resistors.length - (i + 2)].getResistance();
} // end else
} // end for
totalResistance[totalResistance.length - 1] = resistors[0].getResistance() +
totalResistance[totalResistance.length - 2]; // the final total resistance (RT)
} // end method calculateTotalResitance
void calculateBranchCurrents()
{
// calculate the source current
setSourceCurrent();
resistors[0].setCurrent(getSourceCurrent());
for (int i = 1; i < resistors.length - 1; i++)
{
if (i % 2 != 0)
{
double oddBranchCurrent = (resistors[i - 1].getCurrent() * totalResistance[resistors.length - (i + 3)]) /
(totalResistance[resistors.length - (i + 3)] + resistors[i].getResistance());
resistors[i].setCurrent(oddBranchCurrent);
} // end if
else
{
double evenBranchCurrent = (resistors[i - 2].getCurrent() * resistors[i - 1].getResistance()) /
(totalResistance[resistors.length - (i + 2)] + resistors[i - 1].getResistance());
resistors[i].setCurrent(evenBranchCurrent);
} // end else
resistors[resistors.length - 1].setCurrent(resistors[resistors.length - 2].getCurrent());
} // end for
} // end method calculateBranchCurrents
double getTotalResistance()
{
return totalResistance[totalResistance.length - 1];
} // end method getTotalResistance
void setSourceCurrent()
{
sourceCurrent = sourceVoltage / totalResistance[totalResistance.length - 1];
} // end method setSourceCurrent
void finalOutput()
{
System.out.printf("The Source voltage of the ladder circuit is %.5f volts%n", sourceVoltage);
System.out.printf("The Total Resistance of the ladder circuit is %.5f ohms%n", getTotalResistance());
System.out.printf("The Source current of the ladder circuit is %.5f amperes%n", sourceCurrent);
System.out.println(" Here are the values of Conductance, Current, Voltage, and Power across each Resistor:");
for (int i = 0; i < resistors.length; i++)
{
System.out.println("Resistor # " + (i + 1));
resistors[i].displayQuantities(); // print everything
} // end for
} // end method finalOutput
} // end class LadderCircuit