Results 1 to 4 of 4
- 11-30-2010, 11:19 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 15
- Rep Power
- 0
No enclosing instance of type Exercise9_12 is accessible. Must qualify the allocation
This is the error I'm getting, and I don't quite understand what it means? It comes up on line 53 and 29.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).
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; } } }
- 11-30-2010, 11:33 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 11-30-2010, 11:53 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 15
- Rep Power
- 0
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.
- 12-01-2010, 06:12 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
JNI Memory Allocation
By Smokin' Caterpillar in forum Advanced JavaReplies: 3Last Post: 09-16-2010, 05:00 PM -
Global variable not accessible
By bolduc4 in forum New To JavaReplies: 3Last Post: 05-10-2010, 07:51 AM -
Memory Allocation
By zzpprk in forum Advanced JavaReplies: 2Last Post: 03-16-2010, 01:14 AM -
declare a private instance variable called ran of type random
By tinman in forum New To JavaReplies: 1Last Post: 12-09-2009, 12:57 AM -
[error] No enclosing instance of type RobotTest is accessible
By jon80 in forum New To JavaReplies: 1Last Post: 06-08-2009, 07:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks