Results 1 to 6 of 6

Thread: Bugs

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Bugs

    Here is the code. There are two types of bugs which are worrying me.
    1. Type mismatch: Can not convert from Point to Rectangle.
    2. variable can not be resolved or is not a field.
    3. The method movex() (movey() int he other bug) is undefined in the type rectangle.


    Java Code:
    package objectconcept;
    
    
    public class Point {
        public int x;
        public int y;
        Point(int x, int y){
            this.x=x;
            this.y=y;
        }
    }
    
    public class Rectangle{
        int width;
        int height;
        Rectangle rorigin;
        Rectangle (Point porigin, int w, int h){
            rorigin= porigin;                                 //*********************** BUG 1
            width= w;
            height=h;
        }
        Rectangle (int w, int h){
            width=w;
            height=h;
        }
        int getArea(int w, int h){
            return w*h;
        }
        int moveX(int new_x){
            rorigin.x= new_x;                             //*********************** BUG 2
    
            return rorigin.x;                                 //*********************** BUG 2
        }
        int moveY(int new_y){
            rorigin.y=new_y;                                    //*********************** BUG 2
            return rorigin.y;                                     //*********************** BUG 2
        }
    }
    
    
    public class ObjectDemo {    
        public static void main(String[] args) {
            Point porigin= new Point(23,94);
            
            Rectangle rect_one= new Rectangle(porigin, 100, 200);
            Rectangle rect_two= new Rectangle(50,100);
            
            System.out.println("Width of rect_one:"+ rect_one.width);
            System.out.println("Height of rect_one:"+ rect_one.height);
            System.out.println("Area of rect_one:"+ rect_one.getArea(rect_one.width, rect_one.height));
            
            rect_two.rorigin=porigin;                        //*********************** BUG 1
            
            System.out.println("X of rect_two:"+ rect_two.rorigin.x);                        //*********************** BUG 2
            System.out.println("Y of rect_two:"+ rect_one.rorigin.y);                      //*********************** BUG 2
            
            //Move X by 30 and Y by 72
            System.out.println("After moving, X of rect_two:"+ rect_two.movex(40));     //*********************** BUG 3
            System.out.println("After moving, Y of rect_two:"+ rect_two.movey(72));    //*********************** BUG 3
        }
    }
    Please tell me why am I getting these errors and what should I do to get rid of them. I'll be grateful.
    Thank you in advance.
    Don't forget to smile :-)

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Bugs

    Bug 1: You're attempting to assign a Point to a variable declared as a Rectangle. A Point is not a Rectangle.

    Bug 2: (edit) Your Rectangle class doesn't have fields called x and y.

    Bug 3: Capitalization matters.
    Last edited by kjkrum; 10-12-2013 at 10:33 AM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Bugs

    I have fixed the error :)
    3rd was a typo you can say. I had defined moveX() and called movex().
    The first and second ones were resolved when I made rorigin an instance of Point class instead of Rectangular class. From this I have learnt that an instance of a class can not be assigned to an instance of another class.
    Don't forget to smile :-)

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Bugs

    Quote Originally Posted by Nazneen Ali View Post
    From this I have learnt that an instance of a class can not be assigned to an instance of another class.
    An instance of a class can be assigned to a variable declared as one of its superclasses.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Bugs

    An instance of a class can be assigned to a variable declared as one of its superclasses.
    Does that mean that an instance of a class A can not be assigned to a reference variable belonging to another class B, unless B is a superclass of A ?

    Get in the habit of using standard Java naming conventions!
    OK. Thank you!
    Don't forget to smile :-)

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Bugs

    Quote Originally Posted by Nazneen Ali View Post
    Does that mean that an instance of a class A can not be assigned to a reference variable belonging to another class B, unless B is a superclass of A ?
    An instance of class A may be assigned to a reference type of any super class of A or any interface that A or its super class implements.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Anti-Aliasing, and other bugs
    By ResidentBiscuit in forum New To Java
    Replies: 11
    Last Post: 11-18-2011, 03:39 PM
  2. Bugs in calculator
    By kumarv75 in forum CLDC and MIDP
    Replies: 2
    Last Post: 06-28-2010, 01:12 PM
  3. who let the bugs out, who-who-who!
    By FON in forum Introductions
    Replies: 1
    Last Post: 12-11-2009, 03:05 PM
  4. Replies: 3
    Last Post: 08-19-2009, 03:54 AM
  5. My first Java project, encountering bugs
    By beatzz in forum New To Java
    Replies: 1
    Last Post: 03-17-2009, 08:32 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
  •