跪求JAVA高手,我有几个程序想要高手帮忙编一下!急急急!QQ:362168209~

1.请编写一个类,该类实现如下功能
定义一个Point类,该类描述平面上的一个点。
该点有坐标(x, y),其中x和y为浮点型;
该点有一个名字,如”A”,”B”等
2.定义一个描述“线段”的Line类
一条线端类有两个端的A,B属性成员;其中A和B分别是Point类,该类有成员方法可以返回该线段的长度,有成员方法可以读取线段的名字,也就是连个端点的名字的字符串连接,如上例的线段名字就是”AB”或者”BA”,有方法可以改变任何一个端点的坐标。有方法可以求线段中点到坐标原点的距离
3.定义一个三角型的Triangle类
该三角型Triangle类有表示三个顶点的Point属性, 该类有方法可以返回该三角形的面积的方法;有方法成员可以返回三条边边长。有方法成员判断是不是三角型。

Point类

/**
*
* @author MrCao.tk
*
*/

public class Point {
private float x;
private float y;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Point(float x, float y) {
super();
this.x = x;
this.y = y;
}
public Point() {
super();
}
}

-------------------------------------
Line类

/**
*
* @author MrCao.tk
*
*/

public class Line {
private Point a;
private Point b;
public Point getA() {
return a;
}
public void setA(Point a) {
this.a = a;
}
public Point getB() {
return b;
}
public void setB(Point b) {
this.b = b;
}

public Line() {
super();
}
public Line(Point a, Point b) {
super();
this.a = a;
this.b = b;
}

/**
* 计算直线的长度
* @return 长度
*/
public float getLength() {
float _x = (a.getX() - b.getX()) * (a.getX() - b.getX());
float _y = (a.getY() - b.getY()) * (a.getY() - b.getY());
return (float)Math.sqrt(_x + _y);
}

/**
* 线段中点到坐标原点的距离
* @return 距离
*/
public float getOriginalPointLength() {
//计算出中点
Point midpoint = new Point((a.getX() + b.getX() / 2) , (a.getY() + b.getY()) / 2);
//把中点与原点组装成直线
Line line = new Line(new Point(0 , 0), midpoint);
return line.getLength();
}
}

-------------------------------------
Triangle类

/**
*
* @author MrCao.tk
*
*/

public class Triangle {
private Point a;
private Point b;
private Point c;

public Point getA() {
return a;
}
public void setA(Point a) {
this.a = a;
}
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 Triangle(Point a, Point b, Point c) {
super();
this.a = a;
this.b = b;
this.c = c;
}

public Triangle() {
super();
}

/**
* 计算三角形的周长
* @return 周长
* @throws Exception
*/
public float getLength() throws Exception {
if(isTriangle() == false) {
throw new Exception("不是三角形");
}
Line line1 = new Line(a, b);
Line line2 = new Line(b, c);
Line line3 = new Line(c, a);
return line1.getLength() + line2.getLength() + line3.getLength();
}

/**
* 计算三角形面积
* @return 面积
* @throws Exception
*/
public float getAcreage() throws Exception {
if(isTriangle() == false) {
throw new Exception("不是三角形");
}
//用海伦公式计算
Line line1 = new Line(a, b);
Line line2 = new Line(b, c);
Line line3 = new Line(c, a);
float p = (line1.getLength() + line2.getLength() + line3.getLength()) / 2;
return (float) Math.sqrt(p * (p - line1.getLength()) * (p - line2.getLength()) * (p - line3.getLength()));
}

/**
* 判断是否三角形
* @return 能否组成三角形
* @throws Exception
*/
public boolean isTriangle() {
Line line1 = new Line(a, b);
Line line2 = new Line(b, c);
Line line3 = new Line(c, a);
try {
if(line1.getLength() + line2.getLength() > line3.getLength()
&& line1.getLength() + line3.getLength() > line2.getLength()
&& line2.getLength() + line3.getLength() > line1.getLength()
&& line1.getLength() - line2.getLength() < line3.getLength()
&& line1.getLength() - line3.getLength() < line2.getLength()
&& line2.getLength() - line3.getLength() < line1.getLength()
&& line2.getLength() - line1.getLength() < line3.getLength()
&& line3.getLength() - line1.getLength() < line2.getLength()
&& line3.getLength() - line2.getLength() < line1.getLength()) {
return true;
}
} catch(Exception e) {
return false;
}
return false;
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-06-05
我也是新手,写了给你看看:
Point类:
package com.java.tuxing;

public class Point
{
double x;
double y;

public Point(double x,double y)
{
this.x=x;
this.y=y;
}
}

Line类:
package com.java.tuxing;
import java.util.Scanner;
public class Line
{
Point p1,p2;
public Line(Point p1,Point p2){
this.p1=p1;
this.p2=p2;
}

public double LineLength()
{
return Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
}

public String LineName()
{

String str=p1.toString()+p2.toString();
return str.toString();
}

public Point ChangeXY(Point p)
{
System.out.println("输入坐标值 x,y:");
Scanner sc=new Scanner(System.in);
p.x=sc.nextDouble();
p.y=sc.nextDouble();
return p;
}

public double ZhongdianLength()
{
double m=(p1.x+p2.x)/2;
double n=(p1.y+p2.y)/2;
return Math.sqrt(Math.pow(m, 2)+Math.pow(n, 2));
}
}

Triangle类:
package com.java.tuxing;

public class Triangle {

Point p1,p2,p3;
Line l1=new Line(p1,p2);
Line l2=new Line(p1,p3);
Line l3=new Line(p2,p3);
public Triangle(Point p1,Point p2,Point p3)
{
this.p1=p1;
this.p2=p2;
this.p3=p3;
}

public double bianchang(Point p1,Point p2)
{
return Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
}

public double mianji()
{
double p12=Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
double p13=Math.sqrt(Math.pow((p3.x-p1.x), 2)+Math.pow((p3.y-p1.y), 2));
double p23=Math.sqrt(Math.pow((p3.x-p2.x), 2)+Math.pow((p3.y-p2.y), 2));
double pp=(p12+p13+p23)/2;
return Math.sqrt(pp*(pp-p12)*(pp-p13)*(pp-p23));
}

public boolean isTriangle()
{
if((p2.y-p1.y)/(p2.x-p1.x)==(p3.y-p1.y)/(p3.x-p1.x)) //判断两直线斜率,不要弄特殊点以免斜率不存在
{
return false;
}
else
return true;
}
}

测试类:
package com.java.tuxing;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point X=new Point(1,1);
Point Y=new Point(4,4);

Point A=new Point(1,1);
Point B=new Point(2,3);
Point C=new Point(3,4);

Line L1=new Line(X,Y);

Triangle T=new Triangle(A,B,C);

double xylength=L1.LineLength();
String xyname=L1.LineName();
double zdlength=L1.ZhongdianLength();

double ablength=T.bianchang(A, B);
double aclength=T.bianchang(A, C);
double bclength=T.bianchang(B, C);
double abcarea=T.mianji();
boolean bl=T.isTriangle();
System.out.println("点:------------------------------------------");
System.out.println("点X的坐标是: ("+X.x+","+X.y+")");
System.out.println("点Y的坐标是: ("+Y.x+","+Y.y+")");

System.out.println("直线:-----------------------------------------");
System.out.println("点X的坐标是: ("+X.x+","+X.y+")");
System.out.println("点Y的坐标是: ("+Y.x+","+Y.y+")");
System.out.println("XY的边长是:"+xylength);
System.out.println("XY的边长名称是:"+xyname);
System.out.println("XY的边中点距离原点:"+zdlength);

System.out.println("三角形:---------------------------------------");
System.out.println("点A的坐标是: ("+A.x+","+A.y+")");
System.out.println("点B的坐标是: ("+B.x+","+B.y+")");
System.out.println("点C的坐标是: ("+C.x+","+C.y+")");

System.out.println("AB的边长是:"+ablength);
System.out.println("AC的边长是:"+aclength);
System.out.println("BC的边长是:"+bclength);
System.out.println("三角形ABC的面积是:"+abcarea);
System.out.println("T是三角形吗?:"+bl);
}
}

输出结果:
点:------------------------------------------
点X的坐标是: (1.0,1.0)
点Y的坐标是: (4.0,4.0)
直线:-----------------------------------------
点X的坐标是: (1.0,1.0)
点Y的坐标是: (4.0,4.0)
XY的边长是:4.242640687119285
XY的边长名称是:com.java.tuxing.Point@61de33com.java.tuxing.Point@14318bb
XY的边中点距离原点:3.5355339059327378
三角形:---------------------------------------
点A的坐标是: (1.0,1.0)
点B的坐标是: (2.0,3.0)
点C的坐标是: (3.0,4.0)
AB的边长是:2.23606797749979
AC的边长是:3.605551275463989
BC的边长是:1.4142135623730951
三角形ABC的面积是:0.5000000000000032
T是三角形吗?:true

直线名称不知道怎么弄...
第2个回答  2011-06-05
/**
* @author shy2850
*/
public class Point {

/**点的名称*/
private Character name;
public float x;
public float y;

public Point(float x, float y) {
this('P',x,y);
}
public Point(Character name, float x, float y) {
this.name = name;
this.x = x;
this.y = y;
}
public Point() {
}

public void setName(Character name) {
this.name = name;
}

public Character getName() {
return name;
}

public String toString() {
return name+"(" + x + "," + y + ")";
}
}

public class Line {

private Point beginPoint;
private Point endPoint;

public Line(Point beginPoint, Point endPoint) {
this.beginPoint = beginPoint;
this.endPoint = endPoint;
}

public String getName(){
if(null == beginPoint.getName() || null == endPoint.getName())throw new RuntimeException("线段中的点缺少名称!");
return beginPoint.getName()+""+endPoint.getName();
}

public Point getBeginPoint() {
return beginPoint;
}

public void setBeginPoint(Point beginPoint) {
this.beginPoint = beginPoint;
}

public Point getEndPoint() {
return endPoint;
}

public void setEndPoint(Point endPoint) {
this.endPoint = endPoint;
}

public double getLength(){
return Math.sqrt((beginPoint.x-endPoint.x)*(beginPoint.x-endPoint.x)+(beginPoint.y-endPoint.y)*(beginPoint.y-endPoint.y));
}

public double getDistenceOfCenter(){
return Math.sqrt((beginPoint.x+endPoint.x)*(beginPoint.x+endPoint.x)+(beginPoint.y+endPoint.y)*(beginPoint.y+endPoint.y))/4;
}

public String toString() {
return "Line:"+getName();
}

}

public class Trangle {

private Point p1;
private Point p2;
private Point p3;

private Line[] lines = new Line[3];

public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public Point getP3() {
return p3;
}
public void setP3(Point p3) {
this.p3 = p3;
}

public Trangle(Point p1, Point p2, Point p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}

public Line[] getLines(){
if(null == lines){
lines[0] = new Line(p1, p2);
lines[1] = new Line(p2, p3);
lines[2] = new Line(p3, p1);
}
return lines;
}

public boolean isTrangle(){
Line[] lines = getLines();
double l0 = lines[0].getLength();
double l1 = lines[1].getLength();
double l2 = lines[2].getLength();
return (l0+l1>l2)&&(l0+l2>l1)&&(l1+l2>l0);
}

public double getArea(){
Line[] lines = getLines();
if(!isTrangle())throw new RuntimeException("不是三角形!");
double l0 = lines[0].getLength();
double l1 = lines[1].getLength();
double l2 = lines[2].getLength();
double s = (l0+l1+l2)/2;
return Math.sqrt(s*(s-l0)*(s-l1)*(s-l2));
}

public String toString() {
return "Trangle:△"+p1.getName()+p2.getName()+p3.getName();
}

}
第3个回答  2011-06-04
贴出来吧,虽然我也是新手,但觉得程序员要有共享的精神
第4个回答  2011-06-04
把它贴出了吧。
相似回答