import javax.swing.*;

public class SlideLength {

	public static void main(String[] args) {

		// Constants-- coefficients of friction

		final double mu8 = 0.25;

		final double mu4 = 0.30;

		final double mu0 = 0.35;

		final double g = 9.8; // Gravity

		// Min mass at which the coeff of friction applies

		final double mu8min = 8.0; // Kg

		final double mu4min = 4.0; // Kg

		String input;

		// Prompt and read elevation, velocity, box masses from user		

		input = JOptionPane.showInputDialog("Enter elevation (m)");

		double h = Double.parseDouble(input);

		input = JOptionPane.showInputDialog("Enter maximum velocity(m/sec)");

		double vMax = Double.parseDouble(input);

		input = JOptionPane.showInputDialog("Enter box 1 mass(kg)");

		double m1 = Double.parseDouble(input);

		input = JOptionPane.showInputDialog("Enter box 2 mass(kg)");

		double m2 = Double.parseDouble(input);

		input = JOptionPane.showInputDialog("Enter box 3 mass(kg)");

		double m3 = Double.parseDouble(input);

		// If masses not in decreasing (nonincreasing) order, correct order

		if (m1 < m2 || m1 < m3 || m2 < m3) {

			System.out.println("Boxes in wrong order; corrected");

			//Put m2 and m3 in correct order first

			if (m2 < m3) {

				double temp = m2;

				m2 = m3;

				m3 = temp;

			}

			//Now put m1 in correct order

			if (m1 < m3) {

				double temp = m1;

				m1 = m2;

				m2 = m3;

				m3 = temp;

			} else if (m1 < m2 && m1 >= m3) {

				double temp = m1;

				m1 = m2;

				m2 = temp;

			}

		}

		// Find coefficient of friction for each mass.

		// Mass 1

		double mu1, mu2, mu3;

		if (m1 > mu8min)
			mu1 = mu8;

		else if (m1 >= mu4min)
			mu1 = mu4;

		else
			mu1 = mu0;

		// Mass 2	

		if (m2 > mu8min)
			mu2 = mu8;

		else if (m2 >= mu4min)
			mu2 = mu4;

		else
			mu2 = mu0;

		// Mass 3

		if (m3 > mu8min)
			mu3 = mu8;

		else if (m3 >= mu4min)
			mu3 = mu4;

		else
			mu3 = mu0;

		// Loop through all angles 0 to pi/2 radians (0-90 degrees). Exclude endpts 

		// Get answer to nearest 0.01 radian

		double thetaMin = 0.01; // Degrees

		double thetaMax = 0.5 * Math.PI - 0.01; // Degrees

		double a = -999, t1 = -999, t2 = -999, t = -999, s = -999, v = -999;

		double maxTheta = -999;
		
		System.out.println("thetaMin =" + thetaMin);
		System.out.println("thetaMax =" + thetaMax);

		for (double theta = thetaMin; theta < thetaMax; theta += 0.01) {
			
			System.out.println("theta at start =" + theta);

			double c1 = m1 * g * (Math.sin(theta) - mu1 * Math.cos(theta));

			double c2 = m2 * g * (Math.sin(theta) - mu2 * Math.cos(theta));

			double c3 = m3 * g * (Math.sin(theta) - mu3 * Math.cos(theta));


            if( (c1+c2+c3)<0) continue;

			a = (c1 + c2 + c3) / (m1 + m2 + m3);

			t1 = c1 - m1 * a;

			t2 = -c3 + m3 * a;

			s = h / Math.sin(theta);

			t = Math.sqrt(2.0 * s / a);

			v = a * t;

			maxTheta = theta;
			
			System.out.println("theta at end =" + theta);
			System.out.println("v = " + v);

			if (v > vMax)
				break;

		}

		System.out.println("Results");

		System.out.println("Slide elevation(m): " + h);

		System.out.println("Max velocity(m/sec): " + vMax);

		System.out.println("Box 1 mass(kg): " + m1);

		System.out.println("Box 2 mass(kg): " + m2);

		System.out.println("Box 3 mass(kg): " + m3);

		System.out.println("Acceleration(m/sec^2): " + a);

		System.out.println("Tension 1(kg-m/sec^2): " + t1);

		System.out.println("Tension 2(kg-m/sec^2): " + t2);

		System.out.println("Theta (radians):" + maxTheta);

		System.out.println(
			"Theta (degrees): " + maxTheta * 360 / (Math.PI * 2));

		System.out.println("Slide length(m): " + s);

		System.out.println("Time to reach bottom(sec): " + t);

		System.out.println("Velocity at bottom(m/sec): " + v);

		System.exit(0);

	}

}
