5 Replies - 302 Views - Last Post: 30 December 2012 - 06:05 AM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Invoking objects in different classes.

Posted 30 December 2012 - 03:19 AM

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());
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Invoking objects in different classes.

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,591
  • Joined: 29-July 11

Re: Invoking objects in different classes.

Posted 30 December 2012 - 03:37 AM

Consider what is happening. Your main() is static, meaning that it does not rely upon an instance of a class.

System.out.println("Area of circle:"+r1.getArea());



is trying to reference an object which is non-static. Because it is non-static, you have to first create an instance of Rectangle in main(), like you did at the top of the TestRectangle class.

Rectangle r1 = new Rectangle(); 



This and this you just can't do. :)/>
    r1.getRadius(){  // error: package r1 doesn't exist 
        len = 10;
    };
    r1.getWidth(){
        w = 5;
    };



I think you mean to do

r1.setRadius(10);
r1.setWidth(5);



instead.

This post has been edited by farrell2k: 30 December 2012 - 03:38 AM

Was This Post Helpful? 2
  • +
  • -

#3 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: Invoking objects in different classes.

Posted 30 December 2012 - 05:31 AM

Hi, many thanks for your comments. I have edited the code with necessary changes but the code still displays Area of circle:0 Perimeter of circle:0 as the printed output :whatsthat:/>

Edited TestRectangle class
public class TestRectangle {
    public static void main(String[] args){
    Rectangle r1 = new Rectangle();
    
    r1.setLength(10);
    r1.setWidth(5);
    
    int area1 = r1.getArea();
    int perimeter1 = r1.getPerimeter();
    
    
    
    System.out.println("Area of circle:"+area1);
    
    System.out.println("Perimeter of circle:"+perimeter1);

}
}

This post has been edited by Gisengryl: 30 December 2012 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,591
  • Joined: 29-July 11

Re: Invoking objects in different classes.

Posted 30 December 2012 - 05:51 AM

It's a simple mistake. Look at your Rectangle class setter methods.

public void setLength(int len){
    len = length;
}



it should be

length = len;



Same goes for the other setters. By default, length is 0. When you call setLength(5) you are saying:

len = length;

or

5 = 0;

Switch that around. :) Understand?
Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • View blog
  • Posts: 3,121
  • Joined: 05-April 11

Re: Invoking objects in different classes.

Posted 30 December 2012 - 05:55 AM

You assign the value to the variable on the left of the = sign
If you call setLength(5) the following would happen
public void setLength(int len) { 
    //len has the value 5
    //length could have any value at this point

    len = length; //len is set to the value of length
    //the variable length is unchanged, only len has changed
}


Was This Post Helpful? 2
  • +
  • -

#6 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 08-September 12

Re: Invoking objects in different classes.

Posted 30 December 2012 - 06:05 AM

Makes perfect sense after understanding both of your explanations.

Thanks.

Heh, this is really an elementary java mistake :wheelchair:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1