Coordinate (ArrayList) - java

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.

Related

Circle exercise Java?

This is not a homework,this is an exercise in a Java book I am learning by myself.
Build a class with the name circle which represents a circle in the coordinate plane.The fields of the class should be radius length,dy coordinates of the center.The methods of the class should be :
getArea() : Finds the area of the circle
getPerimeter() : Finds the perimeter of the circle
moveCircle() : Changes the coordinates of the center of the circle
modifyRadius() : Modifies the radius of the circle
private int x, y;
public Circle() {
x = 0;
y = 0;
radius = 1;
}
public Circle(int x, int y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
public double getPerimeter() {
return 2 * radius * Math.PI;
}
Now how do I continue this? for moveCircle and ModifyRadius?
There should be a variable called radius in your class.
private int radius;
to move circle, you should pass your new location of the center to the object. Then it will set the new position.
public void moveCircle(int newX, int newY) {
this.x = newX;
this.y = newY;
}
To modify the radius you also can use the same method.
public void ModifyRadius (double newRadius) {
this.radius = newRadius;
}
Another way is to create setters for the variables.
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public void moveCircle (int x, int y) {
setX(x);
setY(y);
}
public void setRadius (double radius) {
this.radius = radius;
}

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;
}
}

Calling the proper constructor based on input length

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.

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