2

Help? I don't know why I am getting this error. I am getting at in line 39:

term[1] = differentiate(Coeff[1], exponent[1]);

How can I fix this issue?

Full code listing:

public class Calcprog {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int numTerms = 7;
        double[] Coeff = new double[6];
        double[] exponent = new double[6];
        String[] term = new String[6];

        System.out.println("Enter the number of terms in your polynomial:");
        numTerms = input.nextInt();

        while (numTerms > 6) {
            if (numTerms > 6) {
                System.out.println("Please limit the number of terms to six.");
                System.out.println("Enter the number of terms in your polynomial:");
                numTerms = input.nextInt();
            }
        }

        for (int i = 1; i < numTerms + 1; i++) {
            System.out.println("Please enter the coefficient of term #" + i + " in decimal form:");
            Coeff[i] = input.nextDouble();
            System.out.println("Please enter the exponent of term #" + i + " in decimal form:");
            exponent[i] = input.nextDouble();
        }
        term[1] = differentiate(Coeff[1], exponent[1]);
    }

    public String differentiate(int co, int exp) {
        double newco, newexp;
        String derivative;
        newexp = exp - 1;
        newco = co * exp;
        derivative = Double.toString(newco) + "x" + Double.toString(newexp);
        return derivative;
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
Sublīmis
  • 21
  • 1
  • 1
  • 3

3 Answers3

4

You are trying to pass double arguments to a method that accepts ints, which requires a casting that may result in loss of information.

You can make it work by an explicit cast :

term[1] = differentiate((int)Coeff[1], (int)exponent[1]);

Or you can change your differentiate method to accept double arguments, which would probably make more sense :

public String differentiate(double co, double exp)
Eran
  • 387,369
  • 54
  • 702
  • 768
1

your method is not static, and you calling in the main which is static, remember a non static method can be access direct in a static method, you have to create an instance of the class to access that method, and also the parameter you are passing is double and not int. Your method should be like that public static String differentiate(double co, double exp){

Joe Doe
  • 141
  • 1
  • 3
0

change the argument types of the differentiate method to double. This should then look as follows

  public String differentiate(double co, double exp){
    ...
  }
René Winkler
  • 6,508
  • 7
  • 42
  • 69