Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class HeronsFormula {
- /**
- * Program: HeronsFormula.java
- * Purpose: Yahoo! Answers triangle area question
- * Creator: Chris Clarke
- * Created: 05.01.2014
- */
- public static void main(String[] args) {
- int n; // number of triangles read from file
- int a, b, c;
- String area;
- try {
- // create scanner to read from file
- Scanner fileScan = new Scanner(new File("Triangles.txt"));
- /*
- Sample file (need to save as "Triangles.txt"):
- 9
- 7 8 9
- 9 9 12
- 6 5 21
- 24 7 25
- 13 12 5
- 50 40 30
- 10 10 10
- 82 34 48
- 4 5 6
- */
- // read lead value indicating the number of triangles to be evaluated
- n = fileScan.nextInt();
- System.out.println("A\tB\tC\tAREA");
- for (int i=0; i<n; i++) {
- a = fileScan.nextInt();
- b = fileScan.nextInt();
- c = fileScan.nextInt();
- System.out.printf("%d\t%d\t%d\t", a, b, c);
- calculateArea(a, b, c);
- } // end for
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- return;
- } // end try/catch
- } // end main()
- public static void calculateArea(int a, int b, int c) {
- double s;
- double temp;
- double area = 0.0;
- s = (a + b + c) / 2.0;
- temp = s*(s-a)*(s-b)*(s-c);
- if (temp < 0) {
- System.out.println("This is not a triangle");
- return;
- } // end if
- area = Math.sqrt(temp);
- if (area==0) {
- System.out.println("This is not a triangle");
- return;
- } // end if
- System.out.printf("%2.4f%n", area);
- } // end calculateArea()
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement