This is how you calculate the area of a triangle, hope you guys find this useful.

import java.text.DecimalFormat;
import java.util.Scanner;

public class Exercise2_15 {
    // find the area of a triangle
    public static void main (String [] args) {
        double side1 = 0;
        double side2 = 0;
        double side3 = 0;

        Scanner input = new Scanner(System.in);

        //obtain three points for a triangle
        System.out.print("Enter three points for a triangle: ");
        double side1x  = input.nextDouble();
        double side1y  = input.nextDouble();
        double side2x  = input.nextDouble();
        double side2y  = input.nextDouble();
        double side3x  = input.nextDouble();
        double side3y  = input.nextDouble();

        //find length of sides of triangle
        side1 = Math.sqrt(Math.pow((side2x - side1x), 2)+ Math.pow((side2y - side1y), 2));

        side2 = Math.sqrt(Math.pow((side3x - side2x), 2)+ Math.pow((side3y - side2y), 2));

        side3 = Math.sqrt(Math.pow((side1x - side3x), 2)+ Math.pow((side1y - side3y), 2));


        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3));

        DecimalFormat myFormatter = new DecimalFormat("#,##0.00");

        System.out.println("The area of the triangle is " + myFormatter.format(area));
    }
}

That's one way to do it.

A more experienced programmer would probably separate the user interface from the calculation.
The initialisations on line 7-9 are redundant because those values are never used.
There's a much simpler formula for the area of a triangle defined by three Points a,b,c:
0.5*((a.x - c.x) * (b.y - a.y) - (a.x-b.x) * (c.y-a.y))

That's one way to do it.
A more experienced programmer would probably separate the user interface from the calculation.
The initialisations on line 7-9 are redundant because those values are never used.
There's a much simpler formula for the area of a triangle defined by three Points a,b,c:
0.5*((a.x - c.x) * (b.y - a.y) - (a.x-b.x) * (c.y-a.y))

oh okay, i see what you meant there.i wll keep that in mind the next time i do something like this.

OK. Just FYI here's the simpler algorithm - I didn't encapsulate it in a method because it's only one line...

        System.out.print("Enter three points for a triangle: ");
        Scanner in = new Scanner(System.in);
        Point a = new Point(in.nextInt(),in.nextInt()); // eg 10 10
        Point b = new Point(in.nextInt(),in.nextInt()); // eg 10 110
        Point c = new Point(in.nextInt(),in.nextInt()); // eg 110 10
        int area = Math.abs(((a.x - c.x)*(b.y - a.y) - (a.x-b.x)*(c.y-a.y))/2);
        System.out.println(area);  // should be 100 * 100 /2 with above data
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.