《用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();