Hi,
I created this problem where i am calculating the projectile motion of a ball. I took paramters of ball angle, gravity and velocity. When compiling this program, I am not able the projectile results are displaying as zero. Also, the exact time when the ball is launched is not showing properly. Here is the code:
using System;
class Plaunch
{
double velocity;
double gravity;
int angle;
double flight_Time;
double verticalDistance;
double horizontalDistance;
DateTime time;
public Plaunch(double v, double g, int a)
{
velocity = v;
gravity = g;
angle = a;
double angleRad = angle * (Math.PI / 180);
verticalDistance = Math.Round(((velocity * velocity) / (2 * gravity)) * ((Math.Sin(angle * Math.PI / 180)) * (Math.Sin(angle * Math.PI / 180))), 2);
horizontalDistance = Math.Round(((velocity * velocity) / gravity) * (Math.Sin(2 * angle * Math.PI / 180)), 2);
flight_Time = Math.Round(((2 * velocity) / gravity) * (Math.Sin(angle * Math.PI / 180)), 2);
time = DateTime.Now;
}
public double Gravity
{
get { return gravity; }
}
public double Velocity
{
get { return velocity; }
}
public int Angle
{
get { return angle; }
}
public double VerticalDistance
{
get { return verticalDistance; }
}
public double HorizontalDistance
{
get { return horizontalDistance; }
}
public double Flight_Time
{
get { return flight_Time; }
}
public DateTime Time
{
get { return time; }
}
}
class Test
{
public static void Main()
{
double userVelocity = 10.00d;
double userGravity = 9.80d;
int userAngle = 180;
string userInput;
double x_range;
double y_range;
double t;
Console.WriteLine("Projectile Motion Calculator");
Console.WriteLine("Setting the launch parameters");
Console.WriteLine(" Launch angle is an integer value ranging from 0 degrees to 180 degrees.");
Console.Write(" Enter the launch angle: ");
userInput = Console.ReadLine();
if (userInput == "")
{
userAngle = 180;
Console.WriteLine("Angle has been defaulted to 180 degrees");
}
else
{
userAngle = int.Parse(userInput);
}
Console.WriteLine(" Launch velocity is a real value ranging from 0 m/s to 1000 m/s.");
Console.Write(" Enter the launch velocity: ");
userInput = Console.ReadLine();
if (userInput == "")
{
userVelocity = 10.00d;
Console.WriteLine("Velocity has been defaulted to 10 m/s");
}
else
{
userVelocity = double.Parse(userInput);
}
Console.WriteLine(" Launch gravity is a real value ranging from 0 m/s^2 to 100 m/s^2.");
Console.Write(" Enter the launch gravity: ");
userInput = Console.ReadLine();
if(userInput == "")
{
userGravity = 9.80d;
Console.WriteLine("Gravity has been defaulted to 9.8 m/s^2");
}
else
{
userGravity = double.Parse(userInput);
}
Console.WriteLine("Verifying the launch Parameters");
Plaunch p1 = new Plaunch(userVelocity, userGravity, userAngle);
Console.WriteLine("Launch angle: {0:F2}", userAngle);
Console.WriteLine("Launch velocity: {0:F2}", userVelocity);
Console.WriteLine("Gravity: {0:F2}", userGravity);
x_range = p1.HorizontalDistance;
y_range = p1.VerticalDistance;
t = p1.Flight_Time;
if((userAngle < 0 || userAngle > 180) || (userVelocity < 0 || userVelocity > 1000) || (userGravity < 0 || userGravity > 100))
{
Console.WriteLine(" The launch parameters are invalid.");
Console.WriteLine("The launch is aborted.");
}
else
{
Console.WriteLine("Launch time: {0:F2}", p1.Time);
Console.WriteLine("Launch angle: {0:F2} degrees", p1.Angle);
Console.WriteLine("Launch velocity: {0:F2} m/s", p1.Velocity);
Console.WriteLine("Gravity: {0:F2} m/s^2", p1.Gravity);
Console.WriteLine("Horizontal range: {0:F2} m", x_range);
Console.WriteLine("Vertical range: {0:F2} m", y_range);
Console.WriteLine(" Flight time: {0:F2} s", t);
}
}
}
Thanks!
I created this problem where i am calculating the projectile motion of a ball. I took paramters of ball angle, gravity and velocity. When compiling this program, I am not able the projectile results are displaying as zero. Also, the exact time when the ball is launched is not showing properly. Here is the code:
using System;
class Plaunch
{
double velocity;
double gravity;
int angle;
double flight_Time;
double verticalDistance;
double horizontalDistance;
DateTime time;
public Plaunch(double v, double g, int a)
{
velocity = v;
gravity = g;
angle = a;
double angleRad = angle * (Math.PI / 180);
verticalDistance = Math.Round(((velocity * velocity) / (2 * gravity)) * ((Math.Sin(angle * Math.PI / 180)) * (Math.Sin(angle * Math.PI / 180))), 2);
horizontalDistance = Math.Round(((velocity * velocity) / gravity) * (Math.Sin(2 * angle * Math.PI / 180)), 2);
flight_Time = Math.Round(((2 * velocity) / gravity) * (Math.Sin(angle * Math.PI / 180)), 2);
time = DateTime.Now;
}
public double Gravity
{
get { return gravity; }
}
public double Velocity
{
get { return velocity; }
}
public int Angle
{
get { return angle; }
}
public double VerticalDistance
{
get { return verticalDistance; }
}
public double HorizontalDistance
{
get { return horizontalDistance; }
}
public double Flight_Time
{
get { return flight_Time; }
}
public DateTime Time
{
get { return time; }
}
}
class Test
{
public static void Main()
{
double userVelocity = 10.00d;
double userGravity = 9.80d;
int userAngle = 180;
string userInput;
double x_range;
double y_range;
double t;
Console.WriteLine("Projectile Motion Calculator");
Console.WriteLine("Setting the launch parameters");
Console.WriteLine(" Launch angle is an integer value ranging from 0 degrees to 180 degrees.");
Console.Write(" Enter the launch angle: ");
userInput = Console.ReadLine();
if (userInput == "")
{
userAngle = 180;
Console.WriteLine("Angle has been defaulted to 180 degrees");
}
else
{
userAngle = int.Parse(userInput);
}
Console.WriteLine(" Launch velocity is a real value ranging from 0 m/s to 1000 m/s.");
Console.Write(" Enter the launch velocity: ");
userInput = Console.ReadLine();
if (userInput == "")
{
userVelocity = 10.00d;
Console.WriteLine("Velocity has been defaulted to 10 m/s");
}
else
{
userVelocity = double.Parse(userInput);
}
Console.WriteLine(" Launch gravity is a real value ranging from 0 m/s^2 to 100 m/s^2.");
Console.Write(" Enter the launch gravity: ");
userInput = Console.ReadLine();
if(userInput == "")
{
userGravity = 9.80d;
Console.WriteLine("Gravity has been defaulted to 9.8 m/s^2");
}
else
{
userGravity = double.Parse(userInput);
}
Console.WriteLine("Verifying the launch Parameters");
Plaunch p1 = new Plaunch(userVelocity, userGravity, userAngle);
Console.WriteLine("Launch angle: {0:F2}", userAngle);
Console.WriteLine("Launch velocity: {0:F2}", userVelocity);
Console.WriteLine("Gravity: {0:F2}", userGravity);
x_range = p1.HorizontalDistance;
y_range = p1.VerticalDistance;
t = p1.Flight_Time;
if((userAngle < 0 || userAngle > 180) || (userVelocity < 0 || userVelocity > 1000) || (userGravity < 0 || userGravity > 100))
{
Console.WriteLine(" The launch parameters are invalid.");
Console.WriteLine("The launch is aborted.");
}
else
{
Console.WriteLine("Launch time: {0:F2}", p1.Time);
Console.WriteLine("Launch angle: {0:F2} degrees", p1.Angle);
Console.WriteLine("Launch velocity: {0:F2} m/s", p1.Velocity);
Console.WriteLine("Gravity: {0:F2} m/s^2", p1.Gravity);
Console.WriteLine("Horizontal range: {0:F2} m", x_range);
Console.WriteLine("Vertical range: {0:F2} m", y_range);
Console.WriteLine(" Flight time: {0:F2} s", t);
}
}
}
Thanks!