Results 1 to 6 of 6
  1. #1
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default 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...
    Last edited by gomdohri; 09-08-2011 at 02:10 PM.

  2. #2
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

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

    abstract Boolean overlap();

    abstract Double perimeter();

    YOU MAY IGNORE THIS

  3. #3
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default 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. #4
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default 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. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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. #6
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

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

    }

Similar Threads

  1. help with assignment near finished
    By belfast09 in forum New To Java
    Replies: 11
    Last Post: 06-15-2011, 04:00 AM
  2. Need help almost finished problem thats due
    By tm02943 in forum New To Java
    Replies: 2
    Last Post: 03-22-2011, 08:10 AM
  3. Finished Product: What now?
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 02-11-2009, 10:41 AM
  4. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 10:33 AM
  5. How to run a code when a download is finished
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 10-14-2008, 12:37 PM

Posting Permissions

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