Dragos Mihai Marcean
Dragos Mihai Marcean

Reputation: 65

Override method in Java

I am trying to execute a method from the class "Operatii". The method is written in class "PolinomIntreg". The problem is that when executed, the program goes to the overrided method where there is no code and not the one where is code.

What can I do so the program goes to the one that has code?

PolinomIntreg class:

import java.util.Scanner;


public class PolinomIntreg<INTEGER> implements IPolinom<INTEGER> {

int grad1, grad2, x, i;
double val = 0;

int[] pol1 = new int[100];
int[] pol2 = new int[100];
int[] pol3 = new int[200];

public void grad(int grad1, int grad2){

    System.out.println("Introduceti gradele celor doua polinoame: ");
    Scanner in = new Scanner(System.in);
    this.grad1 = in.nextInt();
    this.grad2 = in.nextInt();


}


public void coef(int pol1[], int pol2[], int grad1, int grad2){

    System.out.println("Introduceti coeficientii reali ai primului polinom: ");

    for(i = 0; i < this.grad1; i++)
    {
        Scanner in = new Scanner(System.in);
        pol1[i] = in.nextInt();
    }

    System.out.println("Introduceti coeificientii reali ai celui de al doilea polinom: ");

    for(i = 0; i < this.grad2; i++)
    {
        Scanner in = new Scanner(System.in);
        pol2[i] = in.nextInt();
    }
}

public void add(int pol1[], int pol2[], int grad1, int grad2){

    if(this.grad1 > this.grad2)
    {
        for(i = 0; i < this.grad1 ; i++)
            pol1[i] = pol2[i] + pol1[i];
    }
    else
        for(i = 0; i < this.grad2 ; i++)
            pol2[i] = pol2[i] + pol1[i];
}

public void substract(int pol1[], int pol2[], int grad1, int grad2){

    if(this.grad1 > this.grad2)
    {
        for(i = 0; i < this.grad1 ; i++)
            pol1[i] = pol2[i] - pol1[i];
        afisare(pol1,this.grad1);
    }
    else
        for(i = 0; i < this.grad2 ; i++)
            pol2[i] = pol2[i] - pol1[i];
        afisare(pol2,this.grad2);

}

public void value(int pol1[], int grad1){


    System.out.println("Introduceti o valoare pentru x: ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();

    for( i = 0; i < this.grad1; i++)
        val = val + pol1[i] * Math.pow(x,i);

    System.out.println("Valoarea polinomului in punctul" + x + " este " + val);
}

public void afisare(int pol1[], int grad1){

    for(i = grad1 - 1; i >= 0; i--)
        System.out.println(pol1[i] + "^" + i + "+ ");
}


public void multiply(int pol1[], int pol2[], int grad1, int grad2){

    int j = 0;

    for ( i = 0; i < this.grad1 ; i++)
        for (j = 0; j < this.grad2 ; j++)
        {
            pol3[i+j] = pol3[i+j] + pol1[i] + pol2[j]; 
        }
}


@Override
public void add(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1, INTEGER grad2) {
    // TODO Auto-generated method stub

}

@Override
public void value(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}


@Override
public void multiply(INTEGER[] pol1, INTEGER[] pol2, INTEGER[] pol3,
        INTEGER grad1, INTEGER grad2, INTEGER grad3) {
    // TODO Auto-generated method stub

}


@Override
public void grad(INTEGER grad1, INTEGER grad2) {


}


@Override
public void coef(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1,
        INTEGER grad2) {
    // TODO Auto-generated method stub

}


@Override
public void afisare(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}


@Override
public void substract(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1,
        INTEGER grad2) {
    // TODO Auto-generated method stub

}

}   

Operatii class:

import java.util.Scanner;


public class Operatii {

static Integer[] pol1 = new Integer[100];
static Integer[] pol2 = new Integer[100];
static Integer[] pol3 = new Integer[200];
static public Integer grad1, grad2,grad3, x, i;

static Float[] pol11 = new Float[100];
static Float[] pol21 = new Float[100];
static Float[] pol31 = new Float[200]; 
    static Float grad11, grad21, grad31;
Float x1;


public static void main(String args[]){

    IPolinom<Integer> poli = new PolinomIntreg<>();
    IPolinom<Float> polr = new PolinomReal<>();

    System.out.println("Apasati 1 pentru operatii cu polinoame cu coef reali");
    System.out.println("Apasati 0 pentru operatii cu polinoame cu coef intregi");

    Scanner in = new Scanner(System.in);
    int meniu = in.nextInt();

    if( meniu == 0){

    poli.grad(grad1, grad2);
    poli.coef(pol1, pol2, grad1, grad2);
    poli.add(pol1, pol2, grad1, grad2);
    //poli.multiply(pol1, pol2, pol3, grad1, grad2, grad3);
    //poli.value(pol1, grad1);
    //poli.afisare(pol1, grad1);
    //poli.afisare(pol2, grad2);

            }

    else if ( meniu == 1 ){
        polr.grad(grad11, grad21);
        polr.coef(pol11, pol21, grad11, grad21);
        polr.add(pol11, pol21, grad11, grad21);
        polr.multiply(pol11, pol21, pol31, grad11, grad21, grad31);
        polr.value(pol11, grad11);
        polr.afisare(pol11, grad11);
        polr.afisare(pol21, grad21);
    }

    else System.out.println("Eroare introducere optiune! ");



}

}

IPolinom is an interface.

Upvotes: 0

Views: 402

Answers (4)

user_CC
user_CC

Reputation: 4776

If I understand your question correctly, If you are trying to call the add method then you are trying to call the add method which has variables of 'Integer' at the end, so if you declare your grad variables to be of type 'int' then it will go to the method that has code.

So to be more precise:

    poli.add(pol1, pol2, grad1, grad2);  here grad1 and grad2 are type of Integer. Declare grad1 and grad2 to be int then the add method which has code will be called.

declare grad1 and grad2 like

 int grad1, grad2;

Update:

Also change type of pol1 and pol2 to be of type int

   static int[] pol1 = new int[100];
   static int[] pol2 = new int[100];

Upvotes: 1

BobTheBuilder
BobTheBuilder

Reputation: 19294

You are not overriding the methods. In order to override, you need to use the exact signature ( you use int instead of Integer). I suggest you copy all your code inside the "methods without code" (or auto-generated methods) and remove your non overriding methods.

BTW, this is the ultimate argument for force using @Override annotation. If you had tried to add it - you would see your code isn't override any method.

Upvotes: 1

Achintya Jha
Achintya Jha

Reputation: 12843

public void value(int pol1[], int grad1)

and

public void value(INTEGER[] pol1, INTEGER grad1)

are different . You are calling public void value(INTEGER[] pol1, INTEGER grad1) which has no implementation .

Upvotes: 1

Boris the Spider
Boris the Spider

Reputation: 61178

From your code you seem to have some methods with INTEGER in the signature and others with int, for example

public void afisare(int pol1[], int grad1){

}

@Override
public void afisare(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}

The methods you wrote have the int signature and the methods of the Interface have the INTEGER signature. You are not overriding the methods you are simply writing different ones.

Move your implementation code into the methods with the correct signature.

Upvotes: 1

Related Questions