-1

I have written the following java code where I am getting 0 for area of A triangle. What can be the reason for this erroneous output?

package methodoverrideoverload;

import java.io.*;
import java.util.*;

class Figure_Area {
    int dima;
    int dimb;

    Figure_Area()
    {
    }

    Figure_Area(int i, int j)
    {
        dima=i;
        dimb=j;
    }

    void cal_area()
    {
        System.out.println("Area is undefined!!");
    }
}

class Figure_Area_Tri extends Figure_Area {

    Figure_Area_Tri(int i, int j)
    {
        super(i,j);
    }

    void cal_area()
    {
        System.out.println("The area of the Triangle is "+((1/2)*dima*dimb));
    }
}

class Figure_Area_Rec extends Figure_Area {

    Figure_Area_Rec(int i, int j)
    {
        super(i,j);
    }

    void cal_area()
    {
        System.out.println("The area of the Rectangle is "+ dima*dimb);
    }
}

class Figure_Area_Cir extends Figure_Area {
    double pi=3.14;

    Figure_Area_Cir(int i)
    {
        super.dima=i;
    }

    void cal_area()
    {
        System.out.println("The area of the Circle is "+ (pi*dima*dima));
    }
}

class findArea {

    public static void main(String args[]) throws IOException
    {
        System.out.println("<<-MENU-->>");
        System.out.println("Enter the figure you want to know the area of->");
        System.out.println("Press 1 for Triangle");
        System.out.println("Press 2 for Rectangle");
        System.out.println("Press 3 for Circle");
        System.out.println("Press 4 to Exit");
        System.out.println("Enter your choice->");

        Scanner sc=new Scanner(System.in);
        int ch=sc.nextInt();
        switch(ch)
        {
            case 1:
                System.out.println("Enter Height->");
                int h=sc.nextInt();
                System.out.println("Enter Base->");
                int b=sc.nextInt();
                Figure_Area_Tri tri=new Figure_Area_Tri(h,b);
                tri.cal_area();
                break;

            case 2:
                System.out.println("Enter Length->");
                int len=sc.nextInt();
                System.out.println("Enter Breadth->");
                int bre=sc.nextInt();           
                Figure_Area_Rec rec=new Figure_Area_Rec(len,bre);
                rec.cal_area();
                break;

            case 3:
                System.out.println("Enter Radius->");
                int rad=sc.nextInt();
                Figure_Area_Cir cir=new Figure_Area_Cir(rad);
                cir.cal_area();
                break;

            case 4:
                break;

            default:
                System.out.println("Sorry! Invalid Choice");
        }
    }
} 

I have examined the code in debugging mode and found out the values of dima and dimb are set as the input value, but somehow the function for area is most likely unable to give the correct result.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Mistu4u
  • 5,132
  • 15
  • 53
  • 91
  • possible duplicate of [Why does this integer equation output 0](http://stackoverflow.com/questions/20633314/why-does-this-integer-equation-output-0) – Richard Tingle Dec 17 '13 at 12:25

3 Answers3

4

With integers variables java uses integer maths, for example

1/2==0

To solve this use double maths

1.0/2==0.5

1d would also work instead of 1.0

So in your case

(1.0/2)*dima*dimb

As long as one of the variables is a double then double maths applies (be careful when the equation contains multiple "parts", each part must have a double in it).


What won't help and why

Note just casting to double ((double)(1/2)*dima*dimb) won't solve the problem because the detail is already lost by the point of the cast to double, it goes as

(double)(1/2)*dima*dimb
(double)(0)*(dima*dimb)
(double)(0)
0.0
Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0

You are using 1/2 which equals 0

You need to use a double, so 1.0 / 2.0 = 0.5

James Reeves
  • 97
  • 1
  • 13
0

Try using double or float for 1/2.

If you use integers it will be 0 as there is no such thing as 0.5 with integers.

You can write 1/2f for example or 1/2d or look at other answers :)

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85