Advertisement
therrontelford

Triangle

Oct 24th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. /*Therron Telford
  2. AP CS A
  3. October 24, 2017
  4. */
  5.  
  6. import java.util.Scanner;
  7. import java.lang.Math.*;
  8.  
  9. public class Triangle
  10. {
  11.     private int sideA, sideB, sideC;
  12.     private double perimeter;
  13.     private double theArea;
  14.  
  15.     public Triangle()
  16.     {
  17.        setSides(0,0,0);
  18.        perimeter=0;  // not really needed
  19.        theArea=0;  // not really needed
  20.     }
  21.  
  22.     public Triangle(int a, int b, int c)
  23.     {
  24.         setSides(a,b,c);
  25.     }
  26.  
  27.  
  28.     public void setSides(int a, int b, int c)
  29.     {
  30.         // initialize the sides
  31.         sideA = a;
  32.         sideB = b;
  33.         sideC = c;
  34.     }
  35.  
  36.     public void calcPerimeter( )
  37.     {
  38.         perimeter = sideA + sideB + sideC;
  39.     }
  40.  
  41.     public void calcArea( )
  42.     {
  43.         double s= perimeter / 2.0;  // s is a LOCAL VARIABLE.  We generally do not include a local variable
  44.                // as one of the INSTANCE VARIABLES when it will only be used once.
  45.                // In these cases it is declared and initialized in the method where it will be used.
  46.         theArea = Math.sqrt(s* (s-sideA) * (s-sideB) * (s-sideC));
  47.  
  48.     }
  49.  
  50.     public void print( )
  51.     {
  52.         System.out.println("\n" +sideA + " "+ sideB + " "+ sideC);
  53.        
  54.         System.out.printf("Area ==  %.3f  \n\n" ,theArea );
  55.     }
  56.    
  57.     //create a print method or toString or both
  58.    
  59.     public String toString()
  60.     {
  61.         return "\n" +sideA + " "+ sideB + " "+ sideC+ "\n"+
  62.                 "Area ==  " + String.format("%.3f", theArea);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement