我正在尝试从“Operatii”类中执行一个方法。该方法写在“PolinomIntreg”类中。问题是,在执行时,程序会转到没有代码的覆盖方法,而不是代码所在的方法。
我该怎么做才能让程序转到有代码的那个?
PolinomIntreg 类:
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
}
}
操作类:
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 是一个接口。