Home > other >  Calling a method with parameters from main method properly
Calling a method with parameters from main method properly

Time:07-12

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?

CodePudding user response:

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);
}
  •  Tags:  
  • java
  • Related