-2

I am pretty new to coding and I have been having trouble with a Physics calculator I've been making. I made this trying to utilize OOP for a class project. The point I to let the user input the variables, then they get shipped into the equation on the class file, and then finally display the result. When I try to compile, it says that the function getAnswer can't see the result declared above it. I plan to make each of the 16 iterations of the equation, so I need to first figure out why this one doesn't work. Any answer is welcome.

-Thanks

 import java.util.Scanner;

 public class VFD {
    public static void main( String[] args ) {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Welcome to Kinmatics calculator");
        System.out.println("You can find Final Velocity, Acceleration, DIsplacemnt, or Time");
        System.out.println("What variable is not included in the equation?");
        String missing = keyboard.next();
        System.out.println("What variable are you looking for?");
        String find = keyboard.next();
        
        
        if (missing.equals("Final Velocity") && find.equals("Initial Velocity")) {
        System.out.println("Please enter your available values");
        System.out.println("Acceleration = (m/s^2)");
        double a = keyboard.nextDouble();
        System.out.println("Displacement = (m)");
        double d = keyboard.nextDouble();
        System.out.println("Time = (s)");
        double t = keyboard.nextDouble();
        VelocityFinder qviadt = new VelocityFinder();
        qviadt.qviadt(a, d, t); 
        System.out.println(qviadt.getAnswer());
        }
    }
 }

This is the class file

public class VelocityFinder {
    
    public void qviadt( double a, double d, double t  ) {
        double result = d/(.5*a*(t*t))/t;
        double answer = result;
    }
    public String getAnswer () {
        return answer;
    }
}
TYTIN
  • 3
  • 1

2 Answers2

1
    public class VelocityFinder {
    private double answer;

    
    public void qviadt( double a, double d, double t  ) {
        double result = d/(.5*a*(t*t))/t;
        double answer = result;
    }
    public String getAnswer () {
        return String.valueOf(answer);
    }
}
苏小刚
  • 36
  • 3
1

The methods qviadt and getAnswer are in the same class, but they are not the same method. getAnswer is trying to return something that doesn't exist. To fix this, remove getAnswer and change qviadt to:

public void qviadt( double a, double d, double t  ) {
    double result = d/(.5*a*(t*t))/t;
    double answer = result;
    return answer;
}

and store the return value of it directly. This is called a scope issue and you should look into what scope is and how to use it.

Raniconduh
  • 11
  • 1
  • Your code has an issue. You are returning the double answer when the method has a void declaration meaning the method should not have a return statement. – Dennis LLopis Jun 17 '22 at 03:41