awdreg awdreg - 6 months ago 22
Java Question

Java - A value resets to 0 unexpectedly

I'm making a derivative calculator that asks the user for the degree of their polynomial and then the coefficients for each term, partly because I'm an inexperienced programmer and can't parse input like

3x^4/4 + sin(x)
.

Here's my class.

package beta;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class DerivativeCalculator
{
public DerivativeCalculator(String d, String v)
{
int degree = Integer.parseInt(d);

double value = Double.parseDouble(v);

coeffList = new ArrayList<Double>();
for (int i = 0; i <= degree; i++)
{
String console = JOptionPane.showInputDialog("Enter the coefficient of the "
+ "x^" + i + " term.");
Double coeff = Double.parseDouble(console);

coeffList.add(coeff);
}

}
public double calc()
{
double dx = 0.0001;

double x1 = value;
double y1 = 0;
for (int d = degree; d >= 0; d--)
{
y1 += coeffList.get(d) * Math.pow(x1, d);
}

double x2 = x1 + dx;
double y2 = 0;
for (int d = degree; d >= 0; d--)
{
y2 += coeffList.get(d) * Math.pow(x2, d);
}

double slope = (y2 - y1)/ (x2 - x1);

DecimalFormat round = new DecimalFormat("##.##");
round.setRoundingMode(RoundingMode.DOWN);

return Double.valueOf(round.format(slope));
}

public String getEquation()
{
String equation = "";
for (int d = degree; d >= 0; d--)
{
equation = equation + String.valueOf(coeffList.get(d)) + "x^" + String.valueOf(d) + " + ";
}
return equation;
}

public String getValue()
{
return String.valueOf(value);
}

private int degree;
private double value;
private List<Double> coeffList;

}


Now here's my test class.

package beta;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class DerivativeCalculatorTest extends JApplet
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

String d = JOptionPane.showInputDialog("Enter the degree of your polynomial: ");
String v = JOptionPane.showInputDialog("Enter the x value "
+ "at which you want to take the derivative");

DerivativeCalculator myDerivCalc = new DerivativeCalculator(d, v);
g2.drawString(String.valueOf(myDerivCalc.calc()), 10, 100);
g2.drawString(myDerivCalc.getEquation(), 10, 40);
g2.drawString(myDerivCalc.getValue(), 10, 70);
}

}


Running this creates an applet window that displays

5.0x^0+
0.0
0.0


which is not the correct derivative.

I debugged my program and everything runs as expected until the view switches to my test class and it executes
g2.drawString(String.valueOf(myDerivCalc.calc()), 10, 100);


After it executes this line,
degree
(the degree of the polynomial) resets to 0 even though the user entered 5. This then messes up all the for loops in my class.

Why does this happen? Any suggestions on fixing this? Thanks

Answer

You redefine degree and value as local variables inside your constructor. They shadow your class variables with the same name.

Do not re-declare them.

Instead of

  int degree = <something>;
  double value = <something>;

you need

  degree = <something>;
  value = <something>;
Comments