reset password
Author Message
Anonymous
Posts: 166
Posted 19:31 Jan 25, 2014 |

can anyone tell me why I keep getting the area as 0. I tried to set the instance variables to zero and then on the tester when the user entered the values, the variables would be modified but that didn't work.

public class E4_8
{
  
    private int width;
    private int height;
   
    public E4_8(int width, int height)
    {
       width = width ;
       height = height;
    }
    
    public int getArea()
    {
        int area = height * width;
        return area;
       
    }
   
   
}
 

rabbott
Posts: 1649
Posted 21:11 Jan 25, 2014 |

Your constructor does not set the instance variables. It simply sets the constructor parameters to themselves. Use

this.width and this.height.

Last edited by rabbott at 21:11 Jan 25, 2014.