属性包括实数部分和虚数部分;方法有加法add和减法subtract;编写图形界面的程序验证以上的复数类,接收用户输入的复数的实部和虚部,计算复数与复数、复数与实数的加、减的操作。
评定标准:正确地使用重载概念定义复数类,正确地编写测试类验证复数类的加、减操作。
使用JAVA编程实现复数类ComplexNumber
答案:1 悬赏:60
解决时间 2021-11-25 09:34
- 提问者网友:敏感魔鬼
- 2021-11-24 23:03
最佳答案
- 二级知识专家网友:劳资的心禁止访问
- 2021-11-24 23:34
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ComplexTest extends Applet implements ActionListener{
Label firstReal, firstImg;
TextField firstRealNum, firstImgNum;
Label secondReal, secondImg;
TextField secondRealNum, secondImgNum;
Button add = new Button("Add");
Button subtract = new Button("Subtract");
TextField addResult, subtractResult;
public void init(){
firstReal = new Label("First Complex Real Number: ");
firstRealNum = new TextField(7);
super.add(firstReal);
super.add(firstRealNum);
firstImg = new Label("First Complex Imaginary Number: ");
firstImgNum = new TextField(7);
super.add(firstImg);
super.add(firstImgNum);
secondReal = new Label("Second Complex Real Number: ");
secondRealNum = new TextField(7);
super.add(secondReal);
super.add(secondRealNum);
secondImg = new Label("Second Complex Imaginary Number: ");
secondImgNum = new TextField(7);
super.add(secondImg);
super.add(secondImgNum);
super.add(add);
addResult = new TextField(7);
super.add(addResult);
super.add(subtract);
subtractResult = new TextField(7);
super.add(subtractResult);
add.addActionListener(this);
subtract.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
double firstComplxReal = Double.parseDouble(firstRealNum.getText());
double firstComplxImg = Double.parseDouble(firstImgNum.getText());
double secondComplxReal = Double.parseDouble(secondRealNum.getText());
double secondComplxImg = Double.parseDouble(secondImgNum.getText());
ComplexNumber complxNum1 = new ComplexNumber(firstComplxReal, firstComplxImg);
ComplexNumber complxNum2 = new ComplexNumber(secondComplxReal, secondComplxImg);
addResult.setText(complxNum1.add(complxNum2).toString());
subtractResult.setText(complxNum1.subtract(complxNum2).toString());
}
}
class ComplexNumber{
private double real;
private double imaginary;
public ComplexNumber(double realNum, double imaginaryNum){
this.real = realNum;
this.imaginary = imaginaryNum;
}
// (a+bi) + (c+di) = (a+b) + (c+d)i
public ComplexNumber add(ComplexNumber complexNum2){
double newRealPart = this.real + complexNum2.getReal();
double newImgPart = this.imaginary + complexNum2.getImaginary();
return new ComplexNumber(newRealPart, newImgPart);
}
//(a+bi) - (c+di) = (a-b) - (c-d)i
public ComplexNumber subtract(ComplexNumber complexNum2){
double newRealPart = this.real - complexNum2.getReal();
double newImgPart = this.imaginary - complexNum2.getImaginary();
return new ComplexNumber(newRealPart, newImgPart);
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public String toString(){
return real + "+" + imaginary + "i";
}
}
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ComplexTest extends Applet implements ActionListener{
Label firstReal, firstImg;
TextField firstRealNum, firstImgNum;
Label secondReal, secondImg;
TextField secondRealNum, secondImgNum;
Button add = new Button("Add");
Button subtract = new Button("Subtract");
TextField addResult, subtractResult;
public void init(){
firstReal = new Label("First Complex Real Number: ");
firstRealNum = new TextField(7);
super.add(firstReal);
super.add(firstRealNum);
firstImg = new Label("First Complex Imaginary Number: ");
firstImgNum = new TextField(7);
super.add(firstImg);
super.add(firstImgNum);
secondReal = new Label("Second Complex Real Number: ");
secondRealNum = new TextField(7);
super.add(secondReal);
super.add(secondRealNum);
secondImg = new Label("Second Complex Imaginary Number: ");
secondImgNum = new TextField(7);
super.add(secondImg);
super.add(secondImgNum);
super.add(add);
addResult = new TextField(7);
super.add(addResult);
super.add(subtract);
subtractResult = new TextField(7);
super.add(subtractResult);
add.addActionListener(this);
subtract.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
double firstComplxReal = Double.parseDouble(firstRealNum.getText());
double firstComplxImg = Double.parseDouble(firstImgNum.getText());
double secondComplxReal = Double.parseDouble(secondRealNum.getText());
double secondComplxImg = Double.parseDouble(secondImgNum.getText());
ComplexNumber complxNum1 = new ComplexNumber(firstComplxReal, firstComplxImg);
ComplexNumber complxNum2 = new ComplexNumber(secondComplxReal, secondComplxImg);
addResult.setText(complxNum1.add(complxNum2).toString());
subtractResult.setText(complxNum1.subtract(complxNum2).toString());
}
}
class ComplexNumber{
private double real;
private double imaginary;
public ComplexNumber(double realNum, double imaginaryNum){
this.real = realNum;
this.imaginary = imaginaryNum;
}
// (a+bi) + (c+di) = (a+b) + (c+d)i
public ComplexNumber add(ComplexNumber complexNum2){
double newRealPart = this.real + complexNum2.getReal();
double newImgPart = this.imaginary + complexNum2.getImaginary();
return new ComplexNumber(newRealPart, newImgPart);
}
//(a+bi) - (c+di) = (a-b) - (c-d)i
public ComplexNumber subtract(ComplexNumber complexNum2){
double newRealPart = this.real - complexNum2.getReal();
double newImgPart = this.imaginary - complexNum2.getImaginary();
return new ComplexNumber(newRealPart, newImgPart);
}
public double getImaginary() {
return imaginary;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public String toString(){
return real + "+" + imaginary + "i";
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯