JAVA程序设计 考题 高分求助
发布网友
发布时间:2022-05-12 00:35
我来回答
共3个回答
热心网友
时间:2023-10-30 05:24
package com.;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
//复数类的定义
class ComplexNumber {
// 定义复数里的实部与虚部;
private double RealPart, ImaginPart;
// 缺省情况的构造方法;
public ComplexNumber() {
RealPart = 0;
ImaginPart = 0;
}
// 有形参的构造方法;
public ComplexNumber(double r, double i) {
RealPart = r;
ImaginPart = i;
}
// 获取当前对象的实部;
public double getRealPart() {
return RealPart;
}
// 获取当前对象的虚部;
public double getImaginPart() {
return ImaginPart;
}
// 修改当前对象的实部;
public void setRealPart(double r) {
RealPart = r;
}
// 修改当前对象的虚部;
public void setImaginPart(double i) {
ImaginPart = i;
}
// 当前的复数对象与形参复数相加方法;
public ComplexNumber ComplexNumberAdd(ComplexNumber c) {
ComplexNumber result = new ComplexNumber();
/*
* 因为实部与虚部均以private修饰,所以要通过复数类中的setRealPart()与setImaginPart()方法来修改;
* 两复数的实部相加
*/
result.setRealPart(this.RealPart + c.getRealPart());
// 两复数的虚部相加;
result.setImaginPart(this.ImaginPart + c.getImaginPart());
return result;
}
// 当前复数对象与一实数相加;
public ComplexNumber ComplexNumberAdd(double c) {
ComplexNumber result = new ComplexNumber();
// 实部加上实数;
result.setRealPart(this.RealPart + c);
// 虚部不变;
result.setImaginPart(this.ImaginPart);
return result;
}
// 当前复数对象减去形参复数对象;
public ComplexNumber ComplexNumberMinus(ComplexNumber c) {
ComplexNumber result = new ComplexNumber();
// 当前复数的实部减去形参对象的虚部;
result.setRealPart(this.RealPart - c.getRealPart());
// 当前复数的虚部减去形参对象的虚部;
result.setImaginPart(this.ImaginPart - c.getImaginPart());
return result;
}
// 当前复数对象减去某一实数;
public ComplexNumber ComplexNumberMinus(double c) {
ComplexNumber result = new ComplexNumber();
// 复数对象的实部减去实数;
result.setRealPart(this.RealPart - c);
// 复数对象的虚部不变;
result.setImaginPart(this.ImaginPart);
return result;
}
// 当前复数对象乘以形参复数对象运算的方法;
public ComplexNumber ComplexNumberMuti(ComplexNumber c) {
ComplexNumber result = new ComplexNumber();
// 当前复数与形参复数相乘后实部的结果;
result.setRealPart(this.RealPart * c.getRealPart() - this.ImaginPart
* c.getImaginPart());
// 当前复数与形参复数相乘后虚部的结果;
result.setImaginPart(this.RealPart * c.getImaginPart()
+ this.ImaginPart * c.getRealPart());
return result;
}
// 当前复数与某一实数相乘的运算方法;
public ComplexNumber ComplexNumberMuti(double c) {
ComplexNumber result = new ComplexNumber();
result.setRealPart(this.RealPart * c);
result.setImaginPart(this.ImaginPart * c);
return result;
}
// 当前复数对象除以某一复数的运算方法;
public ComplexNumber ComplexNumberDivi(ComplexNumber c) {
ComplexNumber result = new ComplexNumber();
// 复数c中实部和虚部的平方和;
double d = c.getRealPart() * c.getRealPart() + c.getImaginPart()
* c.getImaginPart();
result.setRealPart((this.RealPart * c.getRealPart() + this.ImaginPart
* c.getImaginPart())
/ d);
result.setImaginPart((this.ImaginPart * c.getRealPart() - this.RealPart
* c.getImaginPart())
/ d);
return result;
}
// 将当前的复数对象以字符串的形式表示
public String toString() {
return "" + this.RealPart + "+" + this.ImaginPart + "i";
}
}
// 复数的四则运算在Applet程序的实现;
public class ComplexNumberDemo extends Applet implements ActionListener {
// 定义a、b、c、d四个文本框分别存储复数下x、y的实部与虚部;
TextField a, b, c, d;
Label title;
// 参加运算的复数对象x、y运算结果result的定义;
ComplexNumber x, y, result;
Button bu;
// 定义一个选择菜单,用以选择运算方法(加、减、乘、除);
Choice ch;
// applet的初始化过程;
public void init() {
a = new TextField(5);
b = new TextField(5);
c = new TextField(5);
d = new TextField(5);
ch = new Choice();
ch.add("加");
ch.add("减");
ch.add("乘");
ch.add("除");
bu = new Button("确定");
title = new Label("请输入两个复数:");
add(title);
add(a);
add(new Label("+"));
add(b);
add(new Label("i"));
add(ch);
add(c);
add(new Label("+"));
add(d);
add(new Label("i"));
add(bu);
bu.addActionListener(this);
}
// 触发事件的处理过程;
public void actionPerformed(ActionEvent e) {
// 两复数对象的初始化;
x = new ComplexNumber(Double.parseDouble(a.getText()), Double
.parseDouble(b.getText()));
y = new ComplexNumber(Double.parseDouble(c.getText()), Double
.parseDouble(d.getText()));
// ch.getSelectedIndex()方法获取选择菜单中选中的菜单的索引(该索引以0开始计算);
// 捕获选择菜单中的运算方法,并调用相应的运算方法;
switch (ch.getSelectedIndex()) {
case 0:
result = x.ComplexNumberAdd(y);
break;
case 1:
result = x.ComplexNumberMinus(y);
break;
case 2:
result = x.ComplexNumberMuti(y);
break;
case 3:
result = x.ComplexNumberDivi(y);
break;
default:
break;
}
repaint();
}
public void paint(Graphics g) {
// 运算结果的显示;
if (x != null & y != null)
g.drawString("" + ch.getSelectedItem() + "运算的结果为:" + result, 10,
100);
}
}
热心网友
时间:2023-10-30 05:25
不知道楼主要什么样的,我现做了一个,不知道是不是符合要求,有问题,请提。
代码如下:
public class L {
public static void main(String[] args) {
FS f = new FS();
f.In1(1.0, "2.0i");
f.In2(3.0, "5.0i");
f.JaFa();
f.In1(3.0, "5.0i");
f.In2(1.0, "2.0i");
f.JianFa();
}
}
class FS {
double a, a1, a2, a3, a4, a5;// 实部
String b, b1, b2;// 虚部
public void In1(double a, String b) {// 输入第一个数
this.a = a;
this.b = b;
String str = b.substring(0, b.length() - 1);
a3 = Double.parseDouble(str);
}
public void In2(double a1, String b1) {// 输入第二个数
this.a1 = a1;
this.b1 = b1;
String str = b1.substring(0, b1.length() - 1);
a4 = Double.parseDouble(str);
}
public void JaFa() {// 加法
a2 = a + a1;
a5 = a3 + a4;
b2 = a5 + "i";
System.out.println("(" + a + "+" + b + ")+(" + a1 + "+" + b1 + ")=("
+ a2 + "+" + b2 + ")");
}
public void JianFa() {// 减法
a2 = a - a1;
a5 = a3 - a4;
b2 = a5 + "i";
System.out.println("(" + a + "+" + b + ")-(" + a1 + "+" + b1 + ")=("
+ a2 + "+" + b2 + ")");
}
}
热心网友
时间:2023-10-30 05:25
太简单。