求java程序!!!大一的java课程设计题目,求高手送程序~~~求大家帮忙啊~~~
1复数类要求:1)该系统主要处理复数的相关信息。2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。提示:1)建立数据类、复数类2)数据...
1复数类
要求:
1)该系统主要处理复数的相关信息。
2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。
提示:
1)建立数据类、复数类
2)数据、复数信息的初始化
3)复数信息的输出
4)求复数的绝对值
5)实现复数的加、减、乘、除、乘方、自加、自减等运算 展开
要求:
1)该系统主要处理复数的相关信息。
2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。
提示:
1)建立数据类、复数类
2)数据、复数信息的初始化
3)复数信息的输出
4)求复数的绝对值
5)实现复数的加、减、乘、除、乘方、自加、自减等运算 展开
展开全部
完整的Java程序:
public class Test32 {
public static void main(String[] args) {
Complex c1 = new Complex(2, -1);
Complex c2 = new Complex(3, 4);
int m = 3;
System.out.println(c1.toString() + "的绝对值:" + c1.abs());
System.out.println(c1.toString() + "自增后:" + c1.addBySelf());
System.out.println(c1.toString() + "自减后:" + c1.subtractBySelf());
System.out.println("(" + c1.toString() + ") + (" + c2.toString() + ") = " + c1.add(c2));
System.out.println("(" + c1.toString() + ") - (" + c2.toString() + ") = " + c1.subtract(c2));
System.out.println("(" + c1.toString() + ") * (" + c2.toString() + ") = " + c1.multiply(c2));
System.out.println("(" + c1.toString() + ") / (" + c2.toString() + ") = " + c1.divide(c2));
System.out.println(c1.toString() + "的" + m + "次方 = " + c1.power(m));
}
}
//复数类:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减
class Complex{
protected double real; //实部
protected double image; //虚部
public Complex(){
real = image = 0;
}
public Complex(double real, double image){
this.real = real;
this.image = image;
}
//复数的绝对值
public Complex abs(){
return new Complex(Math.abs(this.real), Math.abs(this.image));
}
//复数相加
public Complex add(Complex c){
return new Complex(this.real + c.real, this.image + c.image);
}
//复数相减
public Complex subtract(Complex c){
return new Complex(this.real - c.real, this.image - c.image);
}
//复数相乘
public Complex multiply(Complex c){
return new Complex(this.real * c.real - this.image * c.image,
this.real * c.image + this.image * c.real);
}
//复数相除
public Complex divide(Complex c){
return new Complex((this.real * c.real + this.image * c.image) / (c.real * c.real + c.image * c.image),
(this.image * c.real - this.real * c.image) / (c.real * c.real + c.image * c.image));
}
//复数乘方
public Complex power(int m){
if(m < 0)
return new Complex();
if(m == 0)
return new Complex(1, 0);
Complex c = this;
for(int i=1; i<m; i++){
c = c.multiply(this);
}
return c;
}
//复数自增
public Complex addBySelf(){
return new Complex(++this.real, ++this.image);
}
//复数自减
public Complex subtractBySelf(){
return new Complex(--this.real, --this.image);
}
public String toString(){
if(this.real == 0)
if(this.image == 0)
return "0";
else
return this.image + "i";
else
if(this.image == 0)
return this.real + "";
else if(this.image > 0)
return this.real + "+" + this.image + "i";
else
return this.real + "" + this.image + "i";
}
}
运行测试:
2.0-1.0i的绝对值:2.0+1.0i
2.0-1.0i自增后:3.0
3.0自减后:2.0-1.0i
(2.0-1.0i) + (3.0+4.0i) = 5.0+3.0i
(2.0-1.0i) - (3.0+4.0i) = -1.0-5.0i
(2.0-1.0i) * (3.0+4.0i) = 10.0+5.0i
(2.0-1.0i) / (3.0+4.0i) = 0.08-0.44i
2.0-1.0i的3次方 = 2.0-11.0i
public class Test32 {
public static void main(String[] args) {
Complex c1 = new Complex(2, -1);
Complex c2 = new Complex(3, 4);
int m = 3;
System.out.println(c1.toString() + "的绝对值:" + c1.abs());
System.out.println(c1.toString() + "自增后:" + c1.addBySelf());
System.out.println(c1.toString() + "自减后:" + c1.subtractBySelf());
System.out.println("(" + c1.toString() + ") + (" + c2.toString() + ") = " + c1.add(c2));
System.out.println("(" + c1.toString() + ") - (" + c2.toString() + ") = " + c1.subtract(c2));
System.out.println("(" + c1.toString() + ") * (" + c2.toString() + ") = " + c1.multiply(c2));
System.out.println("(" + c1.toString() + ") / (" + c2.toString() + ") = " + c1.divide(c2));
System.out.println(c1.toString() + "的" + m + "次方 = " + c1.power(m));
}
}
//复数类:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减
class Complex{
protected double real; //实部
protected double image; //虚部
public Complex(){
real = image = 0;
}
public Complex(double real, double image){
this.real = real;
this.image = image;
}
//复数的绝对值
public Complex abs(){
return new Complex(Math.abs(this.real), Math.abs(this.image));
}
//复数相加
public Complex add(Complex c){
return new Complex(this.real + c.real, this.image + c.image);
}
//复数相减
public Complex subtract(Complex c){
return new Complex(this.real - c.real, this.image - c.image);
}
//复数相乘
public Complex multiply(Complex c){
return new Complex(this.real * c.real - this.image * c.image,
this.real * c.image + this.image * c.real);
}
//复数相除
public Complex divide(Complex c){
return new Complex((this.real * c.real + this.image * c.image) / (c.real * c.real + c.image * c.image),
(this.image * c.real - this.real * c.image) / (c.real * c.real + c.image * c.image));
}
//复数乘方
public Complex power(int m){
if(m < 0)
return new Complex();
if(m == 0)
return new Complex(1, 0);
Complex c = this;
for(int i=1; i<m; i++){
c = c.multiply(this);
}
return c;
}
//复数自增
public Complex addBySelf(){
return new Complex(++this.real, ++this.image);
}
//复数自减
public Complex subtractBySelf(){
return new Complex(--this.real, --this.image);
}
public String toString(){
if(this.real == 0)
if(this.image == 0)
return "0";
else
return this.image + "i";
else
if(this.image == 0)
return this.real + "";
else if(this.image > 0)
return this.real + "+" + this.image + "i";
else
return this.real + "" + this.image + "i";
}
}
运行测试:
2.0-1.0i的绝对值:2.0+1.0i
2.0-1.0i自增后:3.0
3.0自减后:2.0-1.0i
(2.0-1.0i) + (3.0+4.0i) = 5.0+3.0i
(2.0-1.0i) - (3.0+4.0i) = -1.0-5.0i
(2.0-1.0i) * (3.0+4.0i) = 10.0+5.0i
(2.0-1.0i) / (3.0+4.0i) = 0.08-0.44i
2.0-1.0i的3次方 = 2.0-11.0i
追问
我这边还有两个题 可以再帮忙解答下吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询