Reputation: 21
Class;
package com.eteration.model;
public class Circle {
private static double PI;
private int radius;
private double area;
public double calculateArea(){
return radius*radius*PI;
}
public static double getPI() {
return PI;
}
public static void setPI(double pI) {
PI = pI;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
Scrapbook;
Circle.PI=3.14;
System.out.println(Circle.getPI());
Circle circle1=new Circle(7);
System.out.println(circle1.getPI());
circle1.PI=3.1415;
Circle circle2=new Circle(3);
System.out.println(circle1.getPI());
System.out.println(circle2.getPI());
circle2.setPI(3.141592);
System.out.println(circle1.getPI());
System.out.println(circle2.getPI());
System.out.println(Circle.getPI());
I am trying exercise. In this exercise, I create a class, after I am coding example to scrapbook. But , I am seeing fault. These error; "Circle cannot be resaloved to a variable, Circle cannot be resolved, Circle cannot be resolved a type." Help me.
Upvotes: 0
Views: 624
Reputation: 5
You are trying to access a static private variab!e outside of a class through an object. If you want to change PI without your set method, you need to make PI either package-scoped(by writing nothing before it) or make it public. You cannot call static varibles and methods with the object. So the right way to access PI or getPI() is through the class eg.:
Circle.PI
Circle.getPI()
I recommend to not change the visibility of your variables and just access them through the set methods. I would also recommend to make PI final. By using Java API's Math.PI:
private static final double PI = Math.PI;
Or just use Math.PI.
I forgot, since you do not have a constructor defined you cannot give it a value like you did there:
Circle circle3 = new Circle(3); // the 3 cannot be put there.
I strongly advise to get yourself an IDE like [eclipse][1]
[1]: http://www.eclipse.org "eclipse", it marks wrong syntax, such as using static methods with objects or calling constructors that do not exist.
Beest regards.
Upvotes: 0
Reputation: 48258
you just can not do this Circle circle1=new Circle(7);
because you don't have defined a constructor for the class circle taking an integer....
I would modify the class improving it a little bit like this:
Make the constant PI public, and remove setters and getter, you will be able to access to it just like doing Circle.PI
class Circle {
public static double PI = 3.1415927;
private int radius;
private double area;
public double calculateArea() {
return radius * radius * PI;
}
public Circle(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
}
after that the scrapbook will look like:
Class;
package com.eteration.model;
public class Circle { private static double PI; private int radius; private double area;
public double calculateArea(){
return radius*radius*PI;
}
public static double getPI() {
return PI;
}
public static void setPI(double pI) {
PI = pI;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
} Scrapbook;
//Circle.PI=3.14; // dont need this
System.out.println(Circle.PI ); // instead of getPI()
Circle circle1 = new Circle(7); //the same
//System.out.println(circle1.getPI()); //dont need it since is a class constant
//circle1.PI=3.1415; // dont need it
Circle circle2=new Circle(3); //ok
System.out.println(circle1.getPI());//dont need it
System.out.println(circle2.getPI());//dont need it
circle2.setPI(3.141592);//dont need it
System.out.println(circle1.getPI());//dont need it
System.out.println(circle2.getPI());//dont need it
System.out.println(Circle.getPI());//dont need it
Upvotes: 1