Я работаю над проектом для домашнего задания. Тестовый код был дан, и я в основном должен заставить его работать.
Я застрял при создании метода add
, я не могу понять, как заставить работать с входами на тестовой странице. Любая помощь будет оценена. Вот код теста:
import java.util.Scanner;
// Test the Complex number class
public class ComplexTest {
public static void main(String[] args) {
// use Scanner object to take input from users
Scanner input = new Scanner(System.in);
System.out.println("Enter the real part of the first number:");
double real = input.nextDouble();
System.out.println("Enter the imaginary part of the first number:");
double imaginary = input.nextDouble();
Complex a = new Complex(real, imaginary);
System.out.println("Enter the real part of the second number:");
real = input.nextDouble();
System.out.println("Enter the imaginary part of the second number:");
imaginary = input.nextDouble();
Complex b = new Complex(real, imaginary);
System.out.printf("a = %s%n", a.toString());
System.out.printf("b = %s%n", b.toString());
System.out.printf("a + b = %s%n", a.add(b).toString());
System.out.printf("a - b = %s%n", a.subtract(b).toString());
}
}
А вот что у меня есть:
public class Complex {
private double real;
private double imaginary;
public Complex() {
this(0,0);
}
public Complex(double real) {
this(real,0);
}
public Complex(double real, double imaginary) {
this.real=real;
this.imaginary = imaginary;
}
public void setReal(double real) {
this.real = real;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public double add(double a, double b) {
return a + b;
}
}