0

So, I'm making a program that calculates the distance given seven velocities and six angles of trajectory. Given the formula to conduct the necessary 42 operations, I must store the results in a two-dimensional array and print the results in a neat and orderly fashion.

I am having confusion on why a particular part of my program is not working. It seems alright to me, however the output is not what I was expecting because it's random and illogical. The error is the output given from the code which results in "Catapult@xxxxx...." where the "x"s represent a random series of letters, numbers, or characters. I'm not sure where they come from and why, so I would like an explanation, and how to fix this problem of mines. The source code is below:

static final int ROWS = 7;
static final int COLS = 6;

public static void main(String[] args)
{
    double[] angles = {25.0, 30.0, 35.0, 40.0, 45.0, 50.0};
    double[] rads = new double[COLS];
    for(int i = 0; i < COLS; i++)
    {
        rads[i] = Math.toRadians(angles[i]);
    }
    double[] speeds = {20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0};

    Catapult[][] projectile = new Catapult[ROWS][COLS];
    for(int a = 0; a < ROWS; a++)
    {
        for(int b = 0; b < COLS; b++)
        {
            projectile[a][b] = new Catapult(speeds[a], rads[b]);
        }
    }

If you were to type System.out.print(projectile[a][b] + " "); after projectile[a][b] = new Catapult(speeds[a], rads[b]); in the second for-loop, then it would result in the error described earlier.

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
  • 1
    You need to override `toString` in `Catapult`. – Elliott Frisch Dec 18 '15 at 03:05
  • Thanks for answering, but could you explain that a bit more? I'm not sure what you mean by override `toString` in `Catapult`. – Tetramental Dec 18 '15 at 03:08
  • That's a comment, but you're getting the default `toString` from `Object`. See [Overriding and Hiding](https://docs.oracle.com/javase/tutorial/java/IandI/override.html) from the Java Tutorials. – Elliott Frisch Dec 18 '15 at 03:12

1 Answers1

0

You can try this:

class Catapult {

    @Override
    public String toString() {
        // Do here what you want to print
        return "Catapult{}";
    }
}

Edit:

public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode()) Returns: a string representation of the object.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68