java - Why is my smaller method giving me an error -
this first class called class circle:
public class circle { //circle class begins //declaring variables public double circle1; public double circle2; public double circle3; public double xvalue; public double yvalue; public double radius; private double area; //constructor public circle(int x,int y,int r) {//constructor begins xvalue = x; yvalue = y; radius = r; }//constructor ends //method gets area of circle public double getarea () {//method getarea begins area = (3.14*(this.radius * this.radius)); return area; }//getarea ends public static smaller (circle other) { if (this.area > other.area) { return other; else { return this; } //i'm not sure return here. gives me error( want return circle) } }//class ends } this tester class:
public class tester {//tester begins public static void main(string args []) { circle circle1 = new circle(4,9,4); circle circle2 = new circle(4,7,6); c3 = c1.area(c2); system.out.println(circle1.getarea()); //system.out.println( } }//class tester ends
the smaller method should have return type. this keyword cannot used in static method. i.e. method not have access instance of circle. make sense given method name smaller implies - compares current instance of circle passed in.
public circle smaller(circle other) { if (this.area > other.area) { return other; } else { return this; } } to use:
circle smallercircle = circle1.smaller(circle2); aside: java naming conventions show class names start uppercase letter give circle.
Comments
Post a Comment