?? ex2.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 + -