Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Therron Telford
- AP CS A
- October 24, 2017
- */
- import java.util.Scanner;
- import java.lang.Math.*;
- public class Triangle
- {
- private int sideA, sideB, sideC;
- private double perimeter;
- private double theArea;
- public Triangle()
- {
- setSides(0,0,0);
- perimeter=0; // not really needed
- theArea=0; // not really needed
- }
- public Triangle(int a, int b, int c)
- {
- setSides(a,b,c);
- }
- public void setSides(int a, int b, int c)
- {
- // initialize the sides
- sideA = a;
- sideB = b;
- sideC = c;
- }
- public void calcPerimeter( )
- {
- perimeter = sideA + sideB + sideC;
- }
- public void calcArea( )
- {
- double s= perimeter / 2.0; // s is a LOCAL VARIABLE. We generally do not include a local variable
- // as one of the INSTANCE VARIABLES when it will only be used once.
- // In these cases it is declared and initialized in the method where it will be used.
- theArea = Math.sqrt(s* (s-sideA) * (s-sideB) * (s-sideC));
- }
- public void print( )
- {
- System.out.println("\n" +sideA + " "+ sideB + " "+ sideC);
- System.out.printf("Area == %.3f \n\n" ,theArea );
- }
- //create a print method or toString or both
- public String toString()
- {
- return "\n" +sideA + " "+ sideB + " "+ sideC+ "\n"+
- "Area == " + String.format("%.3f", theArea);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement