0

I am not terribly certain where the error(s) occurs within my code (else I would fix it myself). However--it negatively affects my output

enter image description here

I believe it may be a compilation of errors--additionally, I'm fairly certain my class is functioning properly (I think), while its tester is the source of the errors. Both the class and its tester are shown below (in order to accurately reproduce the output and its errors). This is the output I was expecting to calculate for:

enter image description here

Thank you very much for your assistance--I am truly at a loss.

/**
 * This class instantiates Catapult objects with eight private instance variables.
 * It contains one mutator methods to calculates the distance of a projectile fired by the catapult object.
 *
 * Private instance variables include gravity, degreeMeasure, velocity, and distance.
 *
 * @author A. Mackey
 * @version 01/12/14
 */
public class Catapult
{
    //declare private instance variables
    private double gravity = 9.79,                              //gravity affecting the projectile
                   degreeMeasure,                               //degree measurement at which the projectile is fired
                   velocity,                                    //velocity at which the projectile is fired (meters per second)
                   distance;                                    //distance which the projectile travels (in feet)

    //constructor for ojbects of type Catapult
    Catapult(double degMeasure, double velocityValue)
    {
        degreeMeasure = degMeasure;
        velocity = velocityValue / 2.23694;
    }

    /**
     * Mutator method which calculates the distance of a projectile fired by the catapult object (no parameter).
     * @return distance--returns double value for distance of the projectile's travel.
     */
    public double calcDistance()
    {
        return distance = ((Math.pow((velocity), 2) * Math.sin(2 * (Math.toRadians(degreeMeasure))) / gravity)) * 3.28084;
    }
}


/**
 * This class tests the CO2Footprint class.
 * An ArrayList of projectile objects is created to hold the instance variables within the constructor.
 *
 * A for loop is used to use the add() method to add the objects to the ArrayList as they are instantiated.
 * A second for loop is used to call the methods on each object in the ArrayList.
 * A third for loop is used to assign values to the 2d array containing the distance values
 * A fourth for loop is used to print the values of the instance variables for each object as well as other output information.
 *
 * @author A. Mackey
 * @version 01/12/14
 */
import java.util.ArrayList;                                    //import the ArrayList class
public class CatapultTester
{
    public static void main(String[] Args)
    {
        //declare and initialize local variables
        double distance[][] = new double[7][6],                //distance traveled by the projectile
               angle[] = {25, 30, 35, 40, 45, 50},             //angle of projection
               velocity[] = {20, 25, 30, 35, 40, 45, 50};      //velocity of projection
        int counter1 = 0,                                      //counter of first for loop
            counter2 = 0,                                      //counter of third for loop
            counter3 = 0,                                      //counter of fourth for loop
            counter4 = 0,                                      //counter used in fourth for loop for MPH value output
            objectArraylistCounter = 0;                        //counter in third for loop which set values to the distance array

        ArrayList<Catapult> projectile = new ArrayList<Catapult>();
        for(int i = 0; i < 6; i++)
        {
            projectile.add(new Catapult(angle[i], velocity[counter1]));
            if((i % 6) == 0)
            {
                counter1++;
                i = 0;
            }
            if(counter1 == 6)  
            {
                i = 7;
            }
        }

        Catapult dataRecord;                              //creates a new dataRecord object of type ShapesV11

        for(int index = 0; index < projectile.size(); index++)
        {
            dataRecord = projectile.get(index);
            dataRecord.calcDistance();
        }

        for(int i = 0; i < 6; i++)
        {
            dataRecord = projectile.get(objectArraylistCounter);
            distance[counter2][i] = dataRecord.calcDistance();
            if((i % 5) == 0)
            {
                counter2++;
                i = 0;
            }
            if(counter2 == 6)
            {
                i = 6;
            }
        }

        //print output
        System.out.println("                           Projectile Distance (feet)");
        System.out.println("  MPH      25 deg      30 deg      35 deg      40 deg      45 deg      50 deg");
        System.out.print("=================================================================================");
        for(int i = 0; i < 7; i++)
        {
            if((counter4 % 5) == 0)
            {
                System.out.print("\n   " + (int)velocity[(counter4 / 5)]);
            }

            System.out.printf("%12.2f", distance[counter3][i]);
            if((i % 5) == 0)
            {
                counter3++;
                i = 0;
            }
            if(counter3 == 7)
            {
                i = 8;
            }
            counter4++;
        }
    }
}
1
  • For anyone curious or learning from my mistakes and the answerer's answer--this is my final revision and its output (respectively): pastebin.com/RgEfKdyu and i.imgur.com/aDsjEVq.png Commented Jan 13, 2014 at 12:11

1 Answer 1

1

Using counters on 2d arrays are messy. I suggest you change those to nested for loops.. i.e.

List<Catapult> projectile = new List<Catapult>();
for (int i = 0; i < angle.Length; i++)
{
    for (int j = 0; j < velocity.Length; j++)
    {
        Catapult cata = new Catapult(angle[i], velocity[j]);
        projectile.Add(cata);
        cata.calcDistance();
    }
}

Then, accessing your array would be similar.

//PrintHeader();
int cataCtr = 0;
for (int i = 0; i < angle.Length; i++)
{
    if(i == 0) // PrintNewLineAndAngle();
    for (int j = 0; j < velocity.Length; j++)
    {
        Catapult cata = projectile[cataCtr];
        // PrintCataDistance();
        cataCtr++;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the nested loops suggestion--it really beautified my code. I absolutely should've thought of this--stressing before the end of the semester tomorrow, and this is my final submission before my final. It all functions just fine with this new code: pastebin.com/siV6Cgps --however, the output is still wonky. It's only printing a true value for my first distance--and I'm not quite sure what my error is in the new tester. It looks like this: i.imgur.com/bfBVjTf.png Any clue what might be wrong here?
Also--I'd love to upvote your comment and give you rep or whatever--but I'm too new here to do that, and that aside, my rep got absolutely bombed from my last question. Bunch of high-rep people wrecked my post because of my commenting. Thanks a million though, man.
base on what i see.. objectArraylistCounter should be initialized to 0 before the loop, and be incremented by one inside the loop. like what i did in cataCtr on my example.
Forgot the "objectArraylistCounter++;" Thanks man, you're a great guy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.