java程序:计算三角形的面积和周长.

要求如下:
1.设计出point类(点类)。
2.设计出triangle类,至少包括其构造方法,以及获取面积和周长的方法等方法。
3.通过输入三点坐标的方法来确定以个三角形,并输出三角形的面积,周长等方法。
(面向对象编程,求高手详解)

/**
* 利用海伦公式求面积就行了
如果周长为l,p为周长的一半,a,b,c为三边
面积s的平方 = p*(p-a)*(p-b)*(p-c)
【注意】,开发在java中使用Math。sqrt(要开平方的数)
* */

public class Triangle {

private boolean isTriangle = false; //用于判断3个点去定的3个边是否能组成三角形

private double a = 0.0d; //è¾¹a
private double b = 0.0d; //è¾¹b
private double c = 0.0d; //è¾¹c
private double l = 0.0d; //周长
private double s = 0.0f; //面积

//(x1,y1),(x2,y2),(x3,y3)分别为三角形三个顶点坐标
public Triangle(int x1, int y1, int x2, int y2, int x3, int y3){
Point p1 = new Point(x1,y1);
Point p2 = new Point(x2,y2);
Point p3 = new Point(x3,y3);
a = getSide(p1,p2); //利用两点求边
b = getSide(p1,p3);
c = getSide(p2,p3);
if(isTriangle){ //如果是三角形再求周长和面积
getL();
getS();
}
}

//利用两个点来获取边
private double getSide(Point p1, Point p2){
double sub1 = 0.0d; //横坐标之差
double sub2 = 0.0d; //纵坐标之差
sub1 = p1.x > p2.x ? p1.x - p2.x : p2.x - p1.x;
sub1 = p1.y > p2.y ? p1.y - p2.y : p2.y - p1.y;
return Math.sqrt((sub1*sub1 + sub2*sub2));
}

//获取周长
private void getL(){
l = a + b + c;
}

//获取面积
public void getS(){
double p = l/2;
s = Math.sqrt(p*(p-a)*(p-b)*(p-c));
}

public void print(){
System.out.println("三边长为:"+a+","+b+","+c);
System.out.println("周长为:"+l);
System.out.println("面积为:"+s);
}

//点类(仅供 Triangle 类使用的内部类)
private class Point{
public int x = 0;
public int y = 0;
public Point(int px, int py){
x = px;
y = py;
}
}

public static void main(String[] args) {
//求(1,1)、(1,4)、(5,1)三个坐标组成的三角形的周长和面积
Triangle tri = new Triangle(1,1,1,4,5,1);//实例化一个三角形
tri.print(); //输出属性
}
}
温馨提示:答案为网友推荐,仅供参考
第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()));
}

}
相似回答