GUI Help I need to write a program that finds the zeros of a function Here is th
ID: 3568324 • Letter: G
Question
GUI Help I need to write a program that finds the zeros of a function Here is the code:
import java.util.Scanner;
import java.text.*;
public class Polynomials
{
public static void main (String [] args)
{
Polynomials.readCoefficent();
Polynomials.calcuate();
}
private static double upper;
private static double lower;
private static int x5, x4, x3, x2, x1, x0;
private static int num;
private static double precision = 0.000001;
public static void readCoefficent()
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the numbers of coefficent: ");
num = keyboard.nextInt();
if (num == 0)
{
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x0);
}
else if (num == 1)
{
System.out.println("Enter the coefficent of x1 = ");
x1 = keyboard.nextInt();
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x1 + "x +" + x0);
}
else if (num == 2)
{
System.out.println("Enter the coefficent of x2 = ");
x2 = keyboard.nextInt();
System.out.println("Enter the coefficent of x1 = ");
x1 = keyboard.nextInt();
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x2 + "x^2 + "+ x1 + "x + " + x0);
}
else if (num == 3)
{
System.out.println("Enter the coefficent of x3 = ");
x3 = keyboard.nextInt();
System.out.println("Enter the coefficent of x2 = ");
x2 = keyboard.nextInt();
System.out.println("Enter the coefficent of x1 = ");
x1 = keyboard.nextInt();
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x3 + "x^3 + " + x2 + "x^2 + "+ x1 + "x + " + x0);
}
else if (num == 4)
{
System.out.println("Enter the coefficent of x4 = ");
x4 = keyboard.nextInt();
System.out.println("Enter the coefficent of x3 = ");
x3 = keyboard.nextInt();
System.out.println("Enter the coefficent of x2 = ");
x2 = keyboard.nextInt();
System.out.println("Enter the coefficent of x1 = ");
x1 = keyboard.nextInt();
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x4 + "x^4 + " + x3 + "x^3 + " + x2 + "x^2 + "+ x1 + "x + " + x0);
}
else if (num == 5)
{
System.out.println("Enter the coefficent of x5 = ");
x5 = keyboard.nextInt();
System.out.println("Enter the coefficent of x4 = ");
x4 = keyboard.nextInt();
System.out.println("Enter the coefficent of x3 = ");
x3 = keyboard.nextInt();
System.out.println("Enter the coefficent of x2 = ");
x2 = keyboard.nextInt();
System.out.println("Enter the coefficent of x1 = ");
x1 = keyboard.nextInt();
System.out.println("Enter the coefficent of x0 = ");
x0 = keyboard.nextInt();
System.out.println("the equation is:");
System.out.println("y= " + x5 + "x^5 + " + x4 + "x^4 + " + x3 + "x^3 + " + x2 + "x^2 + "+ x1 + "x + " + x0);
}
else
{
System.out.println("Error, enter numbers between 0-5");
System.exit(0);
}
}
public static void calcuate()
{
System.out.println("Roots: ");
DecimalFormat dt = new DecimalFormat("###.##");
for (double x = -10; x < 10 ; x =+ x + 0.01)
{
double y1 = evaluate(x);
double y2 = evaluate(x+ 0.01);
double m;
if (((y1 > 0)&&(y2 < 0))||((y1 < 0)&&(y2 > 0)))
{
if (y1 > 0)
{
setUpper(x);
setLower(x+precision);
}
else
{
setUpper(x+precision);
setLower(x);
}
m = (getUpper() + getLower())/2;
if (m > 0)
{
setUpper(m);
}
else
{
setLower(m);
}
System.out.println(dt.format(x));
}
}
}
private static void setLower(double d)
{
lower = d;
}
private static void setUpper(double x)
{
upper = x;
}
public static double getUpper()
{
return upper;
}
public static double getLower()
{
return lower;
}
private static double evaluate(double x)
{
//put # in the equation
double evEquation = (x5 * Math.pow(x, 5)) + (x4 * Math.pow(x, 4)) + (x3 * Math.pow(x, 3)) + (x2 * Math.pow(x, 2)) + (x1 * Math.pow(x, 1)) + (x0 * Math.pow(x, 0)) ;
return evEquation;} }
________________________________________________________
Now I have to make a GUI but i'm not sure how to write the getRootsButton method Here is my attempt
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class PolynomialsGUI extends JPanel
{
private static int[] coefficients;
private static final double ACCURACY = 0.0000001;
private static int x5, x4, x3, x2, x1, x0;
JPanel titlePanel = new JPanel();
JPanel questionPanel = new JPanel();
JPanel inputNumberPanel1 = new JPanel();
JPanel inputNumberPanel2 = new JPanel();
JPanel inputNumberPanel3 = new JPanel();
JPanel inputNumberPanel4 = new JPanel();
JPanel inputNumberPanel5 = new JPanel();
JPanel inputNumberPanel6 = new JPanel();
JPanel answerPanel = new JPanel();
JPanel getRootsPanel = new JPanel();
JLabel titleLabel = new JLabel();
JLabel questionLabel = new JLabel();
JLabel inputNumberLabel1 = new JLabel();
JLabel inputNumberLabel2 = new JLabel();
JLabel inputNumberLabel3 = new JLabel();
JLabel inputNumberLabel4 = new JLabel();
JLabel inputNumberLabel5 = new JLabel();
JLabel inputNumberLabel6 = new JLabel();
JLabel answerLabel = new JLabel();
JLabel getRootsLabel = new JLabel();
JButton getRootsButton = new JButton();
JTextField inputTextField = new JTextField(15);
JTextField inputTextField2 = new JTextField(15);
JTextField inputTextField3 = new JTextField(15);
JTextField inputTextField4 = new JTextField(15);
JTextField inputTextField5 = new JTextField(15);
JTextField inputTextField6 = new JTextField(15);
public PolynomialsGUI()
{
titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
questionPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel4.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel5.setLayout(new FlowLayout(FlowLayout.CENTER));
inputNumberPanel6.setLayout(new FlowLayout(FlowLayout.CENTER));
answerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
getRootsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
Font quizBigFont = new Font("Helvetica Bold", Font.BOLD, 30);
Font quizMidFont = new Font("Helvetica Bold", Font.BOLD, 20);
Font quizSmallFont = new Font("Helvetica Bold", Font.BOLD, 15);
titleLabel.setFont(quizBigFont);
questionLabel.setFont(quizMidFont);
inputNumberLabel1.setFont(quizMidFont);
inputNumberLabel2.setFont(quizMidFont);
inputNumberLabel3.setFont(quizMidFont);
inputNumberLabel4.setFont(quizMidFont);
inputNumberLabel5.setFont(quizMidFont);
inputNumberLabel6.setFont(quizMidFont);
answerLabel.setFont(quizSmallFont);
getRootsLabel.setFont(quizSmallFont);
inputTextField.setFont(quizMidFont);
inputTextField2.setFont(quizMidFont);
inputTextField3.setFont(quizMidFont);
inputTextField4.setFont(quizMidFont);
inputTextField5.setFont(quizMidFont);
inputTextField6.setFont(quizMidFont);
titleLabel.setText("Polynomials");
questionLabel.setText("Enter the coefficients of the polynomial");
inputNumberLabel1.setText("coefficient 1:");
inputNumberLabel2.setText("coefficient 2:");
inputNumberLabel3.setText("coefficeint 3:");
inputNumberLabel4.setText("coefficient 4:");
inputNumberLabel5.setText("coefficient 5:");
inputNumberLabel6.setText("coefficient 6:");
answerLabel.setText(" Roots are ");
getRootsButton.setText(" Get Roots ");
titlePanel.add(titleLabel);
questionPanel.add(questionLabel);
inputNumberPanel1.add(inputNumberLabel1);
inputNumberPanel1.add(inputTextField);
inputNumberPanel2.add(inputNumberLabel2);
inputNumberPanel2.add(inputTextField2);
inputNumberPanel3.add(inputNumberLabel3);
inputNumberPanel3.add(inputTextField3);
inputNumberPanel4.add(inputNumberLabel4);
inputNumberPanel4.add(inputTextField4);
inputNumberPanel5.add(inputNumberLabel5);
inputNumberPanel5.add(inputTextField5);
inputNumberPanel6.add(inputNumberLabel6);
inputNumberPanel6.add(inputTextField6);
answerPanel.add(answerLabel);
getRootsPanel.add(getRootsButton);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(titlePanel);
add(questionPanel);
add(inputNumberPanel1);
add(inputNumberPanel2);
add(inputNumberPanel3);
add(inputNumberPanel4);
add(inputNumberPanel5);
add(inputNumberPanel6);
add(getRootsPanel);
add(answerPanel);
}
public class getRootsButton implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
}
}
public void display()
{
JFrame theFrame = new JFrame("GUI Example");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.setContentPane(this);
theFrame.setPreferredSize(new Dimension(600, 500));
theFrame.pack();
theFrame.setVisible(true);
}
private void doSomething()
{
for(int x = 1; x <= 10; x++)
System.out.println(" in doSomething method x is " + x);
}
public static void main(String[] args) throws IOException
{
PolynomialsGUI gameGUI = new PolynomialsGUI();
System.out.println("we can print to the console");
gameGUI.display();
}
}
Explanation / Answer
100% Fresh answer, guaranteed to get you best grades possible. Just check it out: http://programmingtutor3.tk/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.