Gravity Calculator Java Example

Create Java gravity calculator, able to determine the object’s final position after falling through the Earth’s atmosphere through the predetermined time. The initial parameters include: gravity (-9.81), falling time (10 seconds), as well as initial position and initial velocity of the object (both set to 0 by default). However the user should be able to modify the last 3 parameters according to his liking, with the calculator providing him with corresponding final position each time one of parameters changes.

SOLUTION:

In this sample we will need to use an equation to determine the final position of the object:
x(t) = 0.5 × a × t2 + vi × t + xi,
wherea – gravity = acceleration
t – time
vi – initial velocity
xi – initial position

With our variables:

Given these variables, our equation will look as following:

finalPosition = 0.5*gravity*fallingTime*fallingTime
+initialVelocity*fallingTime + initialPosition;

We will use do…while cycle in or application, each time keeping the user updated with the current state of variables as well as final position of the object derived from them (based on equation):

We will use a set of if…else in order to react to the commands signalling about the user’s desire to change of the variables:

This part of code makes sure that the user will be able to input nothing but digits (of double type) by ignoring the other kinds of input:

while (!in.hasNextDouble())
in.next();

Therefore, the final code for our application (with console window format in mind) will look like this:

Java Homework Assistance Online

We hope you found our gravity calculator Java sample useful and it will help you to cope with your own Java homework projects. In case you still experience problems with IT assignment, you can count on us. We will provide Java homework help with tasks of any difficulty level and deadline.

Check out also Java program for magic square sample that may come in handy in your studies.

One thought on “Gravity Calculator Java Example”

1. Nana Asare says:

Very useful guide. And I value your website so much. I am just a beginner in Java so I will frequent this site a lot.