I need help to fix this two errors in the code.
Line: 29 The static method isSolvable() from the type LinearEquation should be accessed in a static way.
Line: 39,40: The static method getX() from the type LinearEquation should be accessed in a static way.
Thank you.

// This is my driver
import java.util.Scanner;

public class LinearEquationTest {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in); // new a scanner

        // input the coefficient
        System.out.println("Please input the coefficient a, b, c, d, e,and f: ");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int d = input.nextInt();
        int e = input.nextInt();
        int f = input.nextInt();

        // new a LinearEquation object, and initial with constructor
        LinearEquation equations = new LinearEquation(a, b, c, d, e, f);

        int userChoice;
        boolean exitFlag = false;
        while(exitFlag == false){
            System.out.println("1.isSolvable() 2.getX() and getY() 3.Exit");
            System.out.print("Please choose the method you what to use: ");
            userChoice = input.nextInt();

            switch(userChoice){
                case 1: // use method isSolvable()
                    if(equations.isSolvable() == 0)
                        System.out.println("There is no solution in the equations");
                    else
                        System.out.println("Yes!! There is a solution in the equations");

                    break;
                case 2: // use methods getX() and getY() to get the solution
                    if(equations.isSolvable() == 0)
                        System.out.println("There is no solution QQ");
                    else
                        System.out.println("The solution of these equations is x = " + equations.getX() 
                            + ", y = " + equations.getY() + "!!!");
                    break;
                case 3:
                    System.out.println("See you next time");
                    exitFlag = true;
                    break;
                default:
                    break;
            }
        }
        input.close();
    }
}

Recommended Answers

All 9 Replies

hai toldav,

can you post the details of LinearEquation class once here? (if you have no problem)

so that we try to give a way of solution

rad, Here is the class. Thank you.

public class LinearEquation {
    // declare the six coefficient
    private static int a;
    private static int b;
    private static int c;
    private static int d;
    private static int e;
    private static int f;
    // a constructor to initial the coefficient
    LinearEquation(int inputA, int inputB, int inputC, int inputD, int inputE, int inputF){
            a = inputA;
            b = inputB;
            c = inputC;
            d = inputD;
            e = inputE;
            f = inputF;
    }
    // method isSolvable to show whether the equations has solution or not
    static int isSolvable(){
            if(((a*d) - (b*c)) == 0)
                return 0;
            else
                return 1;
    }

    // method getX() to calculate the solution of x
    static double getX(){
            return (((e*d) - (b*f))/((a*d) - (b*c)));
    }

    // method getY() to calculate the solution of y
    static double getY(){
        return (((a*f) - (e*c))/((a*d) - (b*c)));
    }
}

You have made everything in LinearEquation static. That's wrong.
You create instances of LinearEquation, and each instance should have its own instance variables and instance methods. Simply deleting every use of the worde "static" from that class will fix it, but in the meantime you should revise your course materials on static vs instance variables and methods, or read this
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

hai toldav,

yeah thats exactly correct what JamesCherrill said,

in simple words(as of my knowledge),

Note 1:

if the instantiation of a class is necessary,then its better to make the methods of that class as instance methods only.

Note 2:

if the instantiation of a class is not necessary then its better to make the methods of that class as static methods only

reason is same as described by JamesCherrill

my apologies for my poor written skills

i hope that you got the point.

happy coding.

any comments are appreciated...........................

Thank you guys for your inputs. I'll work on that.

james and radhakrishna already pointed out the things you should be doing.. i just saw that in ur isSolvable() method , ur doing this :

            if(((a*d) - (b*c)) == 0)
                return 0;
            else
                return 1;

you dont need to have a if else structure for return.. once return is called , the method exits. so the above code can be compressed down to

 if(((a*d) - (b*c)) == 0) return 0;
 return 1;

makes code look more compact , especially when your doing more complicated stuff. learnt about this recently , so id thought id share :)

You could even compact it further using the conditional operator:

return ((a * d) == (b * c)) ? 0 : 1;

Keep in mind however that compact code does not necessarily contribute to readability.

Edit:
You seem to have defined your isSolvable() method with int as return type.
A return type of boolean seems to be more appropriate here.
Example:

public boolean isSolvable()
{
    return ((a * b) != (b * c));
}

Thank you, Guys I really new into Java an all your comments help me learn java. I really appreaciated.

please mark this solved if you got your answer

thnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.