Write a program that simulates an airplane race. The program will display a tabl
ID: 3777777 • Letter: W
Question
Write a program that simulates an airplane race. The program will display a table showing the speed in km/hour and distance in km traveled by two airplanes every second until one of them has gone 10 kilometers.
Requirements:
• The program will use a single function that has the following input parameters: time and acceleration.
• The function will pass back two data items: speed and distance. o Use this formula to calculate speed: speed = time * acceleration; (Speed is in meters per second, time is in seconds, and acceleration is in meters per second2 .) o Use this formula for distance: distance = 0.5 * time * time * acceleration; (Distance is in meters)
• You don't need to ask for user input. You can just use constants for acceleration. Use random values if you want to make the race more interesting. The acceleration values should be between 5 and 25 meters per second2
• Do not use any global variables.
• Include a driver for testing the speed and distance function.
Explanation / Answer
package chegg2;
public class Airplane {
public float[] calcSpeed(float time, float acce){
//This function returns an array of speed and distance values
float arr[]=new float[2];
arr[0]=( float)(time* acce);
arr[1]=(float) (.5* time * time * acce);
return arr;
}
public static void main(String args[]){
Airplane a1=new Airplane();
Airplane a2=new Airplane();
boolean ten=false;
float s1[];
float s2[];
float dist1;
float dist2;
float speed1;
float speed2;
// These are all local variables which will store speed and distance values.
int time=1;
System.out.println("Plane Time(sec) Speed(km/hr) Distance(km)");
while(!ten){
s1=a1.calcSpeed(time, 5f);
//You can change the value of acceleration of airplane1 from 5f to anything else.
//Second parameter is speed of airplane 1 in m/sec
s2=a2.calcSpeed(time, 16f);
//You can change the value of acceleration of airplane2 from 16f to anything else.
//Second parameter is speed of airplane 2 in m/sec
dist1=(float) s1[1]/1000;
dist2=(float) s2[1]/1000;
//converts speed from m to km
speed1=(float) (s1[0]*18)/5;
speed2=(float) (s2[0]*18)/5;
//converts speed from m/s to km/hr
if(dist1>=10 || dist2>=10){
ten=true;
}
System.out.println("Airplane1: " + time + " " + speed1 + " " + dist1);
System.out.println("Airplane2: " + time + " " + speed2 + " " + dist2);
System.out.println();
time++;
}
}
}
Sample run:
Plane Time(sec) Speed(km/hr) Distance(km)
Airplane1: 1 18.0 0.0025
Airplane2: 1 57.6 0.008
Airplane1: 2 36.0 0.01
Airplane2: 2 115.2 0.032
Airplane1: 3 54.0 0.0225
Airplane2: 3 172.8 0.072
Airplane1: 4 72.0 0.04
Airplane2: 4 230.4 0.128
Airplane1: 5 90.0 0.0625
Airplane2: 5 288.0 0.2
Airplane1: 6 108.0 0.09
Airplane2: 6 345.6 0.288
Airplane1: 7 126.0 0.1225
Airplane2: 7 403.2 0.392
Airplane1: 8 144.0 0.16
Airplane2: 8 460.8 0.512
Airplane1: 9 162.0 0.2025
Airplane2: 9 518.4 0.648
Airplane1: 10 180.0 0.25
Airplane2: 10 576.0 0.8
Airplane1: 11 198.0 0.3025
Airplane2: 11 633.6 0.968
Airplane1: 12 216.0 0.36
Airplane2: 12 691.2 1.152
Airplane1: 13 234.0 0.4225
Airplane2: 13 748.8 1.352
Airplane1: 14 252.0 0.49
Airplane2: 14 806.4 1.568
Airplane1: 15 270.0 0.5625
Airplane2: 15 864.0 1.8
Airplane1: 16 288.0 0.64
Airplane2: 16 921.6 2.048
Airplane1: 17 306.0 0.7225
Airplane2: 17 979.2 2.312
Airplane1: 18 324.0 0.81
Airplane2: 18 1036.8 2.592
Airplane1: 19 342.0 0.9025
Airplane2: 19 1094.4 2.888
Airplane1: 20 360.0 1.0
Airplane2: 20 1152.0 3.2
Airplane1: 21 378.0 1.1025
Airplane2: 21 1209.6 3.528
Airplane1: 22 396.0 1.21
Airplane2: 22 1267.2 3.872
Airplane1: 23 414.0 1.3225
Airplane2: 23 1324.8 4.232
Airplane1: 24 432.0 1.44
Airplane2: 24 1382.4 4.608
Airplane1: 25 450.0 1.5625
Airplane2: 25 1440.0 5.0
Airplane1: 26 468.0 1.69
Airplane2: 26 1497.6 5.408
Airplane1: 27 486.0 1.8225
Airplane2: 27 1555.2 5.832
Airplane1: 28 504.0 1.96
Airplane2: 28 1612.8 6.272
Airplane1: 29 522.0 2.1025
Airplane2: 29 1670.4 6.728
Airplane1: 30 540.0 2.25
Airplane2: 30 1728.0 7.2
Airplane1: 31 558.0 2.4025
Airplane2: 31 1785.6 7.688
Airplane1: 32 576.0 2.56
Airplane2: 32 1843.2 8.192
Airplane1: 33 594.0 2.7225
Airplane2: 33 1900.8 8.712
Airplane1: 34 612.0 2.89
Airplane2: 34 1958.4 9.248
Airplane1: 35 630.0 3.0625
Airplane2: 35 2016.0 9.8
Airplane1: 36 648.0 3.24
Airplane2: 36 2073.6 10.368
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.