1

this is not science at all i just need some help with java. so if you know, there are 3 equations of motion.

we will just focus on 1 that is v=u+at now forget all the technical terms. you can see there are 4 variables.

but if i try to calculate this in java, i will always have to ask the user to provide the values of u,a and t and provide the output v. but in real life, we can calculate the value of any of the variable in this equation if we have the other ones.

so, is there a way that all of the inputs are being asked and the users have the option to enter no input in one of the 4 inputs and that will be calculated.

example: if i enter 2=v,1=u and 3=a, and when i am asked the value of t, i just press enter. is there something i could do to find the blank space?

now i know there is a way to do an if loop but that will be quite lengthy and i am a bit lazy too. so i am just asking that is there a quick way to do this?

now i am a beginner who has limited knowledge of java so please don't be sarcastic and tell me if i am wrong, more likely where i am wrong.

thanks!

Aayush Mahajan
  • 3,856
  • 6
  • 25
  • 32
  • are you using `System.out/in` to read in the user input? Or do you have a application/applet with textfields and labels? – brimborium Jul 30 '12 at 17:04
  • Could you elaborate if your main problem lies in the way to do the interaction with the user or the calculation of the unknown variable? – brimborium Jul 30 '12 at 17:14
  • If you're entering values at the console, perhaps entering a dash or question mark for one of the variables means to calculate that one? – NomadMaker May 01 '20 at 11:04

3 Answers3

3

Just solve for the other variables directly:

v=u+at
u=v-at
a=(v-u)/t
t=(v-u)/a
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
1

What you want is a system of equations solver (with or without Java).

See https://discursive.atlassian.net/wiki/display/CJCOOK/Solving+a+System+of+Linear+Equations where the article uses Java to setup a matrix representing the set of equations and solves it with the org.apache.commons.math.linear.RealMatrix package.

You can google "Linear System of Equations" to see more information on this process/how to implement your own.

As you letting the user choose 3 of the 4 equations -- have some form where they can enter 3 or more values then plug into the matrix solver and return the 4th answer. Otherwise you could differentiate the inputs by keyword (google argparse for how to do this with command line or look at How to parse command line arguments in Java?)

Community
  • 1
  • 1
Pyrce
  • 8,296
  • 3
  • 31
  • 46
  • I think that is a little bit overkill for that particular application. ;) But nonetheless quite interesting. +1 – brimborium Jul 30 '12 at 17:11
  • True, though if you don't know which 3 of the 4 equations they will give you, this ends up being much less boilerplate code, and easier to change/extend. – Pyrce Jul 30 '12 at 17:28
0

You can do it this way...

import java.util.Scanner;
class motion
{
    public static double i(String str)
    {
     return Double.parseDouble(str);
    }
    public static void main(String args[])
    {
        String v1,u1,a1,S1;
        double v,u,a,S;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the following values and \"?\" to find that value");
        System.out.print("v=");
        v1=sc.next();
        System.out.print("u=");
        u1=sc.next();
        System.out.print("a=");
        a1=sc.next();
        System.out.print("S=");
        S1=sc.next();
        if(v1.equals("?"))
        {
            u=i(u1);
            a=i(a1);
            S=i(S1);
            v=Math.sqrt(((u*u)+2*a*S));
            System.out.println("Final velocity="+v+" m/s");
        }
        else if(u1.equals("?"))
        {
            v=i(v1);
            a=i(a1);
            S=i(S1);
            u=Math.sqrt((v*v)-2*a*S);
            System.out.println("Initial velocity="+u+" m/s");
        }
        else if(a1.equals("?"))
        {
            u=i(u1);
            v=i(v1);
            S=i(S1);
            a=((v*v)-(u*u))/2*S;
            System.out.println("Acceleration="+a+" m/s^2");
        }
        else
        {
            u=i(u1);
            v=i(v1);
            a=i(a1);
            S=((v*v)-(u*u))/2*a;
            System.out.println("Displacement="+S+" m");
        }
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
Anshu
  • 1