Point isAbove, isUnder, isRight, isLeft methods - java

This is my point class;
public class Point
{
private int _x;
private int _y;
public boolean isAbove (Point other)
{
if (_y <= other._y)
return false;
else
return true;
}
public boolean isUnder (Point other)
{
return isAbove(other);
}
public boolean isLeft (Point other)
{
if (_x >= other._x)
return false;
else
return true;
}
public boolean isRight (Point other)
{
return isLeft(other);
}
}
All the above methods not functional as expected and i cannot find what i am doing wrong.
For example:
Point pp1 = new Point(1, 0);
Point pp2 = new Point(0, 0);
System.out.println("isAbove: " + pp1.isAbove(pp2));
System.out.println("isUnder: " + pp1.isUnder(pp2));
System.out.println("isLeft : " + pp1.isLeft(pp2));
System.out.println("isRight: " + pp1.isRight(pp2));
Return all false.

Try this, it should work:
public boolean isAbove (Point other)
{
return _y > other._y;
}
public boolean isUnder (Point other)
{
return other.isAbove(this);
}
public boolean isLeft (Point other)
{
return _x < other._x;
}
public boolean isRight (Point other)
{
return other.isLeft(this);
}

You should negate isRight and isUnder and change <= to < and >= to >.
public class Point {
private int _x;
private int _y;
public Point(int x, int y) {
this._x = x;
this._y = y;
}
public boolean isUnder (Point other)
{
return _y < other._y;
}
public boolean isAbove (Point other) {
return _y > other._y;
}
public boolean isLeft (Point other) {
return _x < other._x;
}
public boolean isRight (Point other)
{
return _x > other._x;
}
}

Related

Stackoverflow Error when working with larger numbers

I'm trying to write the a* path finding algorithm using recursion and am having trouble. The code finds the path to smaller distances but when I try to use it with larger numbers, I get a stack overflow error. I am new to recursion so there is definitely something I am doing wrong but I can't figure it out. I have an exit condition and it works fine on smaller numbers so I don't know what to do unless I have a problem with the algorithm itself. The problem occurs in the findAlgorithm() method where the recursion is being done.
Algorithm:
Node[][]nodes;
ArrayList<Node> options = new ArrayList<Node>();
ArrayList<Node> path = new ArrayList<Node>();
private int x, y, endX, endY;
Node endNode;
public Algorithm(Node[][]nodes) {
this.nodes = nodes;
setStartPosition();
findEndPosition();
}
public ArrayList<Node> getPath() {
Node node = endNode;
while(node.hasParent()) {
path.add(node);
node = node.getParent();
}
return path;
}
public void findPath() {
if(x!=endX || y!=endY) {
getSurroundingNodes();
if(options.size()>0) {
Node lowestVal = options.get(0);
for(Node i : options) {
if(i.getValue()<lowestVal.getValue()) {
lowestVal = i;
}
else if(i.getValue() == lowestVal.getValue()) {
if(i.getEndDistance()<lowestVal.getEndDistance()) {
lowestVal = i;
}
}
}
x = lowestVal.getX();
y = lowestVal.getY();
lowestVal.setTaken();
options.remove(lowestVal);
findPath();
}
}
else {
System.out.println("Made it");
}
}
private void getSurroundingNodes() {
Node parent = nodes[y][x];
for(int i=y-1;i<y+2; i++) {
for(int z=x-1;z<x+2; z++) {
if(i>=0&&z>=0&&i<nodes.length&&z<nodes[i].length) {
Node node = nodes[i][z];
double endDistance = Math.sqrt((i-endY)*(i-endY)+(z-endX)*(z-endX));
double startDistance = parent.getStartDistance()+Math.sqrt((i-y)*(i-y)+(z-x)*(z-x));
double value = endDistance+startDistance;
if(!node.isWall()&&!node.isTaken()) {
if(!options.contains(node)) {
node.setParent(parent);
node.setX(z);
node.setY(i);
node.setStartDistance(startDistance);
node.setEndDistance(endDistance);
node.setValue(value);
options.add(node);
}
else {
if(node.getValue()<value) {
//do nothing
}
else if (node.getValue() == value) {
if(node.getEndDistance()<=endDistance) {
//do nothing
}
else {
node.setParent(parent);
node.setX(z);
node.setY(i);
node.setStartDistance(startDistance);
node.setEndDistance(endDistance);
node.setValue(value);
options.add(node);
}
}
else {
node.setParent(parent);
node.setX(z);
node.setY(i);
node.setStartDistance(startDistance);
node.setEndDistance(endDistance);
node.setValue(value);
options.add(node);
}
}
}
}
}
}
}
private boolean findEndPosition() {
for(Node[] i : nodes) {
for(Node z : i) {
if(z.isEnd()) {
endX = z.getX();
endY = z.getY();
endNode = z;
return true;
}
}
}
return false;
}
private boolean setStartPosition() {
for(Node[] i : nodes) {
for(Node z : i) {
if(z.isStart()) {
z.setTaken();
x = z.getX();
y = z.getY();
return true;
}
}
}
return false;
}
Node:
private int x, y;
private boolean wall, start, end, taken, hasAParent;
private double endDistance, startDistance, value;
private Node parent = null;
public Node() {
wall=false;
start=false;
end=false;
taken=false;
hasAParent = false;
}
public boolean hasParent() {
return hasAParent;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isWall() {
return wall;
}
public void setWall() {
wall = true;
end = false;
start = false;
}
public boolean isStart() {
return start;
}
public void setStart(int x, int y) {
this.x = x;
this.y = y;
start = true;
end = false;
wall = false;
endDistance=0.0;
startDistance=0.0;
value=0.0;
}
public boolean isEnd() {
return end;
}
public void setEnd(int x, int y) {
this.x = x;
this.y = y;
end = true;
start = false;
wall = false;
hasAParent = true;
}
public boolean isTaken() {
return taken;
}
public void setTaken() {
taken = true;
}
public double getEndDistance() {
return endDistance;
}
public void setEndDistance(double endDistance) {
this.endDistance = endDistance;
}
public double getStartDistance() {
return startDistance;
}
public void setStartDistance(double startDistance) {
this.startDistance = startDistance;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
hasAParent = true;
}

How to override equals in base and child class

I have one example where I have overriden equals methods in both base class and child class.
package com.test;
public class Point2D {
private int x = 0;
private int y = 0;
public Point2D(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public String toString() {
return (x + " " + y);
}
#Override
public boolean equals(Object o) {
if (Point2D.class != o.getClass()) {
return false;
}
if ((o instanceof Point2D)) {
if ((((Point2D) o).x == this.x) && (((Point2D) o).y == this.y)) {
return true;
}
}
return false;
}
#Override
public int hashCode() {
return x + y;
}
}
class TestPoint2D {
public static void main(String args[]) {
Point2D d2 = new Point2D(2, 4);
Point2D d3 = new Point2D(2, 4);
Point3D d4 = new Point3D(2, 4, 5);
Point3D d5 = new Point3D(2, 4, 5);
System.out.println(d2.equals(d3));
System.out.println(d3.equals(d5));
System.out.println(d5.equals(d3));
System.out.println(d4.equals(d5));
}
}
class Point3D extends Point2D {
private int z = 0;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
#Override
public boolean equals(Object o) {
if ((o instanceof Point3D)) {
if ((((Point3D) o).z == this.z)) {
Point2D obj = (Point2D) o;
return super.equals(obj);
}
}
return false;
}
#Override
public int hashCode() {
return super.hashCode() + z;
}
}
While testing I'm getting output :
true
false
false
false
The expected output is :
true
false
false
true
Could anyone tell me what is missing here ?
As you saw, your code declares two equal Point3D objects (subclass objects) not equal, here d4 and d5. I think this is because of this if statement in Point2D.equals():
if (Point2D.class != o.getClass()) {
return false;
}
o is d5, so its getClass() returns Point3D.class, not the same as Point2D.class. The result returned is also returned from Point3D.equals() and is printed. I think that instead you want
if (getClass() != o.getClass()) {
Jim Garrison is correct, of course, that you can use a debugger to discover.

How can I get value from one class to another without extends it?

I am newbie in programming and may be this will be stupid question but here it is:
This is my class board:
public class Board {
public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;
PlayingPiece[][] board;
private boolean isFirstMove;
private int color;
public Board() {
this.setBoard(new PlayingPiece[8][8]);
this.isFirstMove = true;
this.initializePieces();
}
// Initialize the chess pieces
public void initializePieces() {
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(1, i, COLOR_WHITE);
}
for (int i = 0; i < 8; i++) {
board[6][i] = new Pawn(6, i, COLOR_BLACK);
}
board[0][0] = new Rook(0, 0, COLOR_WHITE);
board[0][7] = new Rook(0, 7, COLOR_WHITE);
board[7][0] = new Rook(7, 0, COLOR_BLACK);
board[7][7] = new Rook(7, 7, COLOR_BLACK);
board[0][1] = new Knight(0, 1, COLOR_WHITE);
board[0][6] = new Knight(0, 6, COLOR_WHITE);
board[7][1] = new Knight(7, 1, COLOR_BLACK);
board[7][6] = new Knight(7, 6, COLOR_BLACK);
board[0][2] = new Officer(0, 2, COLOR_WHITE);
board[0][5] = new Officer(0, 5, COLOR_WHITE);
board[7][2] = new Officer(7, 2, COLOR_BLACK);
board[7][5] = new Officer(7, 5, COLOR_BLACK);
board[0][3] = new Queen(3, 0, COLOR_WHITE);
board[0][4] = new King(4, 0, COLOR_WHITE);
board[7][3] = new Queen(7, 3, COLOR_BLACK);
board[7][4] = new King(7, 4, COLOR_BLACK);
this.printBoard();
}
public boolean play(int color, int fromX, int fromY, int toX, int toY) {
boolean isTrue = false;
// Check if this is the first turn and only white can move
if (isFirstMove && color == COLOR_WHITE) {
isTrue = true;
} else if (isFirstMove && color == COLOR_BLACK) {
return false;
}
// check if player plays 2 times in a raw and if you move the piece from
// current possition
if (color == this.color || (toX == fromX && toY == fromY)) {
return false;
}
isTrue = true;
if (isTrue == true) {
this.isFirstMove = false;
// Check if player plays with his own color
if (((board[fromX][fromY]).getColor() != color)) {
return false;
}
// Check the isLegal movement of every chess piece
if ((board[fromX][fromY]).move(toX, toY)) {
board[toX][toY] = board[fromX][fromY];
board[fromX][fromY] = null;
}
this.printBoard();
}
return isTrue;
}
public PlayingPiece[][] getBoard() {
return board;
}
public void setBoard(PlayingPiece[][] board) {
this.board = board;
}
I want to get the value of this:
board[toX][toY];
OK after that here is the other my class for chess pieces:
public class PlayingPiece {
public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;
public static final char BLACK_PAWN = '\u265F';
public static final char BLACK_ROOK = '\u265C';
public static final char BLACK_KNIGHT = '\u265E';
public static final char BLACK_BISHOP = '\u265D';
public static final char BLACK_QUEEN = '\u265B';
public static final char BLACK_KING = '\u265A';
public static final char WHITE_PAWN = '\u2659';
public static final char WHITE_ROOK = '\u2656';
public static final char WHITE_KNIGHT = '\u2658';
public static final char WHITE_BISHOP = '\u2657';
public static final char WHITE_QUEEN = '\u2655';
public static final char WHITE_KING = '\u2654';
public static final char NO_PIECE = ' ';
private int x, y;
private boolean isAlive;
private int color;
private char symbol;
protected PlayingPiece (int newX, int newY, int newColor) {
this.setX(newX);
this.setY(newY);
this.color = newColor;
this.isAlive = true;
}
protected PlayingPiece(int newX, int newY) {
this.setX(newX);
this.setY(newY);
}
protected PlayingPiece() {
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
protected boolean moveIsLegal (int newX, int newY) {
boolean isLegal = false;
if ((0 <= newX && newX <= 7) && (0 <= newY && newY <= 7)){
isLegal = true;
}
return isLegal;
}
public boolean move (int newX, int newY) {
if (moveIsLegal(newX, newY)) {
setX(newX);
setY(newY);
return true;
}
return false;
}
public int getColor() {
return color;
}
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public char getSymbol() {
return symbol;
}
public void setSymbol(char symbol) {
this.symbol = symbol;
}
}
And now this is Pawn class which extends PlayingPieces:
public class Pawn extends PlayingPiece {
private boolean hasBeenMoved;
protected Pawn(int newX, int newY, int color) {
super(newX, newY, color);
this.hasBeenMoved = false;
if (color == COLOR_BLACK) {
this.setSymbol(BLACK_PAWN);
} else {
this.setSymbol(WHITE_PAWN);
}
}
#Override
public boolean move(int newX, int newY) {
if (super.move(newX, newY)) {
this.hasBeenMoved = true;
return true;
}
return false;
}
#Override
protected boolean moveIsLegal(int newX, int newY) {
boolean isLegal = false;
int newPositionX = newX - this.getX();
if (super.moveIsLegal(newX, newY)) {
if ((hasBeenMoved == false)
&& (((Math.abs(newPositionX) <= 2) && getY() == newY))) {
isLegal = true;
} else if ((hasBeenMoved == true)
&& (((Math.abs(newPositionX) <= 1) && getY() == newY)) && isValidTrace(newX, newY)) {
isLegal = true;
}
}
return isLegal;
}
public boolean isValidTrace(int newX, int newY) {
PlayingPiece[][] array = new PlayingPiece[8][8];
if (array[newX][newY].equals(new PlayingPiece())) {
return false;
}
return true;
}
}
Now in this method isValidTrace() I want to get the value of board[toX][toY] from class Board and how can I do this without any extends here ?
Another solution (assuming you won't need multiple Board instances) is making board static.
public static PlayingPiece[][] board;
Then you can access it from your Pawn class using Board.board
I do not know if I understand your question but..
If you have got 2 classes A and B instead of using inheritance you can use aggregation. For example, you want to use some method from object B in class A you go with:
class A {
private B b;
}
and then inside methods of class A you go with b.nameOfTheMethod()
You can create a method to return the array and call it in the class you want the data.
public PlayingPiece getBoard()
{
return board;
}

Clones method is not working

I seem to be getting an error at the catch CloneNotSupportedException.
public class Segment extends Point implements Cloneable {
private Point p1, p2;
public Segment() {
this.p1 = new Point();
this.p2 = new Point();
}
public Segment clone() {
try {
Segment cloned = (Segment) super.clone();
cloned.p1 = (Point) p1.clone();
cloned.p2 = (Point) p2.clone();
return (cloned);
} catch (CloneNotSupportedException cnse) { // This is the error
cnse.printStackTrace();
return null;
}
}
}
package myclasses;
public class Point implements Cloneable
{
private int x, y;
public Point()
{
x = 0;
y = 0;
}
public Point(int X, int Y)
{
this.x = X;
this.y = Y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
private void setX(int x)
{
this.x = x;
}
private void setY(int y)
{
this.y = y;
}
public void setPoint(int newX, int newY)
{
getX();
getY();
setX(x);
setY(y);
}
public void up(int i)
{
y = getY() + i;
}
public void down(int i)
{
y = getY() - i;
}
public void left(int i)
{
x = getX() - i;
}
public void right(int i)
{
x = getX() + i;
}
public String toString()
{
return "(" + getX() + "," + getY() + ")";
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point that = (Point) obj;
if (y != that.y)
return false;
if (x != that.x)
return false;
return true;
}
public Point clone()
{
try
{
return (Point) super.clone();
}
catch(CloneNotSupportedException cnse)
{
System.out.println(cnse);
return null;
}
}
}
Error reported by javac is
error: exception CloneNotSupportedException is never thrown in body of corresponding try statement
} catch (CloneNotSupportedException cnse) { // This is the error
You can declare Point.clone() like this
public Point clone() throws CloneNotSupportedException
Then javac won't complain. Or even simpler do not try to catch exception.

java class- setX circle method

I am having problems with my homework assignment. I created a Circle class but i'm getting some errors while using the setX and setY methods. that i don't know how to fix. My code is below. Thank you.
import java.awt.Point;
public class Circle {
private Point origin;
private double radius;
public Circle(Point o, double r)
{
setOrigin(o);
setRadius(r);
}
public Circle(double xValue, double yValue, double r)
{
origin.setX(xValue);
origin.setY(yValue);
setRadius(r);
}
public Circle()
{
setX(0.0);
setY(0.0);
setRadius(1);
}
public Circle(Circle c)
{
setOrigin(c.getOrigin());
setRadius(c.getRadius());
}
Point getOrigin()
{
return new Point(origin);
}
public void setOrigin(Point p)
{
origin.setX(p.getX());
origin.setY(p.getY());
}
public void setX(double value)
{
origin.setX(value);
}
public double getX()
{
return origin.getX();
}
public void setY(double value)
{
origin.setY(value);
}
public double getY()
{
return origin.getY();
}
public void setRadius(double value)
{
radius = value;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return (radius * radius) * Math.PI;
}
public String toString()
{
return "(" + origin.getX() + ", " + origin.getY() + ", " + radius + ")";
}
public boolean equals(Circle c)
{
if (origin.getX() == c.getX() && origin.getY() == c.getY() &&
getRadius() == c.getRadius())
{
return true;
}
else
{
return false;
}
}
public boolean doesOverlap(Circle oC)
{
double distance = Math.sqrt((Math.pow(getX() - oC.getX(), 2) + Math.pow(getY()-oC.getY(), 2)));
if ((radius + oC.radius) > distance)
{
return true;
}
else
{
return false;
}
}
}
public class Point {
private double x;
private double y;
public Point(double xValue, double yValue)
{
x = xValue;
y = yValue;
}
public Point(Point p) {
this(p.x, p.y);
}
public Point() {
this(0, 0);
}
public void setX(double xValue)
{
x = xValue;
}
public double getX()
{
return x;
}
public void setY(double xValue)
{
y = xValue;
}
public double getY()
{
return y;
}
public boolean equals(Point otherPoint)
{
return (this.x == otherPoint.x) && (this.y == otherPoint.y);
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
Have a look at the API of Point. There are no such methods like setX(...)
Additionally, I think you should initialize it especially in your second and third constructor
public Circle(double xValue, double yValue, double r){...}
public Circle(){...}
via
origin = new Point();

Categories

Resources