1

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

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?

qgvqevgwre
  • 13
  • 2
  • @ΦXocę 웃 Пepeúpa ツ has given a good example to you already, for the reason why the main method have to be static, you can check it here: [Why is the Java main method static?](https://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – hokwanhung Jul 12 '22 at 08:37

1 Answers1

1

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);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97