No enclosing instance of type Exercise9_12 is accessible. Must qualify the allocation
Quote:
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.
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;
}
}
}