Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Polynomial Bisection Help

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Polynomial Bisection Help

    Hello guys. I'm trying to make a bisection method in my program I keep getting an error that tells me that I can't reference that method because I can't access a non-static method bisection can't be referenced from a static context. That basically means that I can't access the method unless I make my arrays static but that changes the values from within as well. Its easier to explain if you try running it and changing the accessibility. Also, I don;t know if I'm even doing the Bisection method right or not. I've spent days on this program, is there something I can do to fix this without starting all over?

                import java.util.Scanner;
     
                public class Polynomial {
     
                         int [] coefficients = new int[3];
                         int [] powers = new int[3];
     
     
                      public static void main(String[] args)
                      {
                      int c1, c2, c3;
                      Scanner input = new Scanner(System.in);
     
                      /*for(int i = 0; i<coefficents.length-1;i++)
                      {
     
     
     
     
                      }
                      */
     
                         Polynomial P1 = new Polynomial(4, 5, -3, 3, 1, 0);
                         Polynomial P2 = new Polynomial(1, 1, -2, 3, 1, 0);
     
                         System.out.println("Here is the Polynomial 1: " + P1);
                			System.out.println("Here is the Polynomial 2: " + P2);
                         System.out.println("P1 eval = " + P1.evalAt(-1));
                         System.out.println("P2 eval = " + P2.evalAt(1));
     
                         System.out.println("Bisection " + bisection(P1, P2));
                			System.out.println("================================================");
     
                         P1.derivative();
                         System.out.println("Here is the Derivative for P1: " + P1);
                         P2.derivative();
                         System.out.println("Here is the Derivative for P2: " + P2);
     
     
     
     
     
                      }
     
                  Polynomial(int a, int b, int c, int x, int y, int z)
                  {
                     coefficients[0] = a;
                     coefficients[1] = b;
                     coefficients[2] = c;
     
                     powers[0] = x;
                     powers[1] = y;
                     powers[2] = z;
                  }
     
                  public static int evalAt(int x)
                  {
                   int sum = 0;
                   for(int i = coefficients.length-1; i>=0;i--)
                     {
                       sum = coefficients[i] + x * sum;
                       //System.out.print(sum + "\t");
                     }
                      return sum;
                  }
     
                  public void derivative() // Evaluates the Derivative
                  { 
                   for(int i = 0; i<=2;i++)
                      { 
                       coefficients[i] = powers[i] * coefficients[i];
                       powers[i]--;
                      }
                  }
     
                  public double bisection(Polynomial a, Polynomial b)  
                  {
                   double tolerance = 0.000001;
                  double m, ya, yb;
                  double result = 0;
                  int i = 0;
     
                  double A = a.evalAt(1); 
                  double B = b.evalAt(2);
     
                  while( B-A > tolerance)
                   {
                      m = (A+B)/2;
     
                      ya = m * m - coefficients[2];
                      yb = A * A - coefficients[2];
     
                      if(ya > 0 && ya < 0 ||  yb <0  && yb > 0 )
                       {
                        B =  m;
                       }
                      else
                      {
                      A = m;   
                     }
                     System.out.println((A+B)/2);
                   }
                   result = (A+B)/2;
                   return result;
     
                 }
     
     
                 public  String toString()
                 {
                  String s = "";
                     for(int i=0;i<coefficients.length;i++)
                     {
                     if(powers[i]==0)
                      {
                        s = s + "+" + coefficients[i];
     
                      }
                      else if(powers[i]>0)
                     s = s  + "+" + (coefficients[i]);
                      else if(powers[i]<0)
                    {
                     s = s + "-" + (-coefficients[i]);
                    }
                    if(i == 1)
                    s = s + "x^" + powers[i];
                    else if(i > 1)
                    s = s + "x^" + powers[i];  
                    //else if(i == coefficients.length-1)
     
                    else
                    s =  coefficients[i] + "x^" + powers[i];
                    }
                  return s;
              }  
            }


    Sample:


    Polynomial.java:31: error: non-static method bisection(Polynomial,Polynomial) cannot be referenced from a static context
                         System.out.println("Bisection " + bisection(P1, P2));
                                                           ^
    Polynomial.java:59: error: non-static variable coefficients cannot be referenced from a static context
                   for(int i = coefficients.length-1; i>=0;i--)
                               ^
    Polynomial.java:61: error: non-static variable coefficients cannot be referenced from a static context
                       sum = coefficients[i] + x * sum;
                             ^
    3 errors


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Polynomial Bisection Help

    The static method main() can NOT access any non-static methods or variables in the class. non-static methods and variables are only accessible to methods in instances of the class.
    Solutions:
    1) Move the code that access the methods out of main to a method in the class. Have main() create an instance of the class and use the reference to the class to call the method.
    2) Make everything static so no instance of the class is required.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Polynomial Bisection Help

    Quote Originally Posted by Norm View Post
    The static method main() can NOT access any non-static methods or variables in the class. non-static methods and variables are only accessible to methods in instances of the class.
    Solutions:
    1) Move the code that access the methods out of main to a method in the class. Have main() create an instance of the class and use the reference to the class to call the method.
    2) Make everything static so no instance of the class is required.
    Yeah, youre right. I had to make my bisection( ) method an instance of my polynomial class. I stored the evalAt ( ) object results into a variable in which I was able to use as an argument to pass through my bisection method. No changes affected on my array. Since I was able to pass everything correctly, I can focus more on getting the overall Bisection method correctly.

    Here is my fully functional program: Do keep in mind that the toString ( ) shows +- or --. Ill work on that later.

                import java.util.Scanner;
     
                public class Polynomial {
     
                         int [] coefficients = new int[3];
                         int [] powers = new int[3];
     
     
                      public static void main(String[] args)
                      {
                      int c1, c2, c3;
                      Scanner input = new Scanner(System.in);
     
                      /*for(int i = 0; i<coefficents.length-1;i++)
                      {
     
     
     
     
                      }
                      */
     
                         Polynomial P1 = new Polynomial(4, 5, -3, 3, 1, 0);
                         Polynomial P2 = new Polynomial(1, 1, -2, 3, 1, 0);
     
                         System.out.println("Here is the Polynomial 1: " + P1);
                			System.out.println("Here is the Polynomial 2: " + P2);
                         int c = P1.evalAt(5);
                         int d = P2.evalAt(0);
                         System.out.println("P1 eval = " + c);
                         System.out.println("P2 eval = " + d);
     
                         System.out.println("Bisection " + P1.bisection(c, d));
                			System.out.println("================================================");
     
                         P1.derivative();
                         System.out.println("Here is the Derivative for P1: " + P1);
                         P2.derivative();
                         System.out.println("Here is the Derivative for P2: " + P2);
     
     
     
     
     
                      }
     
                  Polynomial(int a, int b, int c, int x, int y, int z)
                  {
                     coefficients[0] = a;
                     coefficients[1] = b;
                     coefficients[2] = c;
     
                     powers[0] = x;
                     powers[1] = y;
                     powers[2] = z;
                  }
     
                  public int evalAt(int x)
                  {
                   int sum = 0;
                   for(int i = coefficients.length-1; i>=0;i--)
                     {
                       sum = coefficients[i] + x * sum;
                       //System.out.print(sum + "\t");
                     }
                      return sum;
                  }
     
                  public void derivative() // Evaluates the Derivative
                  { 
                   for(int i = 0; i<=2;i++)
                      { 
                       coefficients[i] = powers[i] * coefficients[i];
                       powers[i]--;
                      }
                  }
     
                  public double bisection(double a, double b)  
                  {
                  double tolerance = 0.000001;
                  double m, ya, yb;
                  double result = 0;
                  int i = 0;
     
                  double A = a;
                  double B = b;
     
                  while( B-A > tolerance)
                   {
                      m = (A+B)/2;
     
                      ya = m * m - coefficients[2];
                      yb = A * A - coefficients[2];
     
                      if(ya > 0 && ya < 0 ||  yb <0  && yb > 0 )
                       {
                        B =  m;
                       }
                      else
                      {
                      A = m;   
                     }
                     System.out.println((A+B)/2);
                   }
                   result = (A+B)/2;
                   return result;
     
                 }
     
     
                 public  String toString()
                 {
                  String s = "";
                     for(int i=0;i<coefficients.length;i++)
                     {
                     if(powers[i]==0)
                      {
                        s = s + "+" + coefficients[i];
     
                      }
                      else if(powers[i]>0)
                     s = s  + "+" + (coefficients[i]);
                      else if(powers[i]<0)
                    {
                     s = s + "-" + (-coefficients[i]);
                    }
                    if(i == 1)
                    s = s + "x^" + powers[i];
                    else if(i > 1)
                    s = s + "x^" + powers[i];  
                    //else if(i == coefficients.length-1)
     
                    else
                    s =  coefficients[i] + "x^" + powers[i];
                    }
                  return s;
              }  
            }

     ----jGRASP exec: java Polynomial
     
    Here is the Polynomial 1: 4x^3+5x^1+-3x^0
    Here is the Polynomial 2: 1x^3+1x^1+-2x^0
    P1 eval = -46
    P2 eval = 1
    -10.75
    -4.875
    -1.9375
    -0.46875
    0.265625
    0.6328125
    0.81640625
    0.908203125
    0.9541015625
    0.97705078125
    0.988525390625
    0.9942626953125
    0.99713134765625
    0.998565673828125
    0.9992828369140625
    0.9996414184570312
    0.9998207092285156
    0.9999103546142578
    0.9999551773071289
    0.9999775886535645
    0.9999887943267822
    0.9999943971633911
    0.9999971985816956
    0.9999985992908478
    0.9999992996454239
    0.999999649822712
    Bisection 0.999999649822712
    ================================================
    Here is the Derivative for P1: 12x^2+5x^0-0x^-1
    Here is the Derivative for P2: 3x^2+1x^0-0x^-1

Similar Threads

  1. Polynomial Array List
    By Pinares in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 18th, 2013, 10:30 AM
  2. Urgent Polynomial Assignment !!!!!!!!!
    By thesoulpatchofBruce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2012, 01:38 PM
  3. Solving a polynomial
    By arvindbis in forum Java Theory & Questions
    Replies: 9
    Last Post: March 8th, 2012, 12:16 AM
  4. Help with Polynomial Graph Plotter and its scaling
    By sojharo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 23rd, 2011, 06:04 PM
  5. API polynomial java
    By mcherkao in forum Java SE APIs
    Replies: 1
    Last Post: September 10th, 2010, 08:39 AM