扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
package table;
成都创新互联公司是一家集网站建设,金湾企业网站建设,金湾品牌网站建设,网站定制,金湾网站建设报价,网络营销,网络优化,金湾网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
public class Complex
{
double real;
double imaginary;
public static final Complex ZERO = new Complex (0, 0);
public static final Complex ONE = new Complex (1, 0);
public static final Complex I = new Complex (0, 1);
public Complex ( double real, double imaginary )
{
this.real = real;
this.imaginary = imaginary;
}
public double magnitude ()
{
return Math.sqrt (this.real * this.real + this.imaginary * this.imaginary);
}
public Complex negative ()
{
return new Complex (-real, -imaginary);
}
public double valueOf ()
{
return this.real;
}
public Complex add ( Complex a, Complex b )
{
return new Complex (a.real + b.real, a.imaginary + b.imaginary);
}
public Complex subtract ( Complex a, Complex b )
{
return new Complex (a.real - b.real, a.imaginary - b.imaginary);
}
public Complex multiply ( Complex a, Complex b )
{
return new Complex (a.real * b.real - a.imaginary * b.imaginary, a.real * b.imaginary + a.imaginary * b.real);
}
@Override
public String toString ()
{
StringBuilder builder = new StringBuilder ();
builder.append ("Complex [real=").append (real).append (", imaginary=").append (imaginary).append ("]");
return builder.toString ();
}
}
import java.util.*;
public class ComplexTest{
static class ComplexNumber{
private double real,image;
public ComplexNumber(){
this(0.0,0.0);}
public ComplexNumber(double a,double b){
real=a;image=b;
}
public ComplexNumber add(ComplexNumber x){
return new ComplexNumber(real+x.real,image+x.image);}
public ComplexNumber sub(ComplexNumber x){
return new ComplexNumber(real-x.real,image-x.image); }
public ComplexNumber mul(ComplexNumber x){
return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}
public ComplexNumber div(ComplexNumber x){
if(x.real==0x.image==0){
System.out.println("无法进行除法!");
return new ComplexNumber();}
else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image)
,(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}
public double getReal (){return real;}
public double getImage (){return image;}
public void show(){System.out.println(this.toString());}
public String toString(){
if(image0)return ""+real+image+"i";
else return ""+real+"+"+image+"i";
}
}
static class Test{
public Test(){
Scanner sr=new Scanner(System.in);
ComplexNumber a,b,c;
try{System.out.println("请输入第一个实部和虚部:");
a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.println("请输入第二个实部和虚部:");
b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());
System.out.print("第一个复数:");a.show();
System.out.print("第二个复数:");b.show();
c=a.add(b);
System.out.print("两个复数的和:");c.show();
c=a.sub(b);
System.out.print("两个复数的差:");c.show();
c=a.mul(b);
System.out.print("两个复数的积:");c.show();
c=a.div(b);
System.out.print("两个复数的商:");c.show();
}
catch(InputMismatchException e){
System.out.println("输入有误!");}
}
}
public static void main(String[] args){
new Test();
}
}
(1):具体代码(附注释)
复数类:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu+p.shibu;
result.xubu=this.xubu+p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print("加法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu+p.shibu*this.xubu;
System.out.print("乘法结果为:"+result.shibu+"+"+result.xubu+"i");
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println("请输入数据:");
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第一个复述的实部:");
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第一个复述的虚部:");
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的虚部:");
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println("您输入的一个数为:");
toString(h);
System.out.println("您输入的二个数为:");
toString(j);
Book k;
k=new Book();
char z='y';
do{
System.out.println("请选择您要进行的计算:");
System.out.println("1 :进行加法运算");
System.out.println("2 :进行减法运算");
System.out.println("3 :进行修改");
System.out.println("4 :进行乘法运算");
System.out.println("5 :进行除法运算");
System.out.println("6 :查看修改结果");
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
switch(i)
{
case 1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 3:
System.out.println("请输入您要修改哪个实数:");
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=0;
f=cheng(h.sb,j.sb)+cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb)+cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println("计算结果的实部为:"+k.sb);
System.out.println("计算结果的虚部为:"+k.xb);
toString(k);
break ;
case 6:
System.out.println("修改后的结果为:");
System.out.println("第一个复数:"+toString(h));
System.out.println("第二个复数:"+toString(j));
break ;
}
System.out.println("请问您是否还要继续 y/n:");
try{
z=(char)System.in.read();
System.in.skip(2); //忽略回车换行
}
catch(IOException e){}
} while(z=='y');
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的实部:");
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
a.sb=m;
System.out.println("修改成功:");
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的虚部:");
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
b.xb=n;
System.out.println("修改成功:");
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a+b;
System.out.println("加法成功:");
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println("减法成功:");
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println("乘法成功:");
return c;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println("除法成功:");
return c ;
}
public static double toString(Book a){
System.out.println("结果为:"+a.sb+"+"+a.xb+"*i");
return 0;
}
}
(3)测试结果截图:
1、real和image这两个field前面的static去掉。
2、public Complex() 这个构造器去掉,如果要接受输入的话,应该放到main方法里,这样这个类更清晰。
3、静态方法Complex_add和Complex_minus没指定返回值类型,应该返回的是Complex。另外方法名字首字母应小写。
4、参考这个:
public class Complex {
protected int a;
protected int b;
public Complex(int a, int b) {
this.a = a;
this.b = b;
}
public String toString() {
return this.a + " + " + this.b + "i";
}
public static Complex addition(Complex complex1, Complex complex2) {
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
}
public static Complex subtract(Complex complex1, Complex complex2) {
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
}
public static Complex multiplication(Complex complex1, Complex complex2) {
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
}
public static Complex division(Complex complex1, Complex complex2) throws Exception {
if (complex2.a == 0) {
throw new Exception("complex2.a is 0");
}
if (complex2.b == 0) {
throw new Exception("complex2.b is 0");
}
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
}
}
//测试用的类
public class ComplexTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流