Java class and object 7, java object 7

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.