Clones method is not working - java

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.

Related

I need constructor that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)

So i code consist of 3 parts 2 classes and test.
This is code for test
#Test
public void testRectangle1() {
Point center = new Point(20, 30);
Rectangle rect = new Rectangle(center, 20, 20);
assertAll(
() -> assertEquals(10, rect.getTopLeft().getX()),
() -> assertEquals(20, rect.getTopLeft().getY()),
() -> assertEquals(30, rect.getBottomRight().getX()),
() -> assertEquals(40, rect.getBottomRight().getY()),
() -> assertEquals(20, rect.getWidth()),
() -> assertEquals(20, rect.getHeight())
);
}
Class Point works fine, and i am ading it just for clarity
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
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 void moveTo(int newX, int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx, int dy) {
x += dx;
y += dy;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
This is class for Rectangle itself, and it includes both constructor and aditional methods.
public class Rectangle {
public int width = 0;
public int height = 0;
public Point center;
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
width=x;
height=y;
}
public Point getTopLeft() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(- width / 2, height / 2);
return point;
}
public Point getBottomRight() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(width / 2, - height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Main problem is that this code return only zeroes in tests, i guess problem is in rectangle class in constructor, or aditionalmethods.
Your constructor for Rectangle is always setting width and height to 0. I think the constructor should look something like...
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
this.width=width;
this.height=height;
this.center=center;
}

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.

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();

Point isAbove, isUnder, isRight, isLeft methods

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

Categories

Resources