CrazyEngineers
  • java program...help needed

    zion

    Member

    Updated: Oct 26, 2024
    Views: 1.0K
    i m new to java. i need help my program has compiled but when displaying the output it shows 0 everytime if i change the value then also it shows 0.i think it is something related to garbage value. please help me to configure my problem and correct my code. i wrote a program to calculate rectangle area.
    class recta
    {
    int a,b,c;
    public recta()
    {
    int a=1;
    int b=2;
    int c=3;
    }
    public int calc()
    {
    int area;
    area=a*b*c;
    return area;
    }
    }
    class D
    {
    public static void main(String args[])
    {
    recta d=new recta();
    d.calc();
    System.out.println("arrea is"+d.calc());
    }
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Neeraj Sharma

    MemberMay 22, 2012

    Its because you wrote an extra function call with no variable catching the return value. To resolve this, just delete that line as you are calling the function in the println function. the following code will work as i deleted that line

    class recta
    {
    int a,b,c;
    public recta()
    {
    int a=1;
    int b=2;
    int c=3;
    }
    public int calc()
    {
    int area;
    area=a*b*c;
    return area;
    }
    }
    class D
    {
    public static void main(String args[])
    {
    recta d=new recta();
    System.out.println("arrea is"+d.calc());
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • nareshkumar6539

    MemberMay 23, 2012

    zion
    i m new to java. i need help my program has compiled but when displaying the output it shows 0 everytime if i change the value then also it shows 0.i think it is something related to garbage value. please help me to configure my problem and correct my code. i wrote a program to calculate rectangle area.
    class recta
    {
    int a,b,c;
    public recta()
    {
    int a=1;
    int b=2;
    int c=3;
    }
    public int calc()
    {
    int area;
    area=a*b*c;
    return area;
    }
    }
    class D
    {
    public static void main(String args[])
    {
    recta d=new recta();
    d.calc();
    System.out.println("arrea is"+d.calc());
    }
    }
    For above what you worte for that output will be 0 because
    you declare instance variables and local variables(defined inside default constructor) with same name
    when recta class object is created that default constructor will be executed values will be declared and intialized and these variables scope will be with in the block ,when you call d.calc() method it will take instance variables that varibles are not intialized so it will take default values(for int type default value is 0) thats why you are getting 0 as output.
    If you want to get 6 as output make the below change

    class recta
    {
    int a,b,c;
    public recta()
    {
    a=1;
    b=2;
    c=3;
    }
    public int calc()
    {
    int area;
    area=a*b*c;
    return area;
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 23, 2012

    THANK U...😀
    that really helped alot..😀
    Are you sure? This action cannot be undone.
    Cancel
  • zion

    MemberMay 23, 2012

    thank u..😀
    Are you sure? This action cannot be undone.
    Cancel
  • suraz

    MemberJun 19, 2012

    why not join nataraz.blogspot.com a famous faculty in hyderabad at satya
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 19, 2012

    declaring variables inside constructor has local scope inside constructor only and it doesn't initialize the values of data members,so default values will be placed in data members... 😎
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register