0

このクラスは、JPanel GUI から入力を読み取ります。各係数に 1 を入力しない限り、何も SOP しません。次に、「0.0」と出力するだけです。これを機能させる方法に関する推奨事項はありますか? 考えられることはすべて試しました。

public class PolyRoots extends PolyGUI {

private double coefficientToFifthPower;
private double coefficientToFourthPower;
private double coefficientToThirdPower;
private double coefficientToSecondPower;
private double coefficientToFirstPower;
private double constant;
private double x;

public double getCoefficientToFifthPower() {
    return coefficientToFifthPower;
}

public void setCoefficientToFifthPower(double coefficientToFifthPower) {
    this.coefficientToFifthPower = coefficientToFifthPower;
}

public double getCoefficientToFourthPower() {
    return coefficientToFourthPower;
}

public void setCoefficientToFourthPower(double coefficientToFourthPower) {
    this.coefficientToFourthPower = coefficientToFourthPower;
}

public double getCoefficientToThirdPower() {
    return coefficientToThirdPower;
}

public void setCoefficientToThirdPower(double coefficientToThirdPower) {
    this.coefficientToThirdPower = coefficientToThirdPower;
}

public double getCoefficientToSecondPower() {
    return coefficientToSecondPower;
}

public void setCoefficientToSecondPower(double coefficientToSecondPower) {
    this.coefficientToSecondPower = coefficientToSecondPower;
}

public double getCoefficientToFirstPower() {
    return coefficientToFirstPower;
}

public void setCoefficientToFirstPower(double coefficientToFirstPower) {
    this.coefficientToFirstPower = coefficientToFirstPower;
}

public double getConstant() {
    return constant;
}

public void setConstant(double constant) {
    this.constant = constant;
}

private double y;

public void readInputCoefficients() {

/*        this.coefficientToFifthPower = Integer.parseInt(inputCoefficientFifthPower);

    this.coefficientToFourthPower = Integer.parseInt(inputCoefficientFourthPower);

    this.coefficientToThirdPower = Integer.parseInt(inputCoefficientThirdPower);
    this.coefficientToSecondPower = Integer.parseInt(inputCoefficientSecondPower);

    this.coefficientToFirstPower = Integer.parseInt(inputCoefficientFirstPower);

    this.constant = Integer.parseInt(inputConstant);*/
}

public double calculateY(double x) {
    this.y = this.coefficientToFifthPower * Math.pow(x, 5) + this.coefficientToFourthPower * Math.pow(x, 4) +
            this.coefficientToThirdPower * Math.pow(x, 3) + this.coefficientToSecondPower * Math.pow(x, 2) + (this.coefficientToFirstPower * x) + this.constant;
    return this.y;
}

public void getTheBounds() {
    for (double x = -10.0001; x <= 10.0001; x += .1) {

        double y1 = calculateY(x);
        double y2 = calculateY(x + .01);

        if ((y1 * y2) < 0) {
            System.out.println(bisectionMethod(y1, y2));
        }
        else if((y1 * y2) > 0){
            System.out.println(bisectionMethod(y1,y2));
        }
    }

}

public double bisectionMethod(double a, double b) {
    double average;
    double root = 0;
    double yOfC;
    int leaveloop = 0;
    for(int x = 0; x <= 10000000; x++) {
        average = (a + b) / 2;
        yOfC = calculateY(average);
        if (Math.abs(yOfC) < 0.0001) {
            root = average;
            return root;
        } else if (yOfC * calculateY(a) > 0) {
            a = average;
        } else {
            b = average;
        }
    }
    System.out.println(root);
    return root;
}

}

GUI

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class PolyGUI extends JPanel
{
// ***Variables are created ***
//*** GUIs are made up of JPanels.  Panels are created
//*** here and named appropriately to describe what will
//*** be placed in each of them.
JPanel titlePanel = new JPanel();
JPanel c5Panel = new JPanel();
JPanel c4Panel = new JPanel();
JPanel c3Panel = new JPanel();
JPanel c2Panel = new JPanel();
JPanel c1Panel = new JPanel();
JPanel cPanel = new JPanel();
JPanel calculatePanel = new JPanel();

//*** a JLabel is a text string that is given a String value
//*** and is placed in its corresponding JPanel or JButton
JLabel titleLabel = new JLabel();
JLabel c5Label = new JLabel();
JLabel c4Label = new JLabel();
JLabel c3Label = new JLabel();
JLabel c2Label = new JLabel();
JLabel c1Label = new JLabel();
JLabel cLabel = new JLabel();
JLabel questionLabel = new JLabel();
JLabel calculateLabel = new JLabel();
//*** three JButtons are created.  When pushed, each button calls
//*** its corresponding actionPerformed() method from the class created
//*** for each button. This method executes the method code, performing
//*** what the button is to do.
JButton calculateButton = new JButton();
//*** a JTextField creates a location where the client can place
//*** text
JTextField x5Txt = new JTextField(8);
JTextField x4Txt = new JTextField(8);
JTextField x3Txt = new JTextField(8);
JTextField x2Txt = new JTextField(8);
JTextField xTxt = new JTextField(8);
JTextField constantTxt = new JTextField(8);

//*** constructor
//*** Variables are given initial values

public PolyGUI()
{
    //*** set panel layouts
    //*** panels could be LEFT, or RIGHT justified.
    titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    c5Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    c4Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    c3Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    c2Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    c1Panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    cPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    questionLabel.setLayout(new FlowLayout(FlowLayout.CENTER));
    calculateButton.setLayout(new FlowLayout(FlowLayout.CENTER));


    //*** set Label fonts.  You can use other numbers besides 30,20
    //*** or 15 for the font size.  There are other fonts.
    Font quizBigFont = new Font("Helvetica Bold", Font.BOLD, 30);
    Font quizMidFont = new Font("Helvetica Bold", Font.BOLD, 20);
    titleLabel.setFont(quizBigFont);
    questionLabel.setFont(quizMidFont);
    c5Label.setFont(quizMidFont);
    c4Label.setFont(quizMidFont);
    c3Label.setFont(quizMidFont);
    c2Label.setFont(quizMidFont);
    c1Label.setFont(quizMidFont);
    cLabel.setFont(quizMidFont);
    //*** labels are given string values
    titleLabel.setText("Polynomial Project");
    questionLabel.setText("Please enter the coefficients");
    c5Label.setText("5");
    c4Label.setText("4");
    c3Label.setText("3");
    c2Label.setText("2");
    c1Label.setText("1");
    cLabel.setText("constant");
    calculateButton.setText("Calculate");
    calculateButton.addActionListener(new calculate());
    //*** panels
    titlePanel.add(titleLabel);
    c5Panel.add(c5Label);
    c5Panel.add(x5Txt);
    c4Panel.add(c4Label);
    c4Panel.add(x4Txt);
    c3Panel.add(c3Label);
    c3Panel.add(x3Txt);
    c2Panel.add(c2Label);
    c2Panel.add(x2Txt);
    c1Panel.add(c1Label);
    c1Panel.add(xTxt);
    cPanel.add(cLabel);
    cPanel.add(constantTxt);
    calculatePanel.add(calculateButton);
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    add(titlePanel);
    add(questionLabel);
    add(c5Panel);
    add(c4Panel);
    add(c3Panel);
    add(c2Panel);
    add(c1Panel);
    add(cPanel);
    add(calculatePanel);


    //*** The method writeToFile() is called from the constructor.
    //*** One could call a read method from the constructor.

    //  writeToFile();
}// constructor



public void display()
{   //*** A JFrame is where the components of the screen
    //*** will be put.
    JFrame theFrame = new JFrame("GUI Example");
    //*** When the frame is closed it will exit to the
    //*** previous window that called it.
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //*** puts the panels in the JFrame
    theFrame.setContentPane(this);
    //*** sets the dimensions in pixels
    theFrame.setPreferredSize(new Dimension(600, 380));
    theFrame.pack();
    //*** make the window visible
    theFrame.setVisible(true);
}



class calculate implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Calculating...");
        PolyRoots p1 = new PolyRoots();

        double x5 = Integer.parseInt(x5Txt.getText());
        double x4 = Integer.parseInt(x4Txt.getText());
        double x3 = Integer.parseInt(x3Txt.getText());
        double x2 = Integer.parseInt(x2Txt.getText());
        double x = Integer.parseInt(xTxt.getText());
        double constant = Integer.parseInt(constantTxt.getText());

        p1.setCoefficientToFifthPower(x5);
        p1.setCoefficientToFourthPower(x4);
        p1.setCoefficientToThirdPower(x3);
        p1.setCoefficientToSecondPower(x2);
        p1.setCoefficientToFirstPower(x);
        p1.setConstant(constant);

        p1.getTheBounds();


    }
}


public static void main(String[] args) throws IOException
{
    PolyGUI newGUI = new PolyGUI();
    newGUI.display();

}

}

4

1 に答える 1

0

これは、 を持つプログラムの簡単な例で、mainコードの各行が何をするかを見ることができます。

public class PolyRoots {
    private final double[] coeffs; // any number of coefficients.

    public PolyRoots(double... coeffs) {
        this.coeffs = coeffs;
    }

    public double calcY(double x) {
        double ret = coeffs[0];
        for (int i = 1; i < coeffs.length; i++)
            ret = ret * x + coeffs[i];
        return ret;
    }

    public static void main(String[] args) {
        PolyRoots roots = new PolyRoots(1e-3, -2e-2, 0.1, -1, 5);
        for (int i = -10_000; i <= 10_000; i++) {
            double x = i / 1000.0;
            double y = roots.calcY(x);
            System.out.println(x + "\t" + y);
        }
    }
}

このようなプログラムに追加できますが、getBoundsこれらbisectionMethodのメソッドは想定どおりに実行されます (なぜなら、彼らが何をしようとしているのかさえ理解できないためですが、私は 30 年しか開発していません ;)

于 2016-04-16T19:42:58.437 に答える