zoukankan      html  css  js  c++  java
  • 以点类 Point 及平面图形类 Plane 为基础设计三角形类 Triangle

    学习内容:以点类 Point 及平面图形类 Plane 为基础设计三角形类 Triangle

    代码示例:

    import java.util.Scanner;

    class Point{
    private double x;
    private double y;
    public Point(double x,double y) {
    this.x=x;
    this.y=y;
    System.out.println("Point Constructor run");
    }
    public void setX(double x) {//设置x坐标
    this.x=x;
    }
    public final double getX(){//返回x坐标
    return x;
    }
    public void setY(double y) {//设置y坐标
    this.y=y;
    }
    public final double getY(){//返回y坐标
    return y;
    }
    public void show() {//显示点的坐标
    System.out.print("Point(X="+x+",Y="+y+")");
    }
    public double dis(Point p)
    {
    double _x = Math.abs(this.x - p.x);
    double _y = Math.abs(this.y - p.y);
    return Math.sqrt(_x*_x+_y*_y);
    }
    }
    abstract class Plane{//抽象类Plane
    double length() {
    return 0;
    }
    double area() {
    return 0;
    }
    }
    public class Triangle extends Plane {

    private Point a;
    private Point b;
    private Point c;
    public Triangle(final Point a, final Point b, final Point c) {
    this.a=a;
    this.b=b;
    this.c=c;
    System.out.println("Triangle Constructorrun");
    }
    public void setA(final Point a) {
    this.a=a;
    }
    public Point getA() {
    return a;
    }
    public void setB(final Point b) {
    this.b=b;
    }
    public Point getB() {
    return b;
    }
    public void setC(final Point c) {
    this.c=c;
    }
    public Point getC() {
    return c;
    }
    public final void show() {
    System.out.print("Triangle(A=");
    a.show();
    System.out.print(",B=");
    b.show();
    System.out.print(",C=");
    c.show();
    }
    final double area() {
    double l1, l2, l3,s;
    l1 = a.dis(b);
    l2 = a.dis(c);
    l3 = b.dis(c);
    s = (l1 + l2 + l3) / 2;
    double area=Math.sqrt(s * (s - l1) * (s - l2) * (s - l3));
    System.out.println("Area="+area);
    return 0;
    }
    final double length() {
    double l1=a.dis(b);
    double l2=a.dis(c);
    double l3=b.dis(c);
    double length=l1+l2+l3;
    System.out.println("length="+length);
    return 0;
    }
    public static void main(String[] args) {
    double x,y;
    Point p1=new Point(0, 0);
    Point p2=new Point(1, 1);
    Point p3=new Point(2, 2);
    Triangle t1=new Triangle(p1, p1, p1);
    Triangle t2=t1;
    t1.show();
    System.out.println();//换行
    t1.area();
    t1.length();
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入x,y:");
    x=sc.nextDouble();
    y=sc.nextDouble();
    p1.setX(x);
    p1.setY(y);
    t2.setA(p1);
    t2.setB(p2);
    t2.setC(p3);
    t2.show();
    System.out.println();
    t2.area();
    t2.length();

    }

    }

    运行截图:

    遇到的问题:面积函数采用“return Math.sqrt(s * (s - l1) * (s - l2) * (s - l3));”的形式无法输出面积

    解决方法:设置变量area,使area=Math.sqrt(s * (s - l1) * (s - l2) * (s - l3));输出area,返回值设为0

    明天任务:以圆类 Circle 及立体图形类 Solid 为基础设计球类 Sphere

  • 相关阅读:
    iOS中静态库的制作——使用创建静态库项目的方式创建静态库
    在Xcode中自定义代码块——快速代码
    在博客园的第一篇博客
    委派
    As I Began to Love Myself
    When you are old
    RESOURCE SUMMARY
    First One
    js 动态生成div显示id
    js 层的显示和隐藏
  • 原文地址:https://www.cnblogs.com/zyj3955/p/13433552.html
Copyright © 2011-2022 走看看