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;
}
}
Related
So i am trying to add collision detection for a little game i am creating and i am specifying the width and height of my defender class however i am unable to do so. My code below gives me an error saying the global variable width and height do not exist
class Defender
{
int x,y;
Defender(int x, int y)
{
this.x = x;
this.y = y;
}
void render()
{
//draw a defender
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,100);
rect(x,y-10,20,10);
}
boolean collision()
{
color detectedColour;
for (int i=y; i<y+ Defender.height; i++) {
detectedColour = get(x + Defender.width, i);
if (detectedColour == Alien1) {
return true;
}
}
return false;
}
}
The problem is you're trying to access the variables statically when they do not exist in the class. Create class members that represent the width and height of the defender.
class Defender {
private final int x;
private final int y;
private final int width;
private final int height;
public Defender(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// getters
}
Defender defender = new Defender(0, 0, 1, 1);
int width = defender.width;
int height = defender.height;
This is a difficult problem to describe so I will do my best.
Basically my program has two concrete classes: MovablePoint and MovableCircle. Both implement the interface Movable which has four methods: moveUp Down left right.
Now where I am stuck is that when I am constructing my MovableCircle I need to use an instance variable of MovablePoint to represent the center of the circle. However I also need the constructor of MovableCircle to take in 5 params: int x, int y, int xSpeed, int ySpeed, int radius. The first 4 params are the "center" of the circle and they should be instantiated by the MovablePoint.
Here is what I have:
public class MovableCircle implements Movable {
private int radius;
private int y;
private int x;
private int xSpeed;
private int ySpeed;
public MovablePoint circleCenter;
public MovableCircle (int x, int y, int xSpeed, int ySpeed, int radius) {
this.radius = radius;
this.x = circleCenter.getX();
this.y = circleCenter.getY();
this.xSpeed = circleCenter.getxSpeed();
this.ySpeed = circleCenter.getySpeed();
}
Here also is the MovablePoint code if that helps:
public class MovablePoint implements Movable{
private int x;
private int y;
private int xSpeed;
private int ySpeed;
public MovablePoint(int x, int y, int xSpeed,
int ySpeed) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
Obviously there is more code beyond what I have posted but I am fairly positive I have coded the class methods properly.. I believe my problem lies in the fact that I need to instantiate the MovableCircle's center with a MovablePoint but I'm not sure how to remedy it. Thanks for your help and I apologize if this problem is easily solved and I am just oblivious..
How about something like:
public class MovableCircle implements Movable {
private int radius;
private MovablePoint circleCenter;
public MovableCircle (int x, int y, int xSpeed, int ySpeed, int radius) {
this.radius = radius;
this.circleCenter = new MovablePoint(x, y, xSpeed, ySpeed);
}
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;
}
}
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.
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.