Below is the code I've written thus far. I'm struggling on the System.out.println where I'm attempting to output "The area of the triangle is (area)." I'm very new to Java coming from C++. I thought it best to post my work thus far here and see if anyone could offer a potential solution, as I'm so close but continue to receive the following error:
The method format(String, double) is undefined for the type AreaOfATriangle
import java.util.Scanner;
public class AreaOfATriangle {
public static double getDistance(double x1, double y1, double x2, double y2) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
public static void main(String[] args) {
double x1, y1, x2, y2, x3, y3;
Scanner sc = new Scanner(System.in);
System.out.print("Enter three points for a triangle: ");
x1 = sc.nextDouble();
y1 = sc.nextDouble();
x2 = sc.nextDouble();
y2 = sc.nextDouble();
x3 = sc.nextDouble();
y3 = sc.nextDouble();
double side1 = getDistance(x1, y1, x2, y2);
double side2 = getDistance(x2, y2, x3, y3);
double side3 = getDistance(x3, y3, x1, y1);
double s = (side1 + side2 + side3) / 2.0;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
System.out.println("The area of the triangle is " + format("%.2f",area) + ".");
}
}
Any help would be greatly appreciated!