Java class and object 7, java object 7
Create A triangle class with three sides of the member variable. Evaluate the perimeter by using the method. Create class main Class A to test it.
1 public class sanjiaoxing {2 3 private double a; 4 private double B; 5 private double c; 6 double zhouchang; 7 8 public double getA () {9 return; 10} 11 12 public void setA (double a) {13 this. a = a; 14} 15 16 public double getB () {17 return B; 18} 19 20 public void setB (double B) {21 this. B = B; 22} 23 24 public double getC () {25 return c; 26} 27 28 public void setC (double c) {29 this. c = c; 30} 31 3 2 sanjiaoxing (double a, double B, double c) {33 this. a = a; 34 this. B = B; 35 this. c = c; 36} 37 38 public void getZhouChang () {39 zhouchang = 0; 40 if (a + B> c & a + c> B & B + c>) & (a> = 0 & B> = 0 & c> = 0) {41 zhouchang = a + B + c; 42} else {43 System. out. println (this. a + "\ t" + this. B + "\ t" + this. c + "three numbers cannot constitute triangles. "); 44} 45 if (zhouchang! = 0) {46 System. out. println (this. a + "\ t" + this. B + "\ t" + this. c + "three triangles are successfully formed, perimeter =" + zhouchang); 47} 48} 49 50 public static void main (String [] args) {51 sanjiaoxing s1 = new sanjiaoxing (1.4, 2.5, 3); 52 s1.getZhouChang (); 53 54 sanjiaoxing s2 = new sanjiaoxing (1, 3, 4); 55 s2.getZhouChang (); 56 57 sanjiaoxing s3 = new sanjiaoxing (1, 5, 5); 58 s3.getZhouChang (); 59 60 sanjiaoxing s4 = new sanjiaoxing (3,-4, 5 ); 61 s4.getZhouChang (); 62}
Run: