Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Triangle {
- private int side1;
- private int side2;
- private int side3;
- private String type;
- public Triangle(int side1, int side2, int side3) throws TriangleException {
- if(isTriangle(side1, side2, side3)) {
- this.side1 = side1;
- this.side2 = side2;
- this.side3 = side3;
- this.classify();
- }
- else {
- throw new TriangleException("No es un triángulo");
- }
- }
- private boolean isTriangle(int side1, int side2, int side3) {
- if(side1 + side2 > side3) {
- if(side1 + side3 > side2) {
- if(side2 + side3 > side1) {
- return true;
- }
- }
- }
- return false;
- }
- private void classify() {
- if(side1 == side2 && side1 == side3) {
- type = "Equilátero";
- }else if(side1 != side2 && side1 != side3) {
- type = "Escaleno";
- }else {
- type = "Isósceles";
- }
- }
- public String getType() {
- return this.type;
- }
- @Override
- public String toString() {
- return "Triangulo de lados a=" + side1 + ", b=" + side2 + ", c=" + side3 + ", tipo=" + type;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement