Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a method with parameters from main method properly

I am in my first week of programming Java and am finding the syntax and errors extremely confusing. I am expected to write the code for the following inputs/output:

Access: public

Name: evaluateFormula

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Return: double

Parameters: 2

integer values – valA and valB

Body: Evaluate formula and return value – (valA + (valB mod 20) * 3.14 / 12)

I have tried:

public class Area
{

    public void evaluateFormula(int valA, int valB) {
        System.out.println(valA + (valB % 20) * 3.14 / 12);
   }

    public static void main(String[] args)
    {
        evaluateFormula(1,2);
    }
}

I am getting the error :

Area.java:14: error: non-static method evaluateFormula(int,int) cannot
be referenced from a static context
evaluateFormula(1,2);
^ 1 error

Being our first week, we haven’t learned what static even means, or why we should be using it in the main method. How do I get this code to run properly with 1 week worth of Java knowledge?

>Solution :

option 1:
you do the method static

public static void evaluateFormula(int valA, int valB) {

or

option 2:

you create an instance of class Area and call the method in main(…)

public static void main(String[] args)
{   
    Area a = new Area();
    a.evaluateFormula(1,2);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading