# Thread: Finished the code but don't know how to run this...

1. Member
Join Date
Sep 2011
Posts
22
Rep Power
0

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

public Circle(double radius, int x, int y){
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...
Last edited by gomdohri; 09-08-2011 at 03:10 PM.

2. Member
Join Date
Sep 2011
Posts
22
Rep Power
0

## Re: Finished the code but don't know how to run this...

abstract Boolean overlap();

abstract Double perimeter();

YOU MAY IGNORE THIS

3. Member
Join Date
Sep 2011
Posts
22
Rep Power
0

## 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...

4. Member
Join Date
Sep 2011
Location
Athens Greece
Posts
29
Rep Power
0

## Re: Finished the code but don't know how to run this...

Hello there.

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!

5. ## 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.

Java 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

6. Member
Join Date
Sep 2011
Posts
22
Rep Power
0

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

}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•