JAVA语言程序设计题请高手回答

建立一个表示复数的类:私有数据为两个浮点数分别表示实部和虚部,初始化方式有1)无参默认为0;2)两个浮点数分别表示实部和虚部;外部接口有:以a+bi的形式输出的复数;两个复数相加;复数加一浮点数;比较两个复数是否相等。

完整代码如下:

public class Complex {

private float real; //实部
private float imagin;//虚部

public Complex(){//无参默认为(0, 2)
this.real = 0F;
this.imagin = 2F;
}

public String toString(){//以a+bi的形式输出的复数
return real + "+" + imagin + "i";
}

// a+ bi + (c+ di) = (a+c) + (b+d)i
public static Complex add(Complex c1, Complex c2){//两个复数相加
Complex complex = new Complex();
complex.setReal(c1.getReal() + c2.getReal());
complex.setImagin(c1.getImagin() + c2.getImagin());

return complex;
}

// a+ bi ==? c + di----> a==c, b==d --> it's true
public static boolean equal(Complex c1, Complex c2){//比较两个复数是否相等
return c1.getReal() == c2.getReal() && c1.getImagin() == c2.getImagin();
}

//a + bi + f = (a+f) + bi
public static Complex addFloat(Complex c1, float fValue){//复数加一浮点数
c1.setReal(c1.getReal() + fValue);
return c1;
}

public float getImagin() {
return imagin;
}

public void setImagin(float imagin) {
this.imagin = imagin;
}

public float getReal() {
return real;
}

public void setReal(float real) {
this.real = real;
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-30
//boss Wang
package example;

public class ComplexClass {
private double real,imag;
public ComplexClass()
{
this.real = 0.0;
this.imag = 0.0;
}
public ComplexClass(double real,double imag)
{
this.real = real;
this.imag = imag;
}
public void setomplex(double real,double imag)
{
this.real = real;
this.imag = imag;
}
public void complexAdd(ComplexClass complex)
{
ComplexClass add = new ComplexClass();
add.real = (this.real + complex.real);
add.imag = (this.imag + complex.imag);
System.out.println(this.toString()+" 和 "+complex.toString()+" 的和是 "+add.toString());
}
public void complexdecrease(ComplexClass complex)
{
ComplexClass dec = new ComplexClass();
dec.real = (this.real - complex.real);
dec.imag = (this.imag - complex.imag);
System.out.println(this.toString()+" 和 "+complex.toString()+" 的差是 "+dec.toString());
}
public void complexCompare(ComplexClass complex)
{
ComplexClass max;
if(this.real == complex.real&&this.imag == complex.imag)
System.out.println(this.toString()+" 和 "+complex.toString()+" 一样大!!!");
if(this.real > complex.real)
System.out.println(this.toString()+" 和 "+complex.toString()+" 中 "+this.toString()+" 大!!!");
if(this.imag > complex.imag)
System.out.println(this.toString()+" 和 "+complex.toString()+" 中 "+this.toString()+" 大!!!");
else
System.out.println(this.toString()+" 和 "+complex.toString()+" 中 "+complex.toString()+" 大!!!");
}
public String toString()
{
String complexString;
if(this.imag == 0)
complexString = String.valueOf(this.real);
else if(this.real == 0)
complexString = String.valueOf(this.imag+"i");
else if(this.imag > 0)
complexString = String.valueOf(this.real + "+" + this.imag + "i");
else
complexString = String.valueOf(String.valueOf(this.real) + this.imag + "i");
return complexString;
}
}

//boss Wang
package example;

public class test {

public static void main(String[] args) {
ComplexClass a = new ComplexClass(1,2);
ComplexClass b = new ComplexClass(2,3);
a.toString();
b.toString();
a.complexAdd(b);
a.complexdecrease(b);
a.complexCompare(b);
}
}
我懒得改;
自己去改改 不难 大体都在 这是我学的时候编的
第2个回答  2010-12-30
public class Plural{
private float real;
private float virtual;

//无参构造函数
Plural(){
real=0f;
virtual=0f;
}

Plural(float r,float v){
real=r;
virtual=v;
}
//返回以a+bi的形式输出的复数
String toString(){
return real+"+"+virtual+"i";
}

//与另一个复数相加
void add(Plural p){
real+=p.real;
virtual+=p.virtual;
}
//与一个浮点数相加
void add(float a){
real+=a;
}
//比较两个复数是否相等
boolean equals(Plural p){
return real==p.real&&virtual==p.virtual;
}
}
第3个回答  2011-01-03
一、填空
1.方法名;参数列表;个数;类型;顺序
2.this
3.值传递
4.软件重用;程序结构;类;类
5.InpitStreamReader;OutputStreamWriter
6.几个程序或任务的能力

二、选择
A B A D D B C B D A C C

三、判断
1.2.3.7错,其余对

四、
通过按钮激活change()文件。
<Form>
<Input type="button"Value=""onClick="change()">
</Form>
第4个回答  2010-12-30
这么简单的题目还要高手回答!
随便一个菜鸟都能回答。
百度知道成了帮学生做作业的地方了么?悲哀!

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网