user3502827
user3502827

Reputation: 11

How do I display my Object's parameters using a method

public class Vector
 {
   private double x;
   private double y;


public Vector (double x_comp, double y_comp)
{
  x= x_comp;
  y= y_comp;  
}

public double getX ()
{

}


public double getY ()
{

}


public double magnitude ()
{

}


public double dotProduct (Vector vec)
{

}


public Vector addVectors (Vector vec)
{
   // return new Vector (add parameters);
}


public Vector unitVector ()
{
    // return new Vector (add parameters);
}


public double angleBetween (Vector vec)
{

}


public String formatAsString ()
{

}
public static void main(String[]args){
   Vector v1 = new Vector (-3, 2);
}
} 

I'm in the midst of building a program that deals with vectors. However, for the first two methods, I'm wondering how can I display the x coordinate and y coordinate of my Vector objects. Is there anyway for me to use the methods to display the coordinates of the Vector objects I create?

Upvotes: 0

Views: 46

Answers (3)

Jason C
Jason C

Reputation: 40335

Displaying the values is part of your user interface. The actual implementation of Vector is part of the application logic. These distinctions apply both to simple programs and very complex applications.

Usually, it's beneficial to keep UI separate from application logic / implementation. In your case, all this really translates to is that you simply want to do this at a higher level. So:

  1. Implement your getters to return the correct values.

  2. Higher up in your application, a simple System.out.println("The X coord is " + myVector.getX()) will do.

By not adding print-out code directly to Vector, you are now free to display (or not display) the values in any format you wish.

You could also override toString(), but generally this is just done for debugging (that may be your goal) rather than meaningful formatting. The other answers here describe that option well (e.g. christopher's answer). Then you can simply System.out.println(myVector).

Note that the above concepts still apply to toString(): You're still printing at a higher level, rather than from within Vector itself, which is the take-home point here.

Upvotes: 1

christopher
christopher

Reputation: 27346

In Java, all classes implicitly extend the Object type. The Object class specifies a method, toString(). This method is designed to output a meaningful String representing the class. This can be overridden in subclasses.

@Override
public String toString()
{
    return "Meaningful String";
}

In your example, it can be something like:

public String toString()
{
    return "X: " + x + ", Y: " + y;
}

NOTE: For objects with a lot of parameters, it's better to use a StringBuilder. This provides a mutable version of String, so you don't need to concatenate several values and create several instances of type String.

For example, if you have a more complex Vector, that describes a three dimensional movement with respect to time, then you might have x, y, z and t. In this case, a toString() that looks like the following will be more efficient:

public String toString()
{
    StringBuilder builder = new StringBuilder();
    builder.append("X: ");
    builder.append(x);
    builder.append("Y: ");
    builder.append(y);
    builder.append("Z: ");
    builder.append(z);
    builder.append("T: ");
    builder.append(t);

    return builder.toString();
}

And this can be called by calling something like.

Vector vec = new Vector(3,-1);

System.out.println(vec);

// Outputs: X: 3, Y: -1

Upvotes: 3

snapperdragon
snapperdragon

Reputation: 96

Override the toString method and print both x and y

@Override
public String toString() {
   return "x value is "+ x ", y value is " + y;
}

Upvotes: 0

Related Questions