第1个回答 2013-10-27
package com.pt.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public class Triangle {
private Map<String, Point> points;
private Map<String, Double> lines;
private double circumference = 0;
private double area = 0;
public Triangle(double a1, double a2, double b1, double b2, double c1,
double c2) {
points = new HashMap<String, Point>();
lines = new HashMap<String, Double>();
points.put("A", new Point(a1, a2));
points.put("B", new Point(b1, b2));
points.put("C", new Point(c1, c2));
getLines();
getCircumference();
}
private void getLines() {
lines
.put("a", Math.sqrt(Math.pow(points.get("B").getX()
- points.get("C").getX(), 2)
+ Math.pow(points.get("B").getY()
- points.get("C").getY(), 2)));
lines
.put("b", Math.sqrt(Math.pow(points.get("A").getX()
- points.get("C").getX(), 2)
+ Math.pow(points.get("A").getY()
- points.get("C").getY(), 2)));
lines
.put("c", Math.sqrt(Math.pow(points.get("B").getX()
- points.get("A").getX(), 2)
+ Math.pow(points.get("B").getY()
- points.get("A").getY(), 2)));
}
private void getCircumference() {
for (double line : lines.values()) {
circumference += line;
}
}
private void getArea() {
}
public void showInfo() {
System.out.print("这个三角形的A点坐标为:( " + points.get("A").getX() + " , "
+ points.get("A").getY() + " )");
System.out.println(",该点所对应的边长a为:" + lines.get("a"));
System.out.print("这个三角形的B点坐标为:( " + points.get("B").getX() + " , "
+ points.get("B").getY() + " )");
System.out.println(",该点所对应的边长b为:" + lines.get("b"));
System.out.print("这个三角形的C点坐标为:( " + points.get("C").getX() + " , "
+ points.get("C").getY() + " )");
System.out.println(",该点所对应的边长c为:" + lines.get("c"));
System.out.println("这个三角形的周长为:a边 + b边 + c边 = " + lines.get("a") + " + "
+ +lines.get("b") + " + " + lines.get("c") + " = "
+ circumference);
}
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
new Triangle(sca.nextDouble(), sca.nextDouble(), sca.nextDouble(), sca
.nextDouble(), sca.nextDouble(), sca.nextDouble()).showInfo();
}
}
面积没算,角度没算,自己去查初中的数学吧!照代公式就OK了!
第2个回答 2013-10-27
/*
* @(#)Point.java 2008-1-4
*/
package cn.com.robert.baidu.triangle;
/**
* @author R
*/
public class Point {
private int x;
private int 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 2008-1-4
*/
package cn.com.robert.baidu.triangle;
/**
* @author R
*/
/*
* 构造函数
*/
public class Triangle {
private Point a;
private 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.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 heron(l1,l2,l3);
}
/*
* 三点不一线
*/
public boolean isTriangle(){
return ((a.getX()-b.getX())/(a.getY()-b.getY()))!=((a.getX()-c.getX())/(a.getY()-c.getY()));
}
}