Finished the code but don't know how to run this...
Question: Add the ability of store the xy-position of each shape. Also add a method that is able to determine whether two shapes overlap with each other.
Basically, there is Shape abstract class which has Circle, Square and Triangle classes. I think my code is okay but I dont know how to run this...
it must return true as a result (on the screen)
CODE------------------------------------------------------------
package lab213;
abstract public class Shape {
public static void main(String[]args){
Shape s1 = new Square(10.0, 8, 3);
Shape s2 = new Square(8.0, 9, 2);
Shape c1 = new Circle(1.0, 10, 5);
Shape c2 = new Circle(1.0, 14, 3);
System.out.println(overlapsqsq(s1,s2));
}
public Boolean overlapsqsq(Square s1, Square s2){
if((s1.x < s2.x+s2.side) && (s2.x < s1.x + s1.side) && (s2.y < s1.y + s1.side) && (s1.y < s2.y + s2.side) ){
return true;
}else
return false;
}
public Boolean overlapclcl(Circle c1, Circle c2){
if((c1.x - c2.x)*(c1.x - c2.x) + (c1.y - c2.y)*(c1.y - c2.y) < (c1.radius + c2.radius) * (c1.radius + c2.radius))
return true;
else
return false;
}
public Boolean overlapsqcl(Circle c1, Square s1){
if((distance(c1.x, s1.x, s1.side)*distance(c1.x,s1.x,s1.side) + distance(c1.y, s1.y, s1.side)*distance(c1.y,s1.y,s1.side) < c1.radius*c1.radius)){
return true;
}else
return false;
}
double distance(double a, double b, double c){
if(a > b + c){
return a-b-c;
}else if (a < b){
return b - a;
}else
return 0;
}
}
package lab213;
public class Circle extends Shape {
int x;
int y;
double radius;
public Circle(double radius, int x, int y){
this.radius = radius;
this.x = x;
this.y = y;
}
}
package lab213;
public class Square extends Shape{
int x;
int y;
double side;
public Square(Double side, int x, int y){
this.side = side;
this.x = x;
this.y = y;
}
}
END
System.out.println(overlapsqsq(s1,s2));
THIS IS THE PROBLEM T.T.... I tried to do like this but failed hehe...
Re: Finished the code but don't know how to run this...
abstract Boolean overlap();
abstract Double perimeter();
YOU MAY IGNORE THIS
Re: Finished the code but don't know how to run this...
System.out.println(overlapsqsq(s1,s2));
THIS IS THE PROBLEM T.T.... I tried to do like this but failed hehe...
Re: Finished the code but don't know how to run this...
Hello there.
Quote:
System.out.println(overlapsqsq(s1,s2));
THIS IS THE PROBLEM T.T.... I tried to do like this but failed hehe...
There are two issues in this call.
First the overlapsqsq() takes two square object as parametres(in the first implementation) but you are actually passing it two Shape objects.
One solution to this problem could be to explicitly cast the s1 and s2 to Shape.
The second problem is that they way you perform your call to overlapsqsq() makes the compiler "think" that it is a class method which isn't
instead it is a instance method which means you need an object to call that method.
Try and make the overlapsqsq methods static.
Try these and let me know what happened! :)-:
Re: Finished the code but don't know how to run this...
Your abstract superclass should have a single abstract method. From there, the sublclasses should implement the overlap method in there own way.
Code:
public abstract class Shape{
abstract boolean overlap(Shape s);
}
public class Circle extends Shape{
//instance variables
@Override public boolean overlap(Shape s){
method for circle
}
}
It will look similar for all the classes
Re: Finished the code but don't know how to run this...
1st advice : SOLVED
package lab213;
abstract public class Shape {
abstract Double perimeter();
public static void main(String[]args){
Square s1 = new Square(10.0, 8, 3);
Square s2 = new Square(8.0, 9, 2);
Circle c1 = new Circle(1.0, 10, 5);
Circle c2 = new Circle(1.0, 14, 3);
System.out.println(overlapsqsq(s1,s2));
System.out.println(overlapclcl(c1,c2));
System.out.println(overlapsqcl(c1,s1));
}
public static Boolean overlapsqsq(Square s1, Square s2){
if((s1.x < s2.x+s2.side) && (s2.x < s1.x + s1.side) && (s2.y < s1.y + s1.side) && (s1.y < s2.y + s2.side) ){
return true;
}else
return false;
}
public static Boolean overlapclcl(Circle c1, Circle c2){
if((c1.x - c2.x)*(c1.x - c2.x) + (c1.y - c2.y)*(c1.y - c2.y) < (c1.radius + c2.radius) * (c1.radius + c2.radius))
return true;
else
return false;
}
public static Boolean overlapsqcl(Circle c1, Square s1){
if((distance(c1.x, s1.x, s1.side)*distance(c1.x,s1.x,s1.side) + distance(c1.y, s1.y, s1.side)*distance(c1.y,s1.y,s1.side) < c1.radius*c1.radius)){
return true;
}else
return false;
}
static double distance(double a, double b, double c){
if(a > b + c){
return a-b-c;
}else if (a < b){
return b - a;
}else
return 0;
}
}