java编程 麻烦大神们写个完整的给我 急!!!

发布网友 发布时间:2022-04-22 22:56

我来回答

1个回答

热心网友 时间:2023-06-27 04:38

复数类:
/**
*复数类
* @author sunsnowad
*/
public class Plural {
double real, unreal;
public Plural(double real, double unreal){
this.real = real;
this.unreal = unreal;
}

private Plural() {
real = 0.0;
unreal = 0.0;
}

public Plural add(Plural a, Plural b){
return new Plural(a.getReal()+b.getReal(), a.getUnreal()+b.getUnreal());
}

public Plural minus(Plural a, Plural b){
return new Plural(a.getReal()-b.getReal(), a.getUnreal()-b.getUnreal());
}

public Plural multi(Plural a, Plural b){
return new Plural(a.getReal()*b.getReal()-a.getUnreal()*b.getUnreal(),
a.getUnreal()*b.getReal() + a.getReal()*b.getUnreal());
}

public Plural division(Plural a, Plural b){
double middleResult = a.getReal()*a.getReal() + a.getUnreal()*b.getUnreal();
double realResult = (a.getReal()*b.getReal()+a.getUnreal()*b.getUnreal())/middleResult;
double unrealResult = (a.getUnreal()*b.getReal()-a.getReal()*b.getUnreal())/middleResult;
return new Plural(realResult, unrealResult);
}

public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public double getUnreal() {
return unreal;
}

public void setUnreal(double unreal) {
this.unreal = unreal;
}

@Override
public String toString(){
return real + "+" + unreal + "i";
}

public static Plural parsePlural(String plural){
plural = plural.trim();
String realString = "";
String unrealString = "";
if(plural.contains("i")){
for(int i = 0; i < plural.length(); i ++){
if(plural.charAt(i) == '+'){
for(int j = i + 1; j < plural.length()-1; j ++){
unrealString += plural.charAt(j);
}
break;
}
realString+=plural.charAt(i);
}
}
//TODO else
return new Plural(Double.parseDouble(realString), Double.parseDouble(unrealString));
}
}

Applet测试程序(使用Netbeans设计工具):

/**
*
* @author sunsnowad
*/
public class PluralTest extends javax.swing.JApplet {

/** Initializes the applet PluralTest */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

jLabel1.setText("实数1:");

jTextField1.setText("4+5i");

jLabel2.setText("实数2:");

jTextField2.setText("5+4.4i");

jButton1.setText("加");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("减");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("乘");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("除");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jLabel3.setText("结果:");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField2)))
.addComponent(jButton4)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE))
.addComponent(jButton1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(122, Short.MAX_VALUE))
);
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Plural a = Plural.parsePlural(jTextField1.getText());
Plural b = Plural.parsePlural(jTextField2.getText());
jLabel3.setText(a.add(a, b).toString());
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Plural a = Plural.parsePlural(jTextField1.getText());
Plural b = Plural.parsePlural(jTextField2.getText());
jLabel3.setText(a.minus(a, b).toString());
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Plural a = Plural.parsePlural(jTextField1.getText());
Plural b = Plural.parsePlural(jTextField2.getText());
jLabel3.setText(a.multi(a, b).toString());
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
Plural a = Plural.parsePlural(jTextField1.getText());
Plural b = Plural.parsePlural(jTextField2.getText());
jLabel3.setText(a.division(a, b).toString());
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

}

Application 测试类和Applet类似。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com