// *************************************************************
// Name:         Mohamed Aboutabl
// Date:         10/04/2004
// Assignment:   Programming Assignment #2 Projectile.java
//
// *************************************************************
// References & Acknowledgements: I received no outside help
//
// *************************************************************
// Name of the class:	Projectile
// Purpose of the class: Compute trajectory of a cannonball
// *************************************************************

import java.text.DecimalFormat ;
import cs1.Keyboard ;

public class Projectile
{
	/**----------------------------------------------------------
	// Read in the initial velocity (meters/sec) and 
	// shooting angle (degress).
	// Then calculates the maximum altitude (meters), 
	// when this altitude is reached (seconds)
	// and the maximum horizontal distance travelled by 
	// the cannonball (meters)
	// --------------------------------------------------------*/
	public static void main( String[] args)
	{
		// Variables Declarations
		double	velocity ,	// initial velocity of the ball
					angleDegrees , angleRadians,	// Shooting angle
					maxAltitude , 	// maximum altitude
					maxAltitudeTime , 	// when it occurs (in seconds)
					totalFlightTime , 	// 
					maxDistance ,	// max. horizontal distance
					vSinTh , vCosTh ; // temporary variables

		DecimalFormat fmt;

		// Constants
		final double 
			GRAV_ACCEL  = 9.81 ,				// in m/sec/sec
		 	RAD_PER_DEG = Math.PI/180.0 ; // convert degrees to radians
		
		
		// Print the header
		System.out.println("\n\t\t\tThe Cannonball Trajectory Problem\n");
		
		// Get the initial velocity and angle
		System.out.print("Enter initial velocity (in m/s): ");
		velocity = Keyboard.readDouble() ;
		System.out.print("\nEnter shooting angle (in degrees): ");
		angleDegrees = Keyboard.readDouble();
		
		// Convert angle from degrees to radians
		angleRadians = angleDegrees * RAD_PER_DEG ;
		
		// Compute Trajectory parameters
		vSinTh = velocity * Math.sin(angleRadians) ;
		vCosTh = velocity * Math.cos(angleRadians) ;
		
		maxAltitudeTime = vSinTh / GRAV_ACCEL ;
		maxAltitude = Math.pow( vSinTh , 2.0 ) / ( 2.0 * GRAV_ACCEL ) ;
		maxDistance = ( 2.0 * vSinTh * vCosTh ) / GRAV_ACCEL ;
		totalFlightTime = 2.0 * maxAltitudeTime ;
		
		// Display results 
		fmt = new DecimalFormat("0.0##");
		System.out.println("\nInitial shooting velocity:\t\t" + fmt.format(velocity) + " m/s" ) ;

		System.out.println("Initial shooting angle   :\t\t" + fmt.format(angleDegrees) 
			+ " degress ( = " +  fmt.format(angleRadians) + " radians)" ) ;

		System.out.println("\nThe cannonball will reach a maximum altitude of " 
			+ fmt.format(maxAltitude) +" meters after " + fmt.format(maxAltitudeTime) + " seconds." ) ;

		System.out.println("It will land " + fmt.format(maxDistance) 
			+ " meters away from the cannon." ) ;

		System.out.println("The total flight time is " + fmt.format(totalFlightTime) + " seconds." ) ;

		// Dsiplay the exit message 
		System.out.println("\n\t\tExiting program Cannonball Trajectory");
			
	}
}