早教吧作业答案频道 -->其他-->
Java作业关于复数的懂得帮帮吧,完成一定加分2.\x05实验要求(1)编写一个复数(z=x+iy)类Complex,其中包括成员变量x(复数的实部:int型)、y(复数的虚部:int型)和方法如下:\x05构造函数C
题目详情
Java作业 关于复数的 懂得帮帮吧,完成一定加分
2.\x05实验要求
(1)编写一个复数(z=x+iy)类Complex,其中包括成员变量x (复数的实部:int型)、y (复数的虚部:int型)和方法如下:
\x05构造函数 Complex(),设置x=0,y=0;构造函数:Complex(int i,int j)
\x05显示复数的方法:showComp()将其显示为如:5+8i或5-8i 的形式;
\x05求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Complex C1,Complex C2)
\x05求两个复数的差的方法:public Complex subComp(Complex C1,Complex C2)
\x05求两个复数的乘积的方法:public Complex multiComp(Complex C1,Complex C2)
\x05比较两个复数是否相等的方法:public boolean equalComp(Complex C1,Complex C2)
(2)编写一个主类NumberComplex,在其main方法中,定义并实例化两个复数对象,输出这两个复数对象的值,然后进行复数的加法、减法、乘法、是否相等运算,输出其运算结果(对象)的值
2.\x05实验要求
(1)编写一个复数(z=x+iy)类Complex,其中包括成员变量x (复数的实部:int型)、y (复数的虚部:int型)和方法如下:
\x05构造函数 Complex(),设置x=0,y=0;构造函数:Complex(int i,int j)
\x05显示复数的方法:showComp()将其显示为如:5+8i或5-8i 的形式;
\x05求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Complex C1,Complex C2)
\x05求两个复数的差的方法:public Complex subComp(Complex C1,Complex C2)
\x05求两个复数的乘积的方法:public Complex multiComp(Complex C1,Complex C2)
\x05比较两个复数是否相等的方法:public boolean equalComp(Complex C1,Complex C2)
(2)编写一个主类NumberComplex,在其main方法中,定义并实例化两个复数对象,输出这两个复数对象的值,然后进行复数的加法、减法、乘法、是否相等运算,输出其运算结果(对象)的值
▼优质解答
答案和解析
public class Complex {
\x05private int entry;//实部
\x05private int visual;//虚部
\x05public Complex() {
\x05\x05super();
\x05}
\x05public Complex(int en,int vi) {
\x05\x05super();
\x05\x05this.setEntry(en);
\x05\x05this.setVisual(vi);
\x05}
\x05public int getEntry() {
\x05\x05return entry;
\x05}
\x05public void setEntry(int entry) {
\x05\x05this.entry = entry;
\x05}
\x05public int getVisual() {
\x05\x05return visual;
\x05}
\x05public void setVisual(int visual) {
\x05\x05this.visual = visual;
\x05}
\x05//显示:
\x05public void showComp(){
\x05\x05System.out.println(this.getEntry()+"+"+this.getVisual()+"i");
\x05}
\x05//加法:
\x05public Complex addComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry + c2.entry;
\x05\x05int v = c1.visual + c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//减法:
\x05public Complex subComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry - c2.entry;
\x05\x05int v = c1.visual - c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//乘法:
\x05public Complex multiComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry*c2.entry - c1.visual*c2.visual;
\x05\x05int v = c1.visual*c2.entry + c1.entry*c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//相同返回true,不同返回false
\x05public boolean equalComp(Complex c1,Complex c2){
\x05\x05\x05return c1.entry==c2.entry && c1.visual == c2.visual;
\x05}
}
//测试类
public class Test {
\x05public static void main(String [] args){
\x05\x05//验证默认构造初始化:实部是0,虚部也是0
\x05\x05Complex c = new Complex();
\x05\x05System.out.print("默认构造结果是:");
\x05\x05System.out.println(c.getEntry());
\x05\x05System.out.println(c.getVisual());
\x05\x05//加法:
\x05\x05Complex c1 = new Complex(3,7);
\x05\x05Complex c2 = new Complex(5,6);
\x05\x05Complex cj = new Complex();
\x05\x05cj = c.addComp(c1,c2);
\x05\x05System.out.print("加法结果是:");
\x05\x05cj.showComp();
\x05\x05//减法:
\x05\x05Complex c3 = new Complex();
\x05\x05Complex c4 = new Complex();
\x05\x05Complex cm = new Complex();
\x05\x05cm = c.subComp(c1,c2);
\x05\x05System.out.print("减法结果是:");
\x05\x05cm.showComp();
\x05\x05//乘法:
\x05\x05Complex c5 = new Complex();
\x05\x05Complex c6 = new Complex();
\x05\x05Complex cc = new Complex();
\x05\x05cc = c.multiComp(c1,c2);
\x05\x05System.out.print("乘法结果是:");
\x05\x05cc.showComp();
\x05\x05//相等比较:
\x05\x05Complex c7 = new Complex(3,-3);
\x05\x05Complex c8 = new Complex(3,4);\x05\x05
\x05\x05Complex c9 = new Complex(3,4);\x05\x05
\x05\x05boolean resuls1 = c.equalComp(c7,c8);
\x05\x05boolean resuls2 = c.equalComp(c8,c9);
\x05\x05System.out.print("比较结果是:");
\x05\x05System.out.print(resuls1?true:false);
\x05\x05System.out.print(resuls2?true:false);
\x05}
}
\x05private int entry;//实部
\x05private int visual;//虚部
\x05public Complex() {
\x05\x05super();
\x05}
\x05public Complex(int en,int vi) {
\x05\x05super();
\x05\x05this.setEntry(en);
\x05\x05this.setVisual(vi);
\x05}
\x05public int getEntry() {
\x05\x05return entry;
\x05}
\x05public void setEntry(int entry) {
\x05\x05this.entry = entry;
\x05}
\x05public int getVisual() {
\x05\x05return visual;
\x05}
\x05public void setVisual(int visual) {
\x05\x05this.visual = visual;
\x05}
\x05//显示:
\x05public void showComp(){
\x05\x05System.out.println(this.getEntry()+"+"+this.getVisual()+"i");
\x05}
\x05//加法:
\x05public Complex addComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry + c2.entry;
\x05\x05int v = c1.visual + c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//减法:
\x05public Complex subComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry - c2.entry;
\x05\x05int v = c1.visual - c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//乘法:
\x05public Complex multiComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry*c2.entry - c1.visual*c2.visual;
\x05\x05int v = c1.visual*c2.entry + c1.entry*c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//相同返回true,不同返回false
\x05public boolean equalComp(Complex c1,Complex c2){
\x05\x05\x05return c1.entry==c2.entry && c1.visual == c2.visual;
\x05}
}
//测试类
public class Test {
\x05public static void main(String [] args){
\x05\x05//验证默认构造初始化:实部是0,虚部也是0
\x05\x05Complex c = new Complex();
\x05\x05System.out.print("默认构造结果是:");
\x05\x05System.out.println(c.getEntry());
\x05\x05System.out.println(c.getVisual());
\x05\x05//加法:
\x05\x05Complex c1 = new Complex(3,7);
\x05\x05Complex c2 = new Complex(5,6);
\x05\x05Complex cj = new Complex();
\x05\x05cj = c.addComp(c1,c2);
\x05\x05System.out.print("加法结果是:");
\x05\x05cj.showComp();
\x05\x05//减法:
\x05\x05Complex c3 = new Complex();
\x05\x05Complex c4 = new Complex();
\x05\x05Complex cm = new Complex();
\x05\x05cm = c.subComp(c1,c2);
\x05\x05System.out.print("减法结果是:");
\x05\x05cm.showComp();
\x05\x05//乘法:
\x05\x05Complex c5 = new Complex();
\x05\x05Complex c6 = new Complex();
\x05\x05Complex cc = new Complex();
\x05\x05cc = c.multiComp(c1,c2);
\x05\x05System.out.print("乘法结果是:");
\x05\x05cc.showComp();
\x05\x05//相等比较:
\x05\x05Complex c7 = new Complex(3,-3);
\x05\x05Complex c8 = new Complex(3,4);\x05\x05
\x05\x05Complex c9 = new Complex(3,4);\x05\x05
\x05\x05boolean resuls1 = c.equalComp(c7,c8);
\x05\x05boolean resuls2 = c.equalComp(c8,c9);
\x05\x05System.out.print("比较结果是:");
\x05\x05System.out.print(resuls1?true:false);
\x05\x05System.out.print(resuls2?true:false);
\x05}
}
看了 Java作业关于复数的懂得帮...的网友还看了以下:
如果n=1,f(n)=1;n=2,f(n)=3;n=3,f(n)=6;n=4,f(n)=10;那么当 2020-03-30 …
已知数列a(n)为等比数列,a(4)=16,q=2,数列b(n)前N项和s(n)=1/2*n的平方 2020-05-13 …
当n取正整数时,定义N(n)表示n的最大奇因数.如N(1)=1,N(2)=1,N(3)=3,N(4 2020-05-13 …
,;定义在正整数集f(x)对任意m,n,都有f(m+n)=f(m)+f(n)+4(m+n)-2,且 2020-05-13 …
数列a[n+1]=k+(2k+1)a[n]+(k(k+1)a[n]a[n+1])^1/2 已知a1 2020-05-16 …
(1/(n^2 n 1 ) 2/(n^2 n 2) 3/(n^2 n 3) ……n/(n^2 n 2020-05-16 …
下图为“地球局部地区近地面气压带和风带分布示意图”,读图完成以下问题。1.图中关于M、N气压带的叙 2020-05-17 …
a(n+1)=[(n+1)/n]an+(n+1)/2^n两边同除(n+1)得:a(n+1)/(n+ 2020-06-02 …
若50条直线将平面分成N部分,N≤98,N可取的值是 2020-06-15 …
当磁铁NS极磁感线外部是N到S内部是S到N如果在内部挖空放上一个小磁铁(很小)还是小磁铁N极和掏空 2020-06-25 …