-
-
Save mariiaKolokolova/f244861069b6c4c29ac3f1f6c998ec5e to your computer and use it in GitHub Desktop.
OOP_HomeWork1b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maricka.kolokolova; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Triangle triangleOne = new Triangle(6.0, 8.0, 10.0); | |
System.out.println(triangleOne.areaTriangle()); | |
Triangle triangleTwo = new Triangle(); | |
triangleTwo.setA(3.1); | |
triangleTwo.setB(8.0); | |
triangleTwo.setC(9.5); | |
System.out.println(triangleTwo); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maricka.kolokolova; | |
public class Triangle { | |
private double a; | |
private double b; | |
private double c; | |
private double area; | |
public Triangle() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public Triangle(double a, double b, double c) { | |
super(); | |
this.a = a; | |
this.b = b; | |
this.c = c; | |
} | |
public double getA() { | |
return a; | |
} | |
public void setA(double a) { | |
this.a = a; | |
} | |
public double getB() { | |
return b; | |
} | |
public void setB(double b) { | |
this.b = b; | |
} | |
public double getC() { | |
return c; | |
} | |
public void setC(double c) { | |
this.c = c; | |
} | |
double areaTriangle() { | |
if ((a < b + c) && (b < a + c) && (c < a + b)) { | |
double p = (a + b + c) / 2; | |
return Math.sqrt(p * (p - a) * (p - b) * (p - c)); | |
} else { | |
return 0; | |
} | |
} | |
@Override | |
public String toString() { | |
if ((a < b + c) && (b < a + c) && (c < a + b)) { | |
area = areaTriangle(); | |
return "Triangle [a=" + a + ", b=" + b + ", c=" + c + ", area=" + area + "]"; | |
} else { | |
return "Triangle does not exist!"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment