Advertisement
mmayoub

Triangle.java

Jan 20th, 2023
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | Software | 0 0
  1. package proj20012023;
  2.  
  3. public class Triangle {
  4.     // attributes
  5.     private double a, b, c;
  6.  
  7.     // ctor
  8.     public Triangle() {
  9.         this.a = 1;
  10.         this.b = 1;
  11.         this.c = 1;
  12.     }
  13.  
  14.     public Triangle(double a, double b, double c) {
  15.         this.a = a;
  16.         this.b = b;
  17.         this.c = c;
  18.     }
  19.  
  20.     public Triangle(double x) {
  21.         this.a = x;
  22.         this.b = x;
  23.         this.c = x;
  24.     }
  25.  
  26.     // getters
  27.     public double getA() {
  28.         return a;
  29.     }
  30.  
  31.     public double getB() {
  32.         return b;
  33.     }
  34.  
  35.     public double getC() {
  36.         return c;
  37.     }
  38.  
  39.     public double getPerimeter() {
  40.         return a + b + c;
  41.     }
  42.  
  43.     // setters
  44.     public void setA(double a) {
  45.         if (a > 0)
  46.             this.a = a;
  47.     }
  48.  
  49.     public void setB(double b) {
  50.         if (b > 0)
  51.             this.b = b;
  52.     }
  53.  
  54.     public void setC(double c) {
  55.         if (c > 0)
  56.             this.c = c;
  57.     }
  58.  
  59.     @Override
  60.     public String toString() {
  61.         return "Triangle [a=" + a + ", b=" + b + ", c=" + c + ", p=" + getPerimeter() + "]";
  62.     }
  63.  
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement