Results 1 to 4 of 4
  1. #1
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default No enclosing instance of type Exercise9_12 is accessible. Must qualify the allocation

    No enclosing instance of type Exercise9_12 is accessible. Must qualify the allocation with an enclosing
    instance of type Exercise9_12 (e.g. x.new A() where x is an instance of Exercise9_12).
    This is the error I'm getting, and I don't quite understand what it means? It comes up on line 53 and 29.
    If I take out line 53 then the error dissapears on line 29. I can make it work with one rectangle object but creating a second one causes problems? Any ideas on what this error means? Thanks.

    I red bolded the two lines in question.
    Java Code:
    //Imports Scanner
    import java.util.Scanner;
    
    class Exercise9_12 
    {
         public static void main(String[] args)
         {
            //Start scanner
             Scanner input = new Scanner(System.in);
            
            //Read in user input
             String s1 = input.nextLine();
             String[] userInputString = s1.split(" ", 0);
             double userInput[] = {0, 0, 0, 0};
             
            //Puts string array into array of doubles
             for (int i = 0;i < userInputString.length;i++)
             {
                 userInput[i]= Integer.parseInt(userInputString[i]);
             }
             
            //Exits program if first user input is -1
             if (userInput[0] == -1)
             {
                 System.out.println(userInput[0]);
                 System.exit(0);
             }
             [B][COLOR="Red"]
             Rectangle r1 = new Rectangle(userInput[0], userInput[1], userInput[2], userInput[3]);[/COLOR][/B][COLOR="Red"][/COLOR]
             
             System.out.println("Enter rectangle:");
             System.out.println("perimeter: " + r1.getPerimeter() );
             System.out.println("area: " + r1.getArea() );
             System.out.println("Enter rectangle: ");
             
             //Read in user input
             String s2 = input.nextLine();
             userInputString = s2.split(" ", 0);
             
            //Puts string array into array of doubles
             for (int i = 0;i < userInputString.length;i++)
             {
                 userInput[i]= Integer.parseInt(userInputString[i]);
             }
             
            //Exits program if first user input is -1
             if (userInput[0] == -1)
             {
                 System.out.println(userInput[0]);
                 System.exit(0);
             }
             [B][COLOR="Red"]
             Rectangle r2 = new Rectangle(userInput[0], userInput[1], userInput[2], userInput[3]);[/COLOR][/B]
             System.out.println("contains: " + r2.contains(r1, r2) );
             System.out.println("overlaps: " + r2.overlaps(r1, r2) );
         }
         
         
         
         public class Rectangle
         {
             //Should these be private?
               double x = 1;
               double y = 1;
               double width = 1;
               double height = 1;
              
            public Rectangle()
            {
            }
            public Rectangle(double  newx, double newy, double newWidth, double newHeight)
            {
                x = newx;
                y = newy;
                this.setWidth(newWidth);
                this.setHeight(newHeight);
            }
            public double getWidth()
            {
                return width;
            }
            public double getHeight()
            {
                return height;
            }
            public void setWidth(double newWidth)
            {
                width = newWidth;
            }
            public void setHeight(double newHeight)
            {
                height = newHeight;
            }
            public double getPerimeter()
            {
                return (width * 2) + (height * 2);
            }
            public double getArea()
            {
                return width * height;
            }
            Boolean contains(Rectangle r1, Rectangle r2)
            {
                Boolean contains = false;
                if ((r1.x + - r2.x) < (r1.width + r2.width)/2 
                        && (r1.y + - r2.y) < (r1.height + r2.height)/2
                        || (r2.x + - r1.x) < (r2.width + r1.width)/2 
                        && (r2.y + - r1.y) < (r2.height + r1.height)/2);
                {
                    contains = true;
                }
                return contains;    
            }
            Boolean overlaps(Rectangle r1, Rectangle r2)
            {
                Boolean overlaps = false;
                if ((r1.x + .5 * r1.width > r2.x + .5 * r2.width)
                        && (r1.y + .5 * r1.height > r2.y + .5 * r2.height)
                        || (r2.x + .5 * r2.width > r1.x + .5 * r1.width)
                        && (r2.y + .5 * r2.height > r1.y + .5 * r1.height) )
                {
                    overlaps = true;
                }
                return overlaps;    
            }
         }
     }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I think the post in the other thread was correct. Take your Rectangle code out and put it another file: Rectangle.java

    If the assignment came with specific instructions about class naming and files, follow them (and post them if you can't understand them) I mention this because there was an inner class problem the other which also had an ExcerciseN_NN class involved. [Edit] I've just realised that this is Rectangle.java! In that case take the other class out and put it in its own file. Basically two classes, and two .java files.

    -------------------------------

    //Should these be private?

    Yes. This should be your default choice. Make everything private unless and until you have a need to declare them as anything. [Edit] I've just realised that this is Rectangle.java! In that case take the other class out and put it in its own file. Basically two classes, and two .java files.

    (Also change Boolean to boolean.)

  3. #3
    Xycose is offline Member
    Join Date
    Jun 2010
    Posts
    15
    Rep Power
    0

    Default

    It has to be all in one file, which I think is what confused me a previous time before.

    We submit our work online to our teachers website by copying and pasting our entire code in and his website complies and runs it with sample input and checks to see if the output matches character by character.

    BUT, I think you helped me figure it out by mentioning this.

    I closed off the Exercise9_12 class and than started the rectangle class but took public off and it at least complies OK now. Time to test everything out! Thank you for the help.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. JNI Memory Allocation
    By Smokin' Caterpillar in forum Advanced Java
    Replies: 3
    Last Post: 09-16-2010, 06:00 PM
  2. Global variable not accessible
    By bolduc4 in forum New To Java
    Replies: 3
    Last Post: 05-10-2010, 08:51 AM
  3. Memory Allocation
    By zzpprk in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2010, 02:14 AM
  4. Replies: 1
    Last Post: 12-09-2009, 01:57 AM
  5. Replies: 1
    Last Post: 06-08-2009, 08:27 AM

Posting Permissions

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