java program...help needed

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

  • Neeraj Sharma
    Neeraj Sharma
    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
    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
    THANK U...๐Ÿ˜€
    that really helped alot..๐Ÿ˜€
  • zion
    zion
    thank u..๐Ÿ˜€
  • suraz
    suraz
    why not join nataraz.blogspot.com a famous faculty in hyderabad at satya
  • Vikash Verma
    Vikash Verma
    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... ๐Ÿ˜Ž

You are reading an archived discussion.

Related Posts

We have many terms and words unique to our profession. Our poor brothers who are CAs, Arts, Journo-based etc might not fully understand, or worse, misunderstand. A handy-dandy guide! And...
Hey everyone, I'm new to this system, my name is Aleem, from Trinidad and Tobago, I'm a student majoring in Electrical and Computer Engineering .
Value First, the digital media company has acquired popular messaging website, Way2SMS for an undisclosed sum. The acquisition will make ValueFirst one of the biggest digital media companies in India....
Ever wondered how your phone's accelerometer works? A short video from 'The Engineer Guy' explains the accelerometer functioning inside your cell phone. Take a look -
The 2/3rd of world is water they say, and there can not be a bigger paradox than this commodity is still scarce. Growing population, pollution, indiscriminate use; whatever could be...