zoukankan      html  css  js  c++  java
  • 【每日日报】第十二天

    1 今天写了以点类设计三角类:

    题目要求:

     

     程序源代码:

     package Plane;
    public class Point {
     double x;
        double y;
        Point()//无参构造
        {
          x=0;
          y=0;
             System.out.println("Point Constructor run");
     }
        Point(double xv,double yv)//有参构造
        {
       x=xv;
          y=yv;
          System.out.println("Point Constructor run");
      }
        Point(Point p)            //拷贝构造
        {
         x=p.x;
          y=p.y;
          System.out.println("Point CopyConstructor run");
        }
        void show()            //显示Point信息
        {
          System.out.print("("+x+","+y+")");
        }
       void setX(double xv){x=xv;}        //设置X坐标
       void setY(double yv){y=yv;}        //设置Y坐标
       double getX() {return x;}           //获取X坐标
       double getY() {return y;}           //获取Y坐标
    }
     
    package Plane;
    public class Plane extends Point {
     Plane(){}
     Plane(double xv,double yv){super(xv,yv);}
     Plane(Plane p){super(p);}
       double length(){return 0;}
       double area(){return 0;}
    }
     
    package Plane;
    public class Triangle extends Plane{
       Point a,b,c;
       Triangle()
       {
        a=new Point(0,0);
        b=new Point(0,0);
        c=new Point(0,0);
        System.out.println("Triangle Constructor run");
       }
       Triangle(Point aa,Point bb,Point cc)
       {
            a=aa;
            b=bb;
            c=cc;
               System.out.println("Triangle Constructor run");
          }
          Triangle(Triangle T)    //拷贝构造函数      
          {
              a=T.a;
              b=T.b;
              c=T.c;
              System.out.println("Triangle CopyConstructor run");
       }
        void setA(Point P)          //设置A顶点
       {
               a.setX(P.getX());
               a.setY(P.getY());
       }
        Point getA(){return a;}
        void setB(Point P)
        {
              b.setX(P.getX());
              b.setY(P.getY());
        }
        Point getB(){return b;}
        void setC(Point P)
        {
             c.setX(P.getX());
             c.setY(P.getY());
         }
        Point getC(){ return c; }
        void show()
        {
             System.out.print("Triangle(A=");
             a.show();
             System.out.print(",B=");
             b.show();
             System.out.print(",c=");
             c.show();
             System.out.print(")");
         }
         double area()
         {
               double S;
               Point p= new Point(a);
               double x1=p.getX();
               Point q= new Point(a);
               double y1=q.getY();
               Point m=new Point(b);
               double x2=m.getX();
               Point n=new Point(b);
               double y2=n.getY();
               Point Y=new Point(c);
               double x3=Y.getX();
               Point J=new Point(c);
               double y3=J.getY();
               double a1=Math.pow(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),0.5);
               double b1=Math.pow(((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)),0.5);
               double c1=Math.pow(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)),0.5);
               double p1=(a1+b1+c1)*0.5;
               S=Math.pow((p1*(p1-a1)*(p1-b1)*(p1-c1)),0.5);
               return S;
           }
           double length()
           {
               double L;
               double x1=a.getX();
               Point o=new Point(a);
               double y1=o.getY();
               Point m=new Point(b);
               double x2=m.getX();
               Point p=new Point(b);
               double y2=p.getY();
               Point n=new Point(c);
               double x3=n.getX();
               Point q=new Point(c);
               double y3=q.getY();
               L=Math.pow(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),0.5)+Math.pow(((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)),0.5)+Math.pow(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)),0.5);
               return L;
           }
    }
     
    package Plane;
    import java.util.Scanner;
    public class Tma extends Triangle{
     void length(Triangle p){System.out.println("Length="+p.length());}
     void area(Triangle p){System.out.println("Area="+p.area());}
        public static void main(String[] args)
        {
         double x,y;
            Point p1=new Point();
            Point p2=new Point(1,1);
            Point p3=new Point(2,2);
            Triangle t1=new Triangle();
            Triangle t2=new Triangle(t1);
            t1.show();
            System.out.println();
            Tma t=new Tma();
            t.area(t1);
            t.length(t1);
            Scanner in=new Scanner(System.in);
            x=in.nextDouble();
            y=in.nextDouble();
            p1.setX(x);
            p1.setY(y);
            t2.setA(p1);
            t2.setB(p2);
            t2.setC(p3);
            t2.show();
            System.out.println();
            t.area(t2);
            t.length(t2);
            in.close();
        }
    }

    运行截图:

     

    2 当子类有构造函数时,父类也要有构造函数

    3 明天继续写题

  • 相关阅读:
    5.3 java虚拟机的体系结构
    5.2 java虚拟机的生命周期
    3.11.5 doPrivileged()方法
    3.11.1 implies方法
    MT【178】平移不变性
    MT【177】三个乘积和
    MT【176】两两乘积
    MT【175】刚刚凑巧
    MT【174】凹凸无妨
    MT【173】齐次消元单变量
  • 原文地址:https://www.cnblogs.com/linmob/p/13332283.html
Copyright © 2011-2022 走看看