Help - Search - Members - Calendar
Full Version: Java Error
Pixel2Life Forum > Help Section > Desktop Programming
npsken
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:
CODE
/*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!");
}
}


And here are the errors:
Click to view attachment

If anybody could help, it would be appreciated.
tjl30
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.

CODE
/*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).
CoryMathews
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.
tjl30
QUOTE (CoryMathews @ Apr 5 2008, 01: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.


that's not his only error, Just use The code I posted smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.