public class Integral { // Variables are defined double a , b , c , FirstValue ,
ID: 3540161 • Letter: P
Question
public class Integral {
// Variables are defined
double a,b,c,FirstValue,SecondValue,ThirdValue,X, HigherValue,LowerValue,ActualValue,LowValue,HighValue, Sum;
String d;
boolean SecondOrder;
boolean TrigFunc;
public Integral(double a,double b,double c,String d) {
a = FirstValue;
b = SecondValue;
c = ThirdValue;
//Determines whether the string objects contain the same data
if (d.equalsIgnoreCase("poly2"))
{
SecondOrder = true;
} else if(d.equalsIgnoreCase("cos")){
TrigFunc = true;
} else {
System.out.println("Error!,You should enter 3 doubles (poly2 or cos)");
// exits the program if input is wrong
System.exit(0);
}
}
public Integral(double a, double b, double c) {
a = FirstValue;
b = SecondValue;
c = ThirdValue;
SecondOrder = true;
}
public void Integrate(double Lower, double Higher, int Bin){
Lower = LowValue;
Higher = HighValue;
//Here is where the polynomial instructions are
if (SecondOrder = true) {
// 1.Need to find BinSize
double BinSize = ((HighValue - LowValue)/Bin);
// 2.Need to find polynomial
double Poly = (FirstValue*Math.pow(X, 2)+ SecondValue*X+ThirdValue);
// 3.Put the first X value as the lower X value
X = (LowValue);
// 4.Two lines of code below signify actual integration used
HigherValue = ((FirstValue*Math.pow(HighValue,3))/3)+((SecondValue*Math.pow(HighValue,2))/2)+ThirdValue*HighValue;
LowerValue = ((FirstValue*Math.pow(LowValue,3))/3)+((SecondValue*Math.pow(LowValue,2))/2)+ThirdValue*LowValue;
// 5.Actual Integration = Highest Value - Lowest value
ActualValue = HigherValue - LowerValue;
while(X < HighValue) {
Sum = (Sum + BinSize*Poly);
// Here X is adding a BinSize in the while loop
// This happens until X is larger then the higher value
X = (X + BinSize);
}
//Here is where the cosine instructions are
} else if (TrigFunc = true) {
double BinSize = ((HighValue - LowValue)/Bin);
// 1.Use of Trigonmetric function
double Trig = (FirstValue*Math.cos(SecondValue*X+ThirdValue));
// 2.Put the first X value as the lower X value
X = LowValue;
// 3.Two lines of code below signify actual integration used
HigherValue = (FirstValue*Math.cos(SecondValue*HighValue+ThirdValue))/SecondValue;
LowerValue = (FirstValue*Math.cos(SecondValue*LowValue+ThirdValue))/SecondValue;
// 4.Actual Integration = Highest Value - Lowest value
ActualValue = HigherValue - LowerValue;
while (X<HighValue) {
// Here you add the cumulative area of the rectangles
Sum = (Sum + BinSize*Trig);
// X has a BinSize added to it every time it wants to repeat
X = (X + BinSize);
}
} else {
System.out.println("What's the integral bro?");
}
}
// Here the sum of est. integration is returned
public double getEstimate() {
return(Sum);
}
//Finally the sum of actual integration is returned
public double getActual() {
return(ActualValue);
}
}
///////////////////////////////////////////////////////////////////////////////
public class IntegralTester {
public static void main(String[] args) {
Integral input = new Integral(5,-8,7);
input.Integrate(2, 14, 50);
System.out.println("Estimated Value: "+input.getEstimate());
System.out.println("Actual Value: "+input.getActual());
System.out.println("HigherValue for Actual Value: "+input.HigherValue);
System.out.println("LowerValue for Actual Value: "+input.LowerValue);
System.out.println("X Value: "+input.X);
}
}
HigherValue for Actual Value: 0.0
LowerValue for Actual Value: 0.0
X Value: 0.0
Explanation / Answer
you did'nt assign properly your class variable.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.