Calling the proper constructor based on input length - java

I have a Point class.
The point can be both in 2-D and 3-D. I am deciding this based on the length of the coordinate array passed to the constructor.
double x, y, z;
int dimension;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
Point(double x, double y) {
this.x = x;
this.y = y;
this.z = 0;
dimension = 2;
}
Point(double[] p)
{
if(p.length == 2)
this(p[0], p[1]);
else if(p.length == 3)
this(p[0], p[1], p[2]);
}
The last constructor gives error because constructor call must be the first statement in a constructor.
Is there a way to achieve what I'm doing?

May be you can do something like
double x, y, z;
int dimension;
Test(double x, double y, double z) {
initDim(x, y, z);
}
Test(double x, double y) {
initDim(x, y);
}
Test(double[] p)
{
if(p.length == 2)
initDim(p[0], p[1]);
else if(p.length == 3)
initDim(p[0], p[1], p[2]);
}
private void initDim(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
private void initDim(double x, double y) {
initDim(x, y, 0);
dimension = 2;
}

Addition to those i would like suggest a solution like this. Less constructors and easily adaptable. Bit similar to what you do to create a singleton.
public class Point {
double x, y, z;
int dimension;
private Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
dimension = 3;
}
private Point(double x, double y) {
this.x = x;
this.y = y;
this.z = 0;
dimension = 2;
}
public Point getInstance(double x, double y, double z) {
return new Point(x, y, z);
}
public Point getInstance(double x, double y) {
return new Point(x, y);
}
public Point getInstance(double[] p) {
if (p.length == 2)
return new Point(p[0], p[1]);
else (p.length == 3)
return new Point(p[0], p[1], p[2]);
}
}
You can create your instance like this.
Point point = Point.getInstance(0, 0);

It's generally not good to have too many constructors. It gives you a good opportunity to express how it should be used.
public static Point Create(double... p)
{
if(p.length == 2)
return Point(p[0], p[1]);
else if(p.length == 3)
return Point(p[0], p[1], p[2]);
// default case or throw error
}
Alternatively you can create an initialize method, which you call from the constructors.

Related

Text Objects for displaying live text on screen with Processing + Eclipse

I currently have a multi-class program that uses the processing library set up in Eclipse. I was wondering if there if there is a Text object in a 3rd party library somewhere that I can use to create text objects on the screen, and crucially, move these text objects around without having to redraw them to the screen. Are there any such classes out there?
Eg. a class called Text init as Text textObject = new Text("String", x, y)
with a method similar to textObject.move(dx, dy)
If you don't find a library to do it, here's a minimal implementation. If you like, I could wrap it up in a library so that the objects draw themselves. If you're using eclipse, you'll probably need to add some parent.s before the PApplet calls.
TextObject to;
void setup(){
size(400,400);
to = new TextObject("test", 0, height/2);
}
void draw(){
background(0);
if(frameCount % 300 == 0) to.set(0, height/2);
to.move(1,0);
to.display();
}
class TextObject
{
float x, y;
String text;
PFont f;
TextObject(String s, float x, float y){
this.x = x;
this.y = y;
this.text = s;
f = createFont("Georgia", 32);
}
TextObject(String s, float x, float y, PFont f){
this.x = x;
this.y = y;
this.text = s;
this.f = f;
}
void set(float x, float y){
this.x = x;
this.y = y;
}
void move(float dx, float dy){
x += dx;
y += dy;
}
void display(){
textFont(f);
text(text, x, y);
}
}

JAVA: Trying to change an object without making a new object

I am working with a Point object that has an x and y component, Point(double x, double y). I want to write a function that changes the values of the x and y component without having a
new Point p = ...
For Example, this is my current version:
public class Point{
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point movePoint(double dx, double dy) {
return new Point(this.x + dx, this.y + dy);
}
}
Is it possible to do something like movePoint() without making a new Point?
Thanks in advance.
Certainly. Just change your code to return a reference to this as such:
public Point movePoint(double dx, double dy) {
this.x += dx;
this.y += dy;
return this;
}
Also note that Java has a built in class for storing double precision points in its java.awt.geom.Point2D.Double class.
Sure, try this:
public class Point{
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point movePoint(double dx, double dy) {
this.x += dx;
this.y += dy;
return this;
}
}

Coordinate (ArrayList)

I'm having trouble formatting when using coordinates.
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
}
So, later, when I'm trying to find the location of my rabbit, I use:
Coordinate (x, y) = rabbit.get(i);
And that doesn't work, but this does:
Coordinate z = rabbit.get(i);
I want to find the x and y values so I'm confused as to how to do that and why the Coordinate (x, y) doesn't work. Thanks for your help!
As your attributes x,y of Coordinate are public:
Coordinate z = rabbit.get(i);
int xCor = z.x; //this is your x coordinate
int yCor = z.y; //this is your y coordinate
Normaly these attriubtes are private and you access them with a getter/setter-Method:
public class Coordinate {
private int x;
private int y;
public Coordinate( int x, int y) {
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public void setX(int newX){
this.x = newX;
}
//same for Y
}
//in the main program.
Coordinate z = rabbit.get(i);
int yourX = z.getX() //this is your x coordinate
int yourY = z.getY() //this is your y coordinate
I assume you use Java, so I added the Tag, this enables highlighting. This works with other languages in the same way.

Adding negative zero floats results in 1.0f only when using custom Vector class

When I calculate the dot product of two vectors a = (-0, -0, -5) and b = (0, 1, 0) using my custom Vector class, it gives me 1 when it should've given 0. However, when I use PVector, it works as expected. I took a look at the source code for PVector, but I noticed nothing that was different for what I was trying to do.
I suspect this has something to do with how -0.0f is equal to +1.0f and I know I could just use PVector, but it's strange that my implementation is broken. Does anyone know why it's broken? (I'm using Processing 1.5.1)
Simple test case comparing dot products for PVector and my Vector:
void setup() {
Vector a = new Vector(-0, -0, -5);
Vector b = new Vector(0, 1, 0);
println(a.dotProduct(b));
PVector pA = new PVector(-0, -0, -5);
PVector pB = new PVector(0, 1, 0);
println(pA.dot(pB));
}
class Vector {
public float x, y, z, w;
Vector(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
this.w = 1;
}
void doScale(float scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
}
float dotProduct(Vector other) {
return this.x * other.x + this.y + other.y + this.z * other.z;
}
}
This is the problem:
float dotProduct(Vector other) {
return this.x * other.x + this.y + other.y + this.z * other.z;
}
You're adding this.y and other.y together, instead of multiplying them. It should be:
float dotProduct(Vector other) {
return this.x * other.x + this.y * other.y + this.z * other.z;
}
Personally I'd add brackets for readability:
float dotProduct(Vector other) {
return (this.x * other.x) + (this.y * other.y) + (this.z * other.z);
}
Typo: should be this.y * other.y perhaps?

Java initializing classes without repeating myself

Is it possible to rewrite the following a bit more concise that I don't have to repeat myself with writing this.x = x; two times?
public class cls{
public int x = 0;
public int y = 0;
public int z = 0;
public cls(int x, int y){
this.x = x;
this.y = y;
}
public cls(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
BoltClock's answer is the normal way. But some people (myself) prefer the reverse "constructor chaining" way: concentrate the code in the most specific constructor (the same applies to normal methods) and make the other call that one, with default argument values:
public class Cls {
private int x;
private int y;
private int z;
public Cls(int x, int y){
this(x,y,0);
}
public Cls(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
Call the other constructor within this overloaded constructor using the this keyword:
public cls(int x, int y, int z){
this(x, y);
this.z = z;
}
Read about constructor overloading
http://www.javabeginner.com/learn-java/java-constructors
You can use initilization block for this purpose.
Very simple: Just write an initialize function like this:
public class cls{
public int x = 0;
public int y = 0;
public int z = 0;
public cls(int x, int y){
init(x,y,0);
}
public cls(int x, int y, int z){
init(x,y,z);
}
public void init(int x, int y, int z ) {
this.x = x;
this.y = y;
this.z = z;
}
}

Categories

Resources