「java题目复数」编写一个类实现复数的运算java

博主:adminadmin 2022-12-31 23:03:08 829

本篇文章给大家谈谈java题目复数,以及编写一个类实现复数的运算java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 复数问题

1、首先Java中的数都是有符号的

2、所以,负数的话只需直接在数字前面加个负号(-)即可,如-5。

3、要开平方的话,请直接调用Math.sqrt(数值)方法即可。

4、当然,由于要开平方的数都必须是正数,所以,你可以直接调用Math.abs(d)方法求绝对值最为正确与简便,而不用写成-(-5),代码里也不提倡此写法。

有问题请继续提问,欢迎采纳,谢谢!

java 中实现复数的加减

(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)测试结果截图:

Java作业两道题

1.public class Complex {

int realPart ;//实数

int imaginPart;//虚数

public Complex(){

this.imaginPart = 0;

this.realPart = 0;

}

public Complex(int r ,int i ){

this.realPart = r;

this.imaginPart = i;

}

public Complex complexAdd(Complex a){

this.imaginPart += a.imaginPart;

this.realPart += a.realPart;

return this;

}

public String toString() {

if(imaginPart = 0){

return this.realPart+"+"+this.imaginPart+"i";

}

return this.realPart+"-"+this.imaginPart+"i";

}

public static void main(String[] args) {

Complex c = new Complex(1,3);

c.complexAdd(c);

System.out.println(c.toString());

}

}

2.public class Rectangle {

double length;

double width;

public Rectangle(double length,double width){

this.length = length;

this.width = width;

}

public double getArea(){

double a = this.length * this.width;

System.out.println("矩形面积为"+a);

return a;

}

public static void main(String[] args) {

Rectangle r = new Rectangle(1.5, 1.5);

r.getArea();

}

}

3.public class Cone {

Rectangle rect;

double height;

public Cone(Rectangle rect,double height){

this.rect = rect;

this.height = height;

}

public double getVolume(){

double a = (this.rect.getArea() * this.height)/3;

System.out.println("四棱锥的体积为:"+a);

return a;

}

public static void main(String[] args) {

Cone c = new Cone(new Rectangle(1.5, 1.5), 5);

c.getVolume();

}

}

多注重自己学习。祝你成功。

一个Java题:设计一个程序用于计算复数的加法和减法运算。比如:1+2i与3+4i的和为4+6i

//他的思路是 建立一个对象Number

public Class Number{

//其中两个成员变量 RQ用来表示实数部分,IQ用来表示虚数部分

    private float RQ;

    private float IQ;

    

    //用构造方法来分别对实数和虚数赋值

    //然后用Add()方法进行计算,其中传入的参数为要加的那个部分(比如你题目中的3+4i)

    //然后再进行字符串的拼接,就成了最终结果.

    //其实他写的这个拼接形式只能做加法,其实判断一下结果是能够顺便做减法的

}

关于java题目复数和编写一个类实现复数的运算java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。