java program...help needed

zion

zion

@zion-6KgmEO Oct 26, 2024
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());
}
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG May 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());
    }
    }
  • nareshkumar6539

    nareshkumar6539

    @nareshkumar6539-BKuVbx May 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;
    }
    }
  • zion

    zion

    @zion-6KgmEO May 23, 2012

    THANK U...😀
    that really helped alot..😀
  • zion

    zion

    @zion-6KgmEO May 23, 2012

    thank u..😀
  • suraz

    suraz

    @suraz-mouLU5 Jun 19, 2012

    why not join nataraz.blogspot.com a famous faculty in hyderabad at satya
  • Vikash Verma

    Vikash Verma

    @vikash-verma-jF0MOy Jun 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... 😎