三种方法求多种形状面积之和

abstract class Shape { protected String type; protected double area, perim; public abstract void computeArea(); public abstract void computePerimeter(); public String toString () { return type + ":/n/tArea=" + area + "/n/tPerimeter=" + perim; } } class Rectangle extends Shape { protected double width, heigth; public Rectangle(double _width, double _heigth) { type = "Rectange"; width = _width; heigth= _heigth; } public void computeArea () { area = width * heigth; } public void computePerimeter() { perim = 2 * width * heigth; } public String toString () { return super.toString() + "/n/tWidth = " + width + "/n/tHeigth = " + heigth; } } class Circle extends Shape { protected double radius; public Circle(double _radius) { type = "Circle"; radius = _radius; } public void computeArea () { area = Math.PI * radius * radius; } public void computePerimeter() { perim = 2 * Math.PI * radius; } public String toString () { return super.toString() + "/n/tRadius = " + radius; } } class Square extends Rectangle { public Square (double _side) { super(_side, _side); type = "Square"; } } //采用抽象实现多种形状面积的累加 public class Main { public static void main (String[] args) { Shape[] shapes = new Shape[10]; double total_area =0.0; int k = 0; for(int i=0; i<shapes.length; ++i) { k = (int) Math.ceil(Math.random() * 3); p("k is :" + k); switch (k) { case 1: shapes[i] = new Circle(Math.random() * 10); break; case 2: shapes[i] = new Rectangle(Math.random() *10, Math.random() * 10); break; case 3: shapes[i] = new Square(Math.random() *10); break; } } for(int i=0; i<shapes.length; ++i) { shapes[i].computeArea(); total_area += shapes[i].area; } p("Total area is :" + total_area); } public static void p (Object o) { System.out.println(o); } }

interface Shape2 { public abstract double computeArea(); public abstract String toString(); } class Rectangle2 implements Shape2 { protected double width, heigth; String type = "Rectange2"; public Rectangle2(double _width, double _heigth) { width = _width; heigth= _heigth; } public double computeArea () { return width * heigth; } public String toString () { return super.toString() + "/n/tWidth = " + width + "/n/tHeigth = " + heigth; } } class Circle2 implements Shape2 { protected double radius; String type = "Circle2"; public Circle2(double _radius) { radius = _radius; } public double computeArea () { return Math.PI * radius * radius; } public String toString () { return super.toString() + "/n/tRadius = " + radius; } } class Square2 extends Rectangle2 { String type = "Square"; public Square2 (double _side) { super(_side, _side); } } //使用接口 public class MainInterface { public static void main (String[] args) { Shape2[] shapes = new Shape2[10]; double total_area =0.0; int k = 0; for(int i=0; i<shapes.length; ++i) { k = (int) Math.ceil(Math.random() * 3); p("k is :" + k); switch (k) { case 1: shapes[i] = new Circle2(Math.random() * 10); break; case 2: shapes[i] = new Rectangle2(Math.random() *10, Math.random() * 10); break; case 3: shapes[i] = new Square2(Math.random() *10); break; } } for(int i=0; i<shapes.length; ++i) { shapes[i].computeArea(); total_area += shapes[i].computeArea(); } p("Total area is :" + total_area); } public static void p (Object o) { System.out.println(o); } }

abstract class Shape { protected String type; protected double area, perim; public abstract void computeArea(); public abstract void computePerimeter(); public String toString () { return type + ":/n/tArea=" + area + "/n/tPerimeter=" + perim; } } class Rectangle extends Shape { protected double width, heigth; public Rectangle(double _width, double _heigth) { type = "Rectange"; width = _width; heigth= _heigth; } public void computeArea () { area = width * heigth; } public void computePerimeter() { perim = 2 * width * heigth; } public String toString () { return super.toString() + "/n/tWidth = " + width + "/n/tHeigth = " + heigth; } } class Circle extends Shape { protected double radius; public Circle(double _radius) { type = "Circle"; radius = _radius; } public void computeArea () { area = Math.PI * radius * radius; } public void computePerimeter() { perim = 2 * Math.PI * radius; } public String toString () { return super.toString() + "/n/tRadius = " + radius; } } class Square extends Rectangle { public Square (double _side) { super(_side, _side); type = "Square"; } } //使用Object类 public class MainObject { public static void main (String[] args) { Object[] shapes = new Shape[10]; double total_area =0.0; int k = 0; for(int i=0; i<shapes.length; ++i) { k = (int) Math.ceil(Math.random() * 3); p("k is :" + k); switch (k) { case 1: shapes[i] = new Circle(Math.random() * 10); break; case 2: shapes[i] = new Rectangle(Math.random() *10, Math.random() * 10); break; case 3: shapes[i] = new Square(Math.random() *10); break; } } for(int i=0; i<shapes.length; ++i) { if((shapes[i]) instanceof Circle) { ((Circle)shapes[i]).computeArea(); total_area += ((Circle) shapes[i]).area; } if((shapes[i]) instanceof Rectangle) { ((Rectangle)shapes[i]).computeArea(); total_area += ((Rectangle) shapes[i]).area; } if((shapes[i]) instanceof Square) { ((Square)shapes[i]).computeArea(); total_area += ((Square) shapes[i]).area; } } p("Total area is :" + total_area); } public static void p (Object o) { System.out.println(o); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值