S
sovichet.ly
I am working directly out of a beginning programmers book and trying
to type out this example. I have two questions.
1) For the Rectangle class eclipse gives me the error of "The field
color cannot be declared static; static fields can only be declared in
static or top level types" It gives me the same errors for the
methods getColor and setColor. I thought that I declared this in the
top level already for the Rectangle class. Once again, this is
directly out of the book(no errata, so I'm confident it's me).
2) I am attempting to create an instance of Rectangle. It gives me
the error that no enclosing instance type C5E1 is accessable. Must
qualify the allocation with an enclosing instance of type C5E1.
Any help would be appreciated.
-----------------------------------------------------------------------------------------------------------------------------------
public class C5E1 {
public static void main(String[] args)
{
//create rectangle
Rectangle r1 = new Rectangle();
}//end main
public class Rectangle
{
private double width = 1;
private double height = 1;
private static String color = "white";
public Rectangle()
{// default constructor
}//end Rectangle
public Rectangle(double width, double height, String color)
{
}//end Rectangle
public double getWidth()
{
return width;
}//end getWidth
public void setWidth(double width)
{
this.width = width;
}//end setWidth
public double getHeight()
{
return height;
}//end getHeight
public void setHeight(double height)
{
this.height = height;
}//end setHeight
public static String getColor()
{
return color;
}//end getColor
public static void setColor(String color)
{
this.color = color;
}//end setColor
public double findArea()
{
double area = this.height * this.width;
return area;
}//end findArea
}//end class rectangle
}
-----------------------------------------------------------------------------------------------------------------------------------
to type out this example. I have two questions.
1) For the Rectangle class eclipse gives me the error of "The field
color cannot be declared static; static fields can only be declared in
static or top level types" It gives me the same errors for the
methods getColor and setColor. I thought that I declared this in the
top level already for the Rectangle class. Once again, this is
directly out of the book(no errata, so I'm confident it's me).
2) I am attempting to create an instance of Rectangle. It gives me
the error that no enclosing instance type C5E1 is accessable. Must
qualify the allocation with an enclosing instance of type C5E1.
Any help would be appreciated.
-----------------------------------------------------------------------------------------------------------------------------------
public class C5E1 {
public static void main(String[] args)
{
//create rectangle
Rectangle r1 = new Rectangle();
}//end main
public class Rectangle
{
private double width = 1;
private double height = 1;
private static String color = "white";
public Rectangle()
{// default constructor
}//end Rectangle
public Rectangle(double width, double height, String color)
{
}//end Rectangle
public double getWidth()
{
return width;
}//end getWidth
public void setWidth(double width)
{
this.width = width;
}//end setWidth
public double getHeight()
{
return height;
}//end getHeight
public void setHeight(double height)
{
this.height = height;
}//end setHeight
public static String getColor()
{
return color;
}//end getColor
public static void setColor(String color)
{
this.color = color;
}//end setColor
public double findArea()
{
double area = this.height * this.width;
return area;
}//end findArea
}//end class rectangle
}
-----------------------------------------------------------------------------------------------------------------------------------