3 Replies - 6777 Views - Last Post: 07 March 2008 - 12:09 AM Rate Topic: -----

#1 Laorik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-November 07

Error: non-static variable this cannot be referenced from a static con

Posted 06 March 2008 - 08:18 PM

I'm creating a java program that stores a polynomial and does things to it, i keep getting the error "non-static variable this cannot be referenced from a static context" when i compile and it points to the creation of p1. Anyone know whats wrong or how to fix it?

 import java.lang.Math;
import java.util.*;
import java.lang.StringBuffer;
import java.io.*;

public class TestPolynomial
{
public static void main(String[] args)
{
	//Scanner
	Scanner scan = new Scanner(System.in);
	int trigger = 0;
	int coefficient;
	int degree;
	IntegerPolynomialArrayBased p1 = new IntegerPolynomialArrayBased();
	while(degree > -1)
	{
	degree = scan.nextInt();
	if(degree >= 0)
	{coefficient = scan.nextInt();
	p1.changeCoefficient(coefficient, degree);
	}
	}
}
public interface IntegerPolynomialInterface{
	public int degree();
	public int getCoefficient(int deg);
	public void changeCoefficient(int cof, int deg);
	public void add(IntegerPolynomialArrayBased p2);
	public String toString();

}
public class IntegerPolynomialArrayBased implements IntegerPolynomialInterface{
	
	private static final int MAX_POLY_SIZE = 51;
	public int coefficients[]; // array of coefficients
	
	public IntegerPolynomialArrayBased(){
		coefficients = new int[MAX_POLY_SIZE];
	} //default constructor
	
	public int degree(){
		int j = 0;
		for(int i = MAX_POLY_SIZE - 1; i >= 0; i--)
		{
			
			if(coefficients[i] != 0 || i == 0)
				{
				j = i;
				}
		}
		return j;
	}
	
	public int getCoefficient(int deg){
		return coefficients[deg];
	}

	public void changeCoefficient(int cof, int deg){
		coefficients[deg] = cof;
	}
	
	public void add(IntegerPolynomialArrayBased p2){
	
		int count = 0;
		while(count < MAX_POLY_SIZE){
			coefficients[count] = coefficients[count] + p2.getCoefficient(count);
			}
	}
	
	public String toString(){
		int firstPrintCheck = 0;
		StringBuilder poly = new StringBuilder();
		for(int j = MAX_POLY_SIZE - 1; j > 0; j--)
		{
			if(coefficients[j] > 0)
			{
				if(firstPrintCheck == 0){
				poly.append(coefficients[j] + " x^" + j + " ");
				firstPrintCheck++;
				}
				else
				poly.append("+ " + coefficients[j] + " x^" + j + " ");
			}
			else if(coefficients[j] < 0)
			{
				if(firstPrintCheck == 0){
				poly.append(coefficients[j] + " x^" + j + " ");
				firstPrintCheck++;
				}
				else
				poly.append("- " + coefficients[j] + " x^" + j + " ");
			}
		}
		if(firstPrintCheck == 0)
			poly.append(coefficients[0]);
		else if(coefficients[0] > 0)
			poly.append("+ " + coefficients[0]);
		else if(coefficients[0] < 0)
			poly.append("- " + coefficients[0]);
			
		return poly.toString();
	}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error: non-static variable this cannot be referenced from a static con

#2 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: Error: non-static variable this cannot be referenced from a static con

Posted 06 March 2008 - 11:27 PM

hi,

use this code and enjoy.

import java.util.Scanner;

interface IntegerPolynomialInterface{
    public int degree();
    public int getCoefficient(int deg);
    public void changeCoefficient(int cof, int deg);
    public void add(IntegerPolynomialArrayBased p2);
    public String toString();

}
class IntegerPolynomialArrayBased implements IntegerPolynomialInterface{

    private static final int MAX_POLY_SIZE = 51;
    public int coefficients[]; // array of coefficients

    public IntegerPolynomialArrayBased(){
        coefficients = new int[MAX_POLY_SIZE];
    } //default constructor

    public int degree(){
        int j = 0;
        for(int i = MAX_POLY_SIZE - 1; i >= 0; i--)
        {

            if(coefficients[i] != 0 || i == 0)
                {
                j = i;
                }
        }
        return j;
    }

    public int getCoefficient(int deg){
        return coefficients[deg];
    }

    public void changeCoefficient(int cof, int deg){
        coefficients[deg] = cof;
    }

    public void add(IntegerPolynomialArrayBased p2){

        int count = 0;
        while(count < MAX_POLY_SIZE){
            coefficients[count] = coefficients[count] + p2.getCoefficient(count);
            }
    }

    public String toString(){
        int firstPrintCheck = 0;
        StringBuilder poly = new StringBuilder();
        for(int j = MAX_POLY_SIZE - 1; j > 0; j--)
        {
            if(coefficients[j] > 0)
            {
                if(firstPrintCheck == 0){
                poly.append(coefficients[j] + " x^" + j + " ");
                firstPrintCheck++;
                }
                else
                poly.append("+ " + coefficients[j] + " x^" + j + " ");
            }
            else if(coefficients[j] < 0)
            {
                if(firstPrintCheck == 0){
                poly.append(coefficients[j] + " x^" + j + " ");
                firstPrintCheck++;
                }
                else
                poly.append("- " + coefficients[j] + " x^" + j + " ");
            }
        }
        if(firstPrintCheck == 0)
            poly.append(coefficients[0]);
        else if(coefficients[0] > 0)
            poly.append("+ " + coefficients[0]);
        else if(coefficients[0] < 0)
            poly.append("- " + coefficients[0]);

        return poly.toString();
    }
}



public class TestPolynomial
{
public static void main(String[] args)
{
    //Scanner
    Scanner scan = new Scanner(System.in);
    int trigger = 0;
    int coefficient;
    int degree=0;
    IntegerPolynomialArrayBased p1 = new IntegerPolynomialArrayBased();
    while(degree > -1)
    {
    degree = scan.nextInt();
    if(degree >= 0)
    {coefficient = scan.nextInt();
    p1.changeCoefficient(coefficient, degree);
    }
    }
}
}



Remember only one class/interface can be public in a file.
Also I have initialized degree to 0 as you need to initialize local variable before using them!!
Was This Post Helpful? 0
  • +
  • -

#3 Laorik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-November 07

Re: Error: non-static variable this cannot be referenced from a static con

Posted 07 March 2008 - 12:03 AM

View Postbhandari, on 6 Mar, 2008 - 11:27 PM, said:

hi,

use this code and enjoy.

import java.util.Scanner;

interface IntegerPolynomialInterface{
    public int degree();
    public int getCoefficient(int deg);
    public void changeCoefficient(int cof, int deg);
    public void add(IntegerPolynomialArrayBased p2);
    public String toString();

}
class IntegerPolynomialArrayBased implements IntegerPolynomialInterface{

    private static final int MAX_POLY_SIZE = 51;
    public int coefficients[]; // array of coefficients

    public IntegerPolynomialArrayBased(){
        coefficients = new int[MAX_POLY_SIZE];
    } //default constructor

    public int degree(){
        int j = 0;
        for(int i = MAX_POLY_SIZE - 1; i >= 0; i--)
        {

            if(coefficients[i] != 0 || i == 0)
                {
                j = i;
                }
        }
        return j;
    }

    public int getCoefficient(int deg){
        return coefficients[deg];
    }

    public void changeCoefficient(int cof, int deg){
        coefficients[deg] = cof;
    }

    public void add(IntegerPolynomialArrayBased p2){

        int count = 0;
        while(count < MAX_POLY_SIZE){
            coefficients[count] = coefficients[count] + p2.getCoefficient(count);
            }
    }

    public String toString(){
        int firstPrintCheck = 0;
        StringBuilder poly = new StringBuilder();
        for(int j = MAX_POLY_SIZE - 1; j > 0; j--)
        {
            if(coefficients[j] > 0)
            {
                if(firstPrintCheck == 0){
                poly.append(coefficients[j] + " x^" + j + " ");
                firstPrintCheck++;
                }
                else
                poly.append("+ " + coefficients[j] + " x^" + j + " ");
            }
            else if(coefficients[j] < 0)
            {
                if(firstPrintCheck == 0){
                poly.append(coefficients[j] + " x^" + j + " ");
                firstPrintCheck++;
                }
                else
                poly.append("- " + coefficients[j] + " x^" + j + " ");
            }
        }
        if(firstPrintCheck == 0)
            poly.append(coefficients[0]);
        else if(coefficients[0] > 0)
            poly.append("+ " + coefficients[0]);
        else if(coefficients[0] < 0)
            poly.append("- " + coefficients[0]);

        return poly.toString();
    }
}



public class TestPolynomial
{
public static void main(String[] args)
{
    //Scanner
    Scanner scan = new Scanner(System.in);
    int trigger = 0;
    int coefficient;
    int degree=0;
    IntegerPolynomialArrayBased p1 = new IntegerPolynomialArrayBased();
    while(degree > -1)
    {
    degree = scan.nextInt();
    if(degree >= 0)
    {coefficient = scan.nextInt();
    p1.changeCoefficient(coefficient, degree);
    }
    }
}
}



Remember only one class/interface can be public in a file.
Also I have initialized degree to 0 as you need to initialize local variable before using them!!


Thanks!! That totally makes sense now!
Was This Post Helpful? 0
  • +
  • -

#4 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Re: Error: non-static variable this cannot be referenced from a static con

Posted 07 March 2008 - 12:09 AM

welcome always.
Don't hesitate to ask more queries.
Please spread the word </DIC> among your friends!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1