Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:57:04+00:00 2026-06-18T12:57:04+00:00

I’m making a pretty simple Java program that calculates the distance a projectile will

  • 0

I’m making a pretty simple Java program that calculates the distance a projectile will travel when launched at different angles and speeds. While it isn’t necessary to include angles above 180 degrees when talking about launch angle, I’m wondering why my code isn’t working the way I expect it to, in particular the following portion:

if((int)(angle / 180.0) % 2 != 0)
{
     myDataArray[i][j] = 0.0;
}

If angle is a value between 180 and 360 (non-inclusive), then it should satisfy the condition, as should any equivalent angle (e.g. 540 to 720 non inclusive). Basically, whenever the firing angle is into the ground, the distance value should be zero.

The rest of the code is working fine, but this portion is being satisfied by ALL angle values 180 and up, meaning ALL those distances are zero. (This doesn’t make sense since a 365 degree launch angle should be the same as a 5 degree launch angle).

Am I doing something wrong, or is there an alternate condition I can use to get the desired result?

EDIT: Huge sigh….
There was no problem with the calculation, I’m just an idiot. I put the angle incrementor inside the else statement, meaning the first angle that read true for the if statement would be used for the rest of the program. Here’s the entire method code I had:

public void calcDistanceValues()
{
    double angleStart = myAngleDegrees;
    double speedStart = mySpeedMPH;

    for(int i = 0; i < myNumRows; i++)
    {
        for(int j = 0; j < myNumColumns; j++)
        {
            if((int)(myAngleDegrees / 180.0) % 2 != 0)
            {
                myDataArray[i][j] = 0.0;
            }
            else
            {
                myDataArray[i][j] = 5280 *  (Math.pow(mySpeedMPH,2)*Math.sin(2*Math.toRadians(myAngleDegrees))/78973);
                myAngleDegrees += myUnitIncrement; //the problem is here*
            }

        }            
        mySpeedMPH += myUnitIncrement;
        myAngleDegrees = angleStart;            
    }
    mySpeedMPH = speedStart;    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:57:06+00:00Added an answer on June 18, 2026 at 12:57 pm

    I think the test is correct, and the bug is in how it is being used. I wrote a simple test program to try it out:

    public class Test {
    
      public static void main(String[] args) {
        test(0);
        test(179);
        test(180);
        test(181.0001);
        test(359);
        test(360.0001);
        test(180+360);
      }
    
      private static void test(double angle){
        System.out.println("Raw angle: "+angle+" modded: "+modAngle(angle));
      }
    
      private static double modAngle(double angle) {
        if ((int) (angle / 180.0) % 2 != 0) {
          return 0;
        } else {
          return angle;
        }
      }
    }
    

    The results were:

    Raw angle: 0.0 modded: 0.0
    Raw angle: 179.0 modded: 179.0
    Raw angle: 180.0 modded: 0.0
    Raw angle: 181.0001 modded: 0.0
    Raw angle: 359.0 modded: 0.0
    Raw angle: 360.0001 modded: 360.0001
    Raw angle: 540.0 modded: 0.0
    

    If I understand your requirements, it seems to be working.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.