【发布时间】:2014-08-23 21:50:23
【问题描述】:
从方法返回对象
import java.util.Scanner;
class ComplexNos
{
private int x,y;
ComplexNos(int a,int b)
{
x=a;
y=b;
}
ComplexNos add (ComplexNos a) //method Returns an object
{
ComplexNos c=new ComplexNos();// iam getting an error here
c.x=x+a.x;
c.y=y+a.y;
return c;
}
void display()
{
if(y>=0)
System.out.println(x+"+i"+y);
else
System.out.println(x + "" + y +"i");
}
}
class ObjectReturn
{
public static void main(String args[])
{
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter real and imaginary part of a complex number:");
a=sc.nextInt();
b=sc.nextInt();
ComplexNos c1=new ComplexNos(a,b);
System.out.println("Enter real and imaginary part of another complex
number:");
a=sc.nextInt();
b=sc.nextInt();
ComplexNos c2=new ComplexNos(a,b);
ComplexNos c3 = new ComplexNos();//iam getting an error here
c3=c1.add(c2); //Storing new value in object c3
c3.display();
System.out.println("Value of the 1st object is: ");
c1.display();
System.out.println("Value of the 2nd object is: ");
c2.display();
}
}
output:
ObjectReturn.java:12: error: constructor ComplexNos in class ComplexNos cannot b
e applied to given types;
ComplexNos c=new ComplexNos();
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
ObjectReturn.java:39: error: constructor ComplexNos in class ComplexNos cannot b
e applied to given types;
ComplexNos c3 = new ComplexNos();
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
2 errors
我是初学者,所以我无法理解问题所在。
【问题讨论】:
标签: java compiler-errors