-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGravityCalculator.java
More file actions
67 lines (61 loc) · 1.87 KB
/
GravityCalculator.java
File metadata and controls
67 lines (61 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* GravityCalculator.java
*
* Version:
* 1
*
*/
/**
* This program calculates the distance and velocity an object
* travels in the downwards direction given some length of time
* based on gravity of Earth and Mars.
*
*
* @author Ishika Prasad
*
*/
public class GravityCalculator {
static double gravity, distance, velocity, time;
/**
* This method is used to calculate and print the
* distance and velocity on Earth and Mars.
*
*/
public static void calculation() {
distance = (gravity/2)* time*time;
velocity = gravity * time;
System.out.print("The object's position after " + time + " seconds is ");
System.out.printf("%.2f", distance);
System.out.println(" m from starting position");
System.out.print("Current velocity is ");
System.out.printf("%.2f", velocity);
System.out.println(" m/s");
}
/**
* The main program.
* @param args command line input
* args[0] can be planet either "earth" or "mars"
* args[1] is the time duration
*
*/
public static void main(String[] args) {
try {
time = Double.parseDouble(args[1]);
if (args.length == 2) {
if (args[0].equals("earth")) {
gravity = 9.81;
calculation();
} else if (args[0].equals("mars")) {
gravity = 3.711;
calculation();
} else {
System.out.println("The planet name can't be other than earth or mars");
}
} else {
System.out.println("The argument length should be two: planet and time duration");
}
} catch (Exception e) {
System.out.println("Time duration should be Integer, Float or Double");
}
}
}