类#是公共的,应在名为#.java的文件中声明
classComplex{privateintrealPart;privateintimaginPart;Complex(){this.realPart=0;this.i...
class Complex{
private int realPart;
private int imaginPart;
Complex(){
this.realPart = 0;
this.imaginPart = 0;
}
Complex(int realPart, int imaginPart){
this.realPart = realPart;
this.imaginPart = imaginPart;
}
public void setRealPart(int realPart){
this.realPart = realPart;
}
public void setImaginPart(int imaginPart){
this.imaginPart = imaginPart;
}
public int getRealPart(){
return this.realPart;
}
public int getImaginPart(){
return this.imaginPart;
}
Complex complexAdd(Complex a){
int realPart = this.realPart + a.getRealPart();
int imaginPart = this.imaginPart + a.getImaginPart();
return new Complex(realPart, imaginPart);
}
public String toString(){
if(this.imaginPart == 0) return "" + this.realPart;
else if(this.imaginPart > 0) return "" + this.realPart + "+" + this.imaginPart + "i";
else return "" + this.realPart + this.imaginPart + "i";
}
}
public class TextComplex{
public static void main(String[] args){
Complex a = new Complex(1,2);
Complex b = new Complex(3,4);
Complex sum = a.complexAdd(b);
System.out.println("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
a.setRealPart(2);
a.setImaginPart(3);
b.setRealPart(b.getRealPart() * 2);
b.setImaginPart(b.getImaginPart() *2);
sum = a.complexAdd(b);
System.out.println("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
}
} 展开
private int realPart;
private int imaginPart;
Complex(){
this.realPart = 0;
this.imaginPart = 0;
}
Complex(int realPart, int imaginPart){
this.realPart = realPart;
this.imaginPart = imaginPart;
}
public void setRealPart(int realPart){
this.realPart = realPart;
}
public void setImaginPart(int imaginPart){
this.imaginPart = imaginPart;
}
public int getRealPart(){
return this.realPart;
}
public int getImaginPart(){
return this.imaginPart;
}
Complex complexAdd(Complex a){
int realPart = this.realPart + a.getRealPart();
int imaginPart = this.imaginPart + a.getImaginPart();
return new Complex(realPart, imaginPart);
}
public String toString(){
if(this.imaginPart == 0) return "" + this.realPart;
else if(this.imaginPart > 0) return "" + this.realPart + "+" + this.imaginPart + "i";
else return "" + this.realPart + this.imaginPart + "i";
}
}
public class TextComplex{
public static void main(String[] args){
Complex a = new Complex(1,2);
Complex b = new Complex(3,4);
Complex sum = a.complexAdd(b);
System.out.println("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
a.setRealPart(2);
a.setImaginPart(3);
b.setRealPart(b.getRealPart() * 2);
b.setImaginPart(b.getImaginPart() *2);
sum = a.complexAdd(b);
System.out.println("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
}
} 展开
展开全部
一个.java文件中只允许出现一个public class,并且用public修饰的类作为文件的名字,例如A.java中
public class A{
//do something
}
class B{
//do something
}
class C{
//do something
}
public class A{
//do something
}
class B{
//do something
}
class C{
//do something
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
文件名应是 TextComplex.java,因为 TextComplex类是public的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个文件的文件名应该是TextComplex.java
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是干什么啊,不明白作者要表达什么意思。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询