Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please only do the part 2. Here is my Polynomial program import java.util.Scanner; import java.text.DecimalFormat; public class PolynomialsProject { private static final double ACCURACY =

image text in transcribed

Please only do the part 2.

Here is my Polynomial program

import java.util.Scanner; import java.text.DecimalFormat;

public class PolynomialsProject { private static final double ACCURACY = 0.0000001;

public static double polyValue(int coeffs[], double x) { double ans = 0.0; for(int coeff : coeffs) { ans = (ans * x) + coeff; } return ans; }

public static int[] getCoeff(Scanner scn) { int[] res = new int[6]; System.out.println("Please enter the 5th Coefficient"); res[0] = scn.nextInt();

System.out.println("Please enter the 4th Coefficient"); res[1] = scn.nextInt();

System.out.println("Please enter the 3rd Coefficient"); res[2] = scn.nextInt();

System.out.println("Please enter the 2nd Coefficient"); res[3] = scn.nextInt();

System.out.println("Please enter the 1st Coefficient"); res[4] = scn.nextInt();

System.out.println("Please enter the zero Coefficient"); res[5] = scn.nextInt();

return res; }

public static double bisection(int[] coeffs, double lower, double upper) { if(polyValue(coeffs, lower) * polyValue(coeffs, upper) > 0) { return -1; } double mid = (lower + upper) / 2; while(Math.abs(polyValue(coeffs, mid)) > ACCURACY) { if(polyValue(coeffs, lower) * polyValue(coeffs, mid)

public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] coeffs = getCoeff(scn); double lastX = -5.0000001; double lastVal = polyValue(coeffs, lastX); for(double x = -5.0000001; x

} }

Here is the GUI example

/** * demonstrating a GUI program */ 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 ExampleGUI 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 questionPanel = new JPanel(); JPanel inputNumberPanel = new JPanel(); JPanel addAndSubtractButtonPanel = new JPanel(); JPanel answerPanel = new JPanel(); JPanel nextNumberPanel = 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 questionLabel = new JLabel(); JLabel inputNumberLabel = new JLabel(); JLabel add5Label = new JLabel(); JLabel subtract5Label = new JLabel(); JLabel answerLabel = new JLabel(); JLabel nextNumberLabel = 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 add5Button = new JButton(); JButton subtract5Button = new JButton(); JButton nextNumberButton = new JButton(); //*** a JTextField creates a location where the client can place //*** text JTextField inputTextField = new JTextField(15); //*** constructor //*** Variables are given initial values public ExampleGUI() { //*** set panel layouts //*** panels could be LEFT, or RIGHT justified. titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER)); questionPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); inputNumberPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); addAndSubtractButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); answerPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); nextNumberPanel.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); Font quizSmallFont = new Font("Helvetica Bold", Font.BOLD, 15); titleLabel.setFont(quizBigFont); questionLabel.setFont(quizMidFont); inputNumberLabel.setFont(quizMidFont); add5Label.setFont(quizSmallFont); subtract5Label.setFont(quizSmallFont); answerLabel.setFont(quizBigFont); nextNumberLabel.setFont(quizSmallFont); inputTextField.setFont(quizMidFont); //*** labels are given string values titleLabel.setText("Add or Subtract Five"); questionLabel.setText("Please enter an integer number."); inputNumberLabel.setText("Number:"); add5Button.setText(" Add 5 "); subtract5Button.setText("Subtract 5"); answerLabel.setText(""); nextNumberButton.setText(" New Number "); //*** the 3 buttons are connected to their classes add5Button.addActionListener(new add5Button()); subtract5Button.addActionListener(new subtract5Button()); nextNumberButton.addActionListener(new nextNumberButton()); //*** Labels, buttons and textFields are added to their //*** panels titlePanel.add(titleLabel); questionPanel.add(questionLabel); //*** inputNumberPanel has 2 items added inputNumberPanel.add(inputNumberLabel); inputNumberPanel.add(inputTextField); //*** submitPanel has two items added addAndSubtractButtonPanel.add(add5Button); addAndSubtractButtonPanel.add(subtract5Button); answerPanel.add(answerLabel); nextNumberPanel.add(nextNumberButton); //*** The panels are added in the order that they should appear. //*** Throughout the declarations and initializations variables were //*** kept in this order to aid in keeping them straight setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(titlePanel); add(questionPanel); add(inputNumberPanel); add(addAndSubtractButtonPanel); add(answerPanel); add(nextNumberPanel); //*** The method writeToFile() is called from the constructor. //*** One could call a read method from the constructor. // writeToFile(); }// constructor //*** This method writes 4 lines to a file. Eclipse puts the file in //*** the folder of the project but not in the src folder. Put any //*** file that you want read in the same place so that Eclipse can //*** find it. /* private void writeToFile() { String fileName = "textFile.txt"; String line = null; int count; Scanner scan = new Scanner(System.in); PrintWriter textStream = TextFileIO.createTextWrite(fileName); System.out.println("Enter 4 lines of text:"); for (count = 1; count  

Thank you.

With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types: people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to an equation f(x) = 0 for some function f. Use the bisection method. To solve the problem using this method first find two values of I, A and B, such that when evaluated in the function f(x) they give opposites signs for the y value. If f(x) is continuous between these two values then we know that there is at least one x which evaluates to a 0 y value, which is between these two values A and B. Treat the positive value as an upper bound and the negative value as a lower bound. Divide the space between A and B in half and evaluate the function at that new point. If the value is positive than it replaces the existing upper- bound and if it is negative it replaces the existing lower-bound. Continue dividing the space between the upper-bound and lower- bound in half and evaluating this new value and generating new upper and lower bounds as the case may be. Continue the evaluation process until the x value that you are plugging into the function evaluates to a y value that is zero plus or minus .0000001. Consider the possibility of finding all the real roots of any given function up to and including x raised to the fifth power. Input should consist of reading the coefficients one at a time to the powers of x up to 5 and some constant. Do a desk check with calculator on y=X2 -2 and identify the variables associated with that problem. Write the code that follows your algorithm. Then test your program on other polynomials such as 2x} -15x++ 35x -15x2-37x +30 (roots are -1, 1, 2, 2.5, 3) and 3x -17x+ + 25x3 + 5x2 -28x + 12 (roots are -1,1, 2/3, 2, 3). Use at lest 3 methods. One to read the 5 coefficients, one to calculate the value of the polynomial and one to do the binary bisection search. Use the following for loop to work through the X values:for(double x=-5.0000001; x

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions