设计Shape类,Circle类,Retangle类,等求不同形状面积和周长,并排序

avatar 2017年04月26日22:54:17 1 3379 views
博主分享免费Java教学视频,B站账号:Java刘哥
主要考察:Java的继承和重写的知识

没有排序功能的代码如下:

  1. package com.liuyanzhao;
  2. class Shape {
  3.     String name;
  4.     public String  getName() {
  5.         return name;
  6.     }
  7.     public double  getArea() {//面积
  8.         return 0;
  9.     }
  10.     public double getGirth() {//周长
  11.         return 0;
  12.     }
  13. }
  14. class Square extends Shape {//正方形
  15.     double a;
  16.     public Square(double a) {
  17.         this.name = "正方形";
  18.         this.a = a;
  19.     }
  20.     public double getArea() {
  21.         return a*a;
  22.     }
  23.     public double getGirth() {
  24.         return 4*a;
  25.     }
  26. }
  27. class Rectangle extends Shape { //长方形
  28.     double a,b;
  29.     public Rectangle(double a,double b) {
  30.         this.name = "长方形";
  31.         this.a = a;
  32.         this.b = b;
  33.     }
  34.     public double getArea() {
  35.         return a*b;
  36.     }
  37.     public double getGirth() {
  38.         return (a+b)*2;
  39.     }
  40. }
  41. class Triangle extends Shape { //三角形
  42.     double a,b,c;
  43.     public Triangle(double a,double b,double c) {
  44.         this.name = "三角形";
  45.         this.a = a;
  46.         this.b = b;
  47.         this.c = c;
  48.     }
  49.     public double getArea() {
  50.         double p = (a+b+c)/2;
  51.         double s = (double)Math.sqrt(p*(p-a)*(p-b)*(p-c));
  52.         return s;
  53.     }
  54.     public double getGirth() {
  55.         return (a+b+c);
  56.     }
  57. }
  58. class Circle extends Shape { //圆
  59.     public static final double PI = 3.14;
  60.     double r;
  61.     public Circle(double r) {
  62.         this.name = "圆";
  63.         this.r = r;
  64.     }
  65.     public double getArea() {
  66.         return (double)(PI*r*r);
  67.     }
  68.     public double getGirth() {
  69.         return 2*PI*r;
  70.     }
  71. }
  72. public class Demo {
  73.     public static void main(String[] args) {
  74.         Shape []s = {new Square(3),new Rectangle(34),new Triangle(345),new Circle(3)};
  75.         for(int i = 0;i<s.length;i++) {
  76.             System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
  77.         }
  78.     }
  79. }
运行结果: 注意: 1、Squale,Circle,Retcangle,Triangle等类的方法不要传参 2、子类对父类的方法的重写,保持一致(是否传参,返回类型) 3、不要为了少写一个方法盲目继承,继承之间必须有附属(父子)关系     同时我们再加一个功能,根据面积和周长大小进行排序

加排序功能代码如下:

  1. package com.liuyanzhao;
  2. class Shape {
  3.     String name;
  4.     public String  getName() {
  5.         return name;
  6.     }
  7.     public double  getArea() {//面积
  8.         return 0;
  9.     }
  10.     public double getGirth() {//周长
  11.         return 0;
  12.     }
  13.     public void compareArea(Shape []ss,int len) //根据面积大小升序排序
  14.     {
  15.          Shape s;
  16.          int i,j;
  17.          for(i=0;i<len;i++)
  18.          {
  19.              for(j=i+1;j<len;j++)
  20.              {
  21.                  if(ss[j].getArea()<ss[i].getArea())
  22.                  {
  23.                      s=ss[i];
  24.                      ss[i]=ss[j];
  25.                      ss[j]=s;
  26.                  }
  27.              }
  28.          }
  29.     }
  30.     public void compareGirth(Shape []ss,int len) //根据周长大小升序排序
  31.     {
  32.          Shape s;
  33.          int i,j;
  34.          for(i=0;i<len;i++)
  35.          {
  36.              for(j=i+1;j<len;j++)
  37.              {
  38.                  if(ss[j].getGirth()<ss[i].getGirth())
  39.                  {
  40.                      s=ss[i];
  41.                      ss[i]=ss[j];
  42.                      ss[j]=s;
  43.                  }
  44.              }
  45.          }
  46.       }
  47. }
  48. class Square extends Shape {//正方形
  49.     double a;
  50.     public Square(double a) {
  51.         this.name = "正方形";
  52.         this.a = a;
  53.     }
  54.     public double getArea() {
  55.         return a*a;
  56.     }
  57.     public double getGirth() {
  58.         return 4*a;
  59.     }
  60. }
  61. class Rectangle extends Shape { //长方形
  62.     double a,b;
  63.     public Rectangle(double a,double b) {
  64.         this.name = "长方形";
  65.         this.a = a;
  66.         this.b = b;
  67.     }
  68.     public double getArea() {
  69.         return a*b;
  70.     }
  71.     public double getGirth() {
  72.         return (a+b)*2;
  73.     }
  74. }
  75. class Triangle extends Shape { //三角形
  76.     double a,b,c;
  77.     public Triangle(double a,double b,double c) {
  78.         this.name = "三角形";
  79.         this.a = a;
  80.         this.b = b;
  81.         this.c = c;
  82.     }
  83.     public double getArea() {
  84.         double p = (a+b+c)/2;
  85.         double s = (double)Math.sqrt(p*(p-a)*(p-b)*(p-c));
  86.         return s;
  87.     }
  88.     public double getGirth() {
  89.         return (a+b+c);
  90.     }
  91. }
  92. class Circle extends Shape { //圆
  93.     public static final double PI = 3.14;
  94.     double r;
  95.     public Circle(double r) {
  96.         this.name = "圆";
  97.         this.r = r;
  98.     }
  99.     public double getArea() {
  100.         return (double)(PI*r*r);
  101.     }
  102.     public double getGirth() {
  103.         return 2*PI*r;
  104.     }
  105. }
  106. public class Demo {
  107.     public static void main(String[] args) {
  108.         Shape []s = {new Square(3),new Rectangle(34),new Triangle(345),new Circle(2)};
  109.         for(int i = 0;i<s.length;i++) {
  110.             System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
  111.         }
  112.         System.out.println("-------------------------------------");
  113.         System.out.println("根据面积大小排序:");
  114.         new Shape().compareArea(s,s.length);
  115.         for(int i = 0;i<s.length;i++) {
  116.             System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
  117.         }
  118.         System.out.println("-------------------------------------");
  119.         System.out.println("根据周长大小排序:");
  120.         new Shape().compareGirth(s,s.length);
  121.         for(int i = 0;i<s.length;i++) {
  122.             System.out.println(s[i].getName()+",面积="+s[i].getArea()+",周长="+s[i].getGirth());
  123.         }
  124.     }
  125. }
运行结果:     本文地址:http://liuyanzhao.com/3396.html 转载请注明  
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0