Question

In: Computer Science

I need a full java code. And I need it in GUI With the mathematics you...

I need a full java code. And I need it in GUI

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 x, 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 2x5 -15x4 + 35x3 -15x2-37x + 30 (roots are -1, 1, 2, 2.5, 3) and 3x5 -17x4 + 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 < 5.0000001; x = x + .1)

Solutions

Expert Solution

  • In above question we need to change the license header , on your editor click on properties , project, click on License header.
  • we have to change template file so it is avaliable on the tool bar
  • On editor plz try to open the template .
  • plz maitain some fixed space while writting lines of code.

plz check the code below

import java.util.Scanner;
public class BetterBisection
{

public static void main(String[] args)

{
double a, b, c; // a, b and c must have specific meaning
double f_of_a, f_of_b;   
int highest_degree;
System.out.println(“ the highest degree of your polynomial ");
Scanner input = new Scanner(System.in);
highest_degree = input.nextInt();
for (int i = highestdegree; i >= 0; i--)
{
int coeffdeg_i;
coeff deg i = poly input(i);
System.out.println(coeff deg i);
}
// The following do-while loop keeps asking the user for a and b until  f(a)f(b) does not become negative
do

{
a = input();
b = input();
if (f(a) * f(b) >= 0) {
System.out.println("provided the two numbers are not bracketing the root");
}
}
while (f(a) * f(b) >= 0);
f_of_a = f(a);
f_of_b = f(b);
double root = bisectionMethod(f_of_a, f_of_b, a, b);
System.out.println("Root is : " + root);
}

public static double input()

{

// Reads in the bracketing number i.e a and b
Scanner input = new Scanner(System.in);
System.out.println("Enter a bracketing number");
return (input.nextDouble());
}

public static double f(double num)

{

// Calculates f(x) given x and returns
// f(x)
final int COEFF_DEG_3 = 1; // Coefficient of x^3
final int COEFF_DEG_2 = 4; // Coefficient of x^2
final int COEFF_DEG_0 = -10; // Coefficient of x^0
return (COEFF_DEG_3 * Math.pow(num, 3) + COEFF_DEG_2 * Math.pow(num, 2) + COEFF_DEG_0
* Math.pow(num, 0));
}

public static double bisectionMethod(double f_of_a, double f_of_b, double a,
double b)

{

// Does the actual work of evaluating
double c; // the root using the method of bisection.
double f_of_c;
final double TOLERANCE = 0.0001;
while (Math.abs(a - b) > TOLERANCE)
{
c = (a + b) / 2;
f_of_c = f(c);
if (f_of_c * f(a) == 0 || f_of_c * f(b) == 0)
{
return c;
} else if (f_of_c * f(a) > 0) {
a = c;
} else {
b = c;
}
}
return (a + b) / 2;
}

public static int poly_input(int degree);
{
System.out.println("Please enter coefficient for degree " + degree);
Scanner input = new Scanner(System.in);
int coefficient;
coefficient = input.nextInt();
return coefficient;
}
}

  • plz use package any name
  • In java while saving the file class name and package name must be same. otherwise it thows an error.

OUTPUT:

the highest degree of your polynomial

3

please enter the cofficient for degree 3

3

please enter the cofficient for degree 2

0

please enter the cofficient for degree 1

0

please enter the cofficient for degree 0

0

Enter a bracketing number

1

Enter a bracketing number

1

provided two numbers are not bracketing the root

Enter a bracketing number



Related Solutions

JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
I need an original matlab code and gui for a simple double pendulum. This needs to...
I need an original matlab code and gui for a simple double pendulum. This needs to be original and not too complicated. Please provide basic instructions. Thank you!
very urgent !!! need it asap::::::::Write Java code which will create a GUI window on screen....
very urgent !!! need it asap::::::::Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I need a Java application with a GUI that includes the following requirements: Three classes minimum...
I need a Java application with a GUI that includes the following requirements: Three classes minimum At least one class must use inheritance At least one class must be abstract Utilization of “get set” method. (get; set; =  is related to how variables are passed between different classes. The get method is used to obtain or retrieve a particular variable value from a class. A set value is used to store the variables. The whole point of the get and set...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT