⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex2.java

📁 讲述各种各样的java初始编程 了解编程
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package chenchao2;import java.util.Random;/** * * @author qjxy */public class Ex2 {    public static void main(String[] args) {        Ex2 test = new Ex2();        test.doTest();    }    private void doTest() {        Random r = new Random();        Point a = new Point(r.nextInt(10), r.nextInt(20));        Point b = new Point(r.nextInt(10), r.nextInt(20));        Point c = new Point(r.nextInt(10), r.nextInt(20));        TriAngle t = new TriAngle(a, b, c);        System.out.println(t);    }}class TriAngle {    private Point a;    private Point b;    private Point c;    private double area;    private double longth;    private double ab;    private double bc;    private double ac;    public Point getA() {        return a;    }    public void setA(Point a) {        this.a = a;    }    public double getArea() {        double p = this.getLongth()/2;        //use the Helen eqution to compute the area of a Triangle        //假设有一个三角形,边长分别为a、b、c,三角形的面积S可由以下公式求得:         //S=√[p(p-a)(p-b)(p-c)]         //而公式里的p为半周长:         //p=(a+b+c)/2         area = Math.sqrt(p* (p-ab)* (p-ac)* (p-bc));        return area;    }    public Point getB() {        return b;    }    public void setB(Point b) {        this.b = b;    }    public Point getC() {        return c;    }    public void setC(Point c) {        this.c = c;    }    public double getLongth() {        longth = ab + ac + bc;        return longth;    }    public TriAngle(Point a, Point b, Point c) {        this.a = a;        this.b = b;        this.c = c;        this.ab = this.getAb();        this.ac = this.getAc();        this.bc = this.getBc();        this.area = this.getArea();        this.longth = this.getLongth();    }    private double getDistance(Point s, Point d) {        double distance = 0;        distance = Math.sqrt(Math.pow(s.getX() - d.getX(), 2) + Math.pow(s.getY() - d.getY(), 2));        return distance;    }    public double getAb() {        ab = getDistance(a, b);        return ab;    }    public double getAc() {        ac = getDistance(a, c);        return ac;    }    public double getBc() {        bc = getDistance(b, c);        return bc;    }    @Override    public String toString() {        String stra = a.toString();        String strb = b.toString();        String strc = c.toString();        String str = "Point A is:\t" + stra + '\n' +                      "Point B is:\t" + strb + '\n' +                      "Point C is:\t" + strc + '\n' +                     "Side AB is:\t" + this.getAb() + '\n' +                     "Side AC is:\t" + this.getAc() + '\n' +                     "Side BC is:\t" + this.getBc() + '\n';        str = str + "The Area is :\t " + this.getArea() + '\n';        str = str + "The Longth is :\t " + this.getLongth() + '\n';        return str;    }}class Point {    private int x;    private int 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;    }    public Point(int x, int y) {        this.x = x;        this.y = y;    }    @Override    public String toString() {        String str = "[the x is :\t" + this.x + '\t' + "the y is :\t" + this.y + ']';        return str;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -