I am attempting to get extra credit in a physics class by making a program to calculate the distance an object will go by calculating muzzle velocity and getting the angle of fire from the user.
I am getting multiple errors that I can't understand from JCreator LE. I am thinking about just making the program in C++ because these errors are getting annoying (the only reason I'm using Java is because I am in a java class right now).
Anyway, here is the source code:
[codebox]/*Shoot For Your Grade
*Calculate horizontal distance for angle via the height and range
*/
public class shootcalculator
{
public static java.util.Scanner scanner = new java.util.Scanner(System.in);
public static void main(String args[])
{
// Get gravity
System.out.print("Enter gravity in m/s^2 (earth default is 9.8): ");
double gravityFactor = scanner.nextInt()/2;
// Get the initial height
System.out.print("\nInitial height of cannon (cm): ");
double height = scanner.nextInt();
// Get horizontal range
System.out.print("\nHorizontal range of cannon (cm): ");
double range = scanner.nextInt();
// Calculate time for payload to hit the ground
double time = (range/gravityFactor);
System.out.print("\nTime: " + time);
// Calculate muzzle velocity
double velocity = (range/time);
System.out.print("\nMuzzle Velocity: " + velocity);
// Get firing angle
double angle = scanner.nextInt();
// Calculate verticle and horizontal velocity
double horVel = velocity*asin(angle);
double verVel = velocity*acos(angle);
System.out.print("\nHorizontal Velocity: " + horVel);
System.out.print("\nVerticle Velocity: " + verVel);
// Calculate new time
double discr = Math.sqrt((velocity * velocity) - (4 * (gravity/(-2) * height));
System.out.print("\nDiscriminant: " + discr);
if ((velocity - discr) < 0)
double newTime = (velocity - discr)/2(gravity/(-2));
else
double newTime = (velocity + discr)/2(gravity/(-2));
System.out.print("\nNew Time: " + newTime);
// Calculate horizontal distance
double horDis = horVel*newTime+(gravity/-2)*pow(newTime, 2);
System.out.print("\nDISTANCE FIRED: " + horDis + "\nyay!");
}
}[/codebox]
And here are the errors:
error.jpg 85.41KB
89 downloads
If anybody could help, it would be appreciated.
Java Error
Started by
npsken
, Apr 04 2008 12:28 PM
3 replies to this topic
#1
Posted 04 April 2008 - 12:28 PM
#2
Posted 04 April 2008 - 03:17 PM
Um you have to create variables before you use them... Also I would use eclipse not JCreator.
Umm ok you had a bunch of mistakes, so I just recreated this in Eclipse then made all the changes. Rather than explain all of your mistakes just look at the changes I made to your code. There are a lot of them.
I added a few ( and ) in the code which might throw off the result if they are in the wrong place (I never took physics).
Umm ok you had a bunch of mistakes, so I just recreated this in Eclipse then made all the changes. Rather than explain all of your mistakes just look at the changes I made to your code. There are a lot of them.
/*Shoot For Your Grade *Calculate horizontal distance for angle via the height and range * *Edited by: TjL30 remove this if you wish;) */ import java.util.Scanner; public class shootcalculator{ public static Scanner input= new Scanner(System.in); public static void main(String [] args){ // Get gravity System.out.print("Enter gravity in m/s^2 (earth default is 9.8): "); double gravityFactor = input.nextDouble()/2; // Get the initial height System.out.println("Initial height of cannon (cm): "); double height = input.nextDouble(); // Get horizontal range System.out.println("Horizontal range of cannon (cm): "); double range = input.nextDouble(); // Calculate time for payload to hit the ground double time = (range/gravityFactor); System.out.println("Time: " + time); // Calculate muzzle velocity double velocity = (range/time); System.out.println("Muzzle Velocity: " + velocity); // Get firing angle double angle = input.nextDouble(); // Calculate verticle and horizontal velocity double horVel = velocity*Math.sin(angle); double verVel = velocity*Math.cos(angle); System.out.print("\nHorizontal Velocity: " + horVel); System.out.print("\nVerticle Velocity: " + verVel); // Calculate new time double discr = Math.sqrt((velocity * velocity) - (4 * (gravityFactor/(-2) * height))); System.out.print("\nDiscriminant: " + discr); double newTime; if ((velocity - discr) < 0){ newTime = ((velocity - discr)/2)*(gravityFactor/(-2)); }else{ newTime = ((velocity + discr)/2)*(gravityFactor/(-2)); } System.out.print("\nNew Time: " + newTime); // Calculate horizontal distance double horDis = horVel*newTime+(gravityFactor/-2)*Math.pow(newTime, 2); System.out.print("\nDISTANCE FIRED: " + horDis + "\nyay!"); } }
I added a few ( and ) in the code which might throw off the result if they are in the wrong place (I never took physics).
#3
Posted 05 April 2008 - 12:31 PM
on line 40
....
// Calculate new time
double discr = Math.sqrt((velocity * velocity) - (4 * (gravity/(-2) * height));
you need 1 more ( after height. Count them you never close your Math.sqrt.
....
// Calculate new time
double discr = Math.sqrt((velocity * velocity) - (4 * (gravity/(-2) * height));
you need 1 more ( after height. Count them you never close your Math.sqrt.
#4
Posted 05 April 2008 - 01:43 PM
on line 40
....
// Calculate new time
double discr = Math.sqrt((velocity * velocity) - (4 * (gravity/(-2) * height));
you need 1 more ( after height. Count them you never close your Math.sqrt.
that's not his only error, Just use The code I posted
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users