Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Watermelon projectile

Assignment objectives:
- Input / output
- Decision making statements
- Loops
- Methods

Shoot a watermelon from a cannon and write a program in JAVA to compute the following given the initial velocity and initial angle of trajectory:

1. Maximum horizontal distance
2. Maximum vertical distance
3. Total travel time
4. Elapsed time when it reaches maximum vertical distance
5. In addition if there is an obstacle in its path, would the cannon ball clear it or not.
Initial velocity, initial angle (in degrees), the obstacle distance from the canon and the height of the obstacle is given by the user, use any way you like to ask for the data.
The user must be allowed to try different set of inputs as many times as desired.

Requirements:
1. You must use methods for each of the above calculations.
2. You must use Dialog box for inputs from the user
3. Check the validity of input data

Sample output:
Initial velocity = ???
Initial angle = ????
Obstacle distance from the anon = ????
Obstacle height = ?????
Maximum horizontal distance = ?????

Obstacle was cleared or not

I am getting that my obstacle is not cleared every time I run the program (even when obstacle is 0 feet). Also, I don't know how to make the program wok using methods and how to validate the input data.

```import java.text.DecimalFormat;
import javax.swing.*;

class projectileMotion{
public static void main(String[] args){

int n;
do{

//declare variables
double x_max;
double y_max;
double t_total;
double t_maxV;
double GRAVITY = 32; //feet/s2
String str1 = null;

// get needed input from user
String initialVelocity = JOptionPane.showInputDialog ( "Enter the initial velocity (in feet/sec):" );
String initialAngle = JOptionPane.showInputDialog ( "Enter the initial angle (in degrees):");
String y_obst = JOptionPane.showInputDialog ( "Enter the height of the obstacle (in feet), if any:");
String x_obs = JOptionPane.showInputDialog ( "Enter the distance seperating the canon and the obstacle (in feet), if any:" );

//convert numbers from type String to type double
double initialVelocity2 = Double.parseDouble ( initialVelocity);
double initialAngle2 = Double.parseDouble ( initialAngle);
double y_obst2 = Double.parseDouble ( y_obst);
double x_obs2 = Double.parseDouble ( x_obs);

//convert the given angle from degrees to radians
double initialAngle3 = initialAngle2 * Math.PI / 180;

//calculate maximum horizontal distance
//double maxDist (double initialVelocity21, double initialAngle31)
//{
x_max = Math.pow(initialVelocity2, 2) * Math.sin(initialAngle3*2)/GRAVITY;
//}

//calculate maximum vertical distance
//double maxHeight (double initialVelocity211, double initialAngle311){
y_max = Math.pow(initialVelocity2, 2) * Math.pow(Math.sin(initialAngle3), 2)/(2*GRAVITY) ;
//}

//calculate total time traveled
//double totalTime (double initialVelocity2111, double initialAngle3111){
t_total = x_max / (initialVelocity2 * Math.cos(initialAngle3)) ;
//}

//calculate elapsed time when maximum vertical distance is reached
//double timeYmax (double t_total1) {
t_maxV = t_total/2;
//}

//will canon ball be clear or not???

// calculate time it takes to reach distance at which obstacle is placed given speed and angle
double	time = x_obs2 / (initialVelocity2 * Math.cos(initialAngle2));

// Calculate height at the time we just found
double height = (initialVelocity2 * Math.sin(initialAngle2)*time) - ((GRAVITY * Math.pow(time, 2)/2));

// Compare height of the projectile and height of the obstacle

if (height <=  y_obst2 ){
str1 = ("Obstacle wasn't cleared.");
}

else{
str1 = ("Obstacle was cleared.");
}

// Format numbers so that only 2 decimals appear
DecimalFormat df = new DecimalFormat("#.##");

// Show ouput in dialog box
String	output = ("Initial velocity = " + initialVelocity2+
"feet/sec. \nInitial angle = " + initialAngle2+
"degrees. \nObstacle distance from the canon = " + x_obs2+
"feet. \nObstacle height = " + y_obst2+
"feet. \nMaximum horizontal distance = " + df.format(x_max)+
"feet. \nMaximum vertical distance = " + df.format(y_max)+
"feet.\nTotal time traveled = " + df.format(t_total)+
"sec. \nElapsed time when maximum vertical distance is reached = " + df.format(t_maxV)+ "sec.\n" + str1);

JOptionPane.showMessageDialog (null,output);

n = JOptionPane.showConfirmDialog(null,
"Would you like to enter a different set of data?",
"Try Again?",JOptionPane.YES_NO_OPTION);
}while(n==0);
}```  Reply With Quote

3. ## Re: Watermelon projectile

Format your code with proper tags to preserve formatting.

[C0DE=java]
<code>
[/CODE]

It would be a good idea to structure your program with methods. You should take the time to seperate it into multiple modules to perform a specific task.

More on Methods:

Method:
-A method is invoked by a method call - Name(args)
-Most methods are invoked through a reference call to a object
-Method args can be: variables, constants, expressions
-Must be defined in a class definition
-Format: return-type method-name(paramter list){declarations and statements}
-At most can return 1 value
-A method cannot be defined inside another method.
-Method control is given back to caller when } is reached or return;

Variables:
-Variables in method definitions are local to that method only
-Instance variables should be only used if they are required in more than
one method.

Method Calls:
-thisMethod();
-object.method(); //object refers to class
-Class.method(); //static methods  Reply With Quote