-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquation.java
More file actions
210 lines (195 loc) · 6.25 KB
/
Copy pathEquation.java
File metadata and controls
210 lines (195 loc) · 6.25 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.ArrayList;
public class Equation
{
private String baseEq;
private ArrayList<String> eqParts;
private ArrayList<String> eqForms;
private ArrayList<String> compForm = new ArrayList<String>(0);;
public Equation(String form)
{
baseEq = form;
eqParts = splitEquation(form);
eqForms = new ArrayList<String>();
eqForms.add(form);
}
public Equation(ArrayList<String> forms)
{
baseEq = forms.get(0);
eqParts = splitEquation(baseEq);
eqForms = forms;
}
public String getEq()
{
return baseEq;
}
public String getEqNoEquals()
{
return baseEq.split("=")[1];
}
public ArrayList<String> getEqParts()
{
return eqParts;
}
public ArrayList<String> getCompForms()
{
return compForm;
}
/**
* This method determines whether or not he base equation needs to do component method or not
*
* @param Figure f A figure is need so that it can be determined, based off figure's variables, whether component method shall be use
* @return if component method needs to be used
*/
public boolean isVector(Figure f)
{
if(eqParts.contains("dot") || eqParts.contains("crs"))
return false;
for(String var: getRequiredVars())
{
if(f.getVar(var) != null && f.getVar(var).getAngle() != null)
{
updateComponentForms(f);
return true;
}
}
return false;
}
/**
* Switches the baseEq so that other methods can use differnt versions of the same equation
*
* @param String nameOfVar the name of the variable (in the eqation) used to determine what form an equation will be in
*/
public void switchBase(String nameOfVar)
{
for(String eq: eqForms)
{
if(eq.indexOf(nameOfVar) < eq.indexOf("="))
{
baseEq = eq;
eqParts = splitEquation(baseEq);
}
}
}
/**
* Gives a list of variable names need to solve an equation. If the equation has any constants listed as NUMBERS,
* they will not be returned. (i.e. "Force=Mass-3" will return ["Force","Mass"])
*
* @return An arraylist of strings that correspond to the names of variables needed
*/
public ArrayList<String> getRequiredVars()
{
ArrayList<String> varParts = new ArrayList<String>();
for(int pos = 1; pos < eqParts.size(); pos++)
{
if(eqParts.get(pos).equals("["))
{
varParts.remove(varParts.size()-1);
}
else if(!(eqParts.get(pos).length() == 1 && !Character.isLetter(eqParts.get(pos).charAt(0))))
{
varParts.add(eqParts.get(pos));
}
}
return varParts;
}
/**
* Allows the user to check if this equation can be used to solve for a particular variable
*
* @param name - the name of the variable the user wants to check against
* @return returns whether the equation can be used to solve for the variable the user wants
*/
public boolean hasVariable(String name)
{
for(String partOf: eqParts)
{
if(name.equals(partOf))
return true;
}
return false;
}
/**
* Splits the string passed in by variables and symbols
*
* @return A String arraylist of the parts of the equation (symbols & parentheses included)
*/
public static ArrayList<String> splitEquation(String eq2)
{
String tmpVal = "";
ArrayList<String> tmpArray = new ArrayList<String>();
for(int i = 0; i < eq2.length(); i++)
{
//case for really small numbers (e.g., 1*E-7)
if(i < eq2.length()-1 && eq2.charAt(i) == 'E' && eq2.charAt(i+1) =='-')
{
tmpVal += eq2.charAt(i)+"";
tmpVal += eq2.charAt(i+1)+"";
i++;
}
else if(Character.isLetterOrDigit(eq2.charAt(i)) || eq2.charAt(i) == '.' || eq2.charAt(i) == ' ')
tmpVal += eq2.charAt(i)+"";
//case for negative numbers
else if(eq2.charAt(i) == '-' && tmpVal.equals(""))
tmpVal += eq2.charAt(i);
else if(!tmpVal.equals(""))
{
tmpArray.add(tmpVal);
tmpVal = "";
tmpArray.add(eq2.charAt(i)+"");
}
else
tmpArray.add(eq2.charAt(i)+"");
}
if(!tmpVal.equals(""))
tmpArray.add(tmpVal);
return tmpArray;
}
/**
*
* Splits the current form of the equation by variables
*
* @return A String arraylist of the parts of the current equation (symbols & parentheses included)
*/
public ArrayList<String> splitEquation()
{
return splitEquation(baseEq);
}
public int countUnknowns(Figure f)
{
int unknown = 0;
for(String v: getRequiredVars())
{
//If a variable isnt current in a figure, (e.g. a term with a the "intial" or "final" tag on it), add 1 to unknowns
if(f!= null && f.getVar(v)==null)
unknown++;
else if(f!=null&&!Variable.canBeDouble(f.getVar(v).getValue().toString()))
unknown++;
}
return unknown;
}
public void updateComponentForms(Figure f)
{
ArrayList<String> parts = new ArrayList<String>(2);
String[] dire = {"sin","cos"};
for(String d: dire)
{
String tmp = "";
String angle = "90.0";
if(d.equals("cos"))
angle = "0.0";
ArrayList<String> req = getRequiredVars();
for(String part: splitEquation())
{
if(req.contains(part) && f.getVar(part).getAngle()!=null)
{
tmp = tmp+"("+part+")*"+d+"["+f.getVar(part).getAngle()+"]";
}
else if(req.contains(part))
tmp = tmp+"("+part+")*"+d+"["+angle+"]";
else
tmp = tmp+part;
}
parts.add(tmp);
}
compForm = parts;
}
}