Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Invoking objects in different classes.

$
0
0
I have this particular question regarding invocation of methods in different classes. Below is my code for the 2 required classes and there's some errors which I can't solve =/
Posted Image

public class Rectangle {
    int length;
    int width;
    
    public void setLength(int len){
        len = length;
    }
    public void setWidth(int w) {
        w = width;
    }
    public int getArea() {
        int area = length*width;
        return area;
    }
    public int getPerimeter() {
        int perimeter = (length*2)+(width*2);
        return perimeter;
    }
}




public class TestRectangle {
    Rectangle r1 = new Rectangle();   // insertion of static for the first line ?
       
    r1.getRadius(){  // error: package r1 doesn't exist 
        len = 10;
    };
    r1.getWidth(){
        w = 5;
    };
     public static void main(String[] args){
     System.out.println("Area of circle:"+r1.getArea());  // non-static variable r1 cannot be referenced from a static context . I inserted static for the 1st line and it solves this error, but is this the correct way ?
    
    System.out.println("Perimeter of circle:"+r1.getPerimeter());
}
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>