用java求三角形的周长和面积

上传人:hs****ma 文档编号:512230437 上传时间:2023-01-02 格式:DOCX 页数:10 大小:13.16KB
返回 下载 相关 举报
用java求三角形的周长和面积_第1页
第1页 / 共10页
用java求三角形的周长和面积_第2页
第2页 / 共10页
用java求三角形的周长和面积_第3页
第3页 / 共10页
用java求三角形的周长和面积_第4页
第4页 / 共10页
用java求三角形的周长和面积_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《用java求三角形的周长和面积》由会员分享,可在线阅读,更多相关《用java求三角形的周长和面积(10页珍藏版)》请在金锄头文库上搜索。

1、用java求三角形的周长和面积 用java求三角形的周长和面积?public class Js private double a; private double b; private double c; Js(double a,double b,double c) this.a = a; this.b = b; this.c = c; public double getA() return a; public double getB() return b; public double getC() return c; public double area() double s1=(a+b+c)

2、/2;double s2 = s1*(s1-a)*(s1-b)*(s1-c); double result = Math.sqrt(s2);return result; public double jsLength() return a+b+c; class Jxcomputer public static void main(String args) Js js = new Js(3,6,8); System.out.println("三角形三边长度分别为:"+"a:"+js.getA()+"tb:"+js.getB()+"

3、;tc:"+js.getC(); System.out.println("三角形面积为:"+js.area(); System.out.println("三角形周长为:"+js.jsLength(); 【编译与运行】 编译:javac Js.java 运行:java Jxcomputer 计算三角形的面积和周长./* (#)Point.java 2023-1-4*/package cn .robert.baidu.triangle;/* author R*/public class Point private int x;private int

4、 y;/* 构造函数*/public Point(int X, int Y) x=X;y=Y;public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void setY(int y) this.y = y;/* (#)Triangle.java 2023-1-4*/package cn .robert.baidu.triangle;/* author R*/* 构造函数*/public class Triangle private Point a;privat

5、e Point b;private Point c;public Triangle(Point A,Point B,Point C) a=A;b=B;c=C;/* 三条边的长度*/private double l1=Math.sqrt(Math.pow(a.getX()-b.getX(),2)+Math.pow(a.getY()-b.getY(), 2);private double l2=Math.sqrt(Math.pow(c.getX()-b.getX(),2)+Math.pow(c.getY()-b.getY(), 2);private double l3=Math.sqrt(Math

6、.pow(a.getX()-c.getX(),2)+Math.pow(a.getY()-c.getY(), 2);/* 定义Heron公式*/private double heron(double x, double y, double z) double p=(x+y+z)/2; double area=Math.sqrt(p*(p-x)*(p-y)*(p-z); return area; /* 周长是三边之和*/public double getVolume()return l1+l2+l3; /* 调用海伦公式求面积*/public double getArea()return hero

7、n(l1,l2,l3);/* 三点不一线*/public boolean isTriangle()return (a.getX()-b.getX()/(a.getY()-b.getY()!=(a.getX()-c.getX()/(a.getY()-c.getY();#交谈中请勿轻信汇款、中奖信息、陌生 ,勿使用外挂软件。候雷(78665444) 13:16:31public class OODemol002 public static void main(String args) Container c = new Container();Thread t1 = new Thread(new

8、Laundryman( c, 1);Thread t2 = new Thread(new Laundryman(c, 2);Thread t3 = new Thread(new WashingWorker(c, 1);Thread t4 = new Thread(new WashingWorker(c, 2);t1.start();t2.start();t3.start();t4.start();class Clothes int in;public Clothes(int index) this.in = index;class Container Clothes clothes = new

9、 Clothes6;int index = 0;public synchronized void push(Clothes c) while (index = clothes.length) try this.wait(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();this.notify();clothesindex = c;index+;public synchronized Clothes pop() while (index = 0) try this.wait

10、(); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();this.notify();index-;return clothesindex;/* 编写程序,使用多线程模拟实现洗衣房工作的示例。条件如下: 1. 洗衣工2人(对应2个线程) 2. 晾衣工2人 3. 共50件衣服 4.* 洗衣工每2秒洗1件衣服 5. 晾衣工每1秒晾1件衣服*/class Laundryman implements Runnable Container container;int id;public

11、Laundryman(Container container, int id) / TODO Auto-generated method stubthis.container = container;this.id = id;public void run() / TODO Auto-generated method stubClothes c = null;try for (int i = 0; i < 25; i+) Thread.sleep(2000);c = new Clothes(i);System.out.println("洗衣工" + id + &quo

12、t;洗了衣服:" + c.in);container.push(c); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();class WashingWorker implements Runnable Container container;int id;public WashingWorker(Container container, int id) this.container = container;this.id = id;public void run() / TODO Auto-generated method stubfor (int i = 0; i < 25; i+) Clothes c = container.pop();System.out.println("晾衣工" + id + "晾了衣服:" + c.in);try Thread.sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号