JAVA【案例4-5】图形的面积与周长计算程序

【图形的面积与周长计算程序】

1、案例描述

长方形和圆形都属于几何图形,都有周长和面积,并且它们都有自己的周长和面积计算公式。使用抽象类的知识设计一个程序,可以计算不同图形的面积和周长。

2、案例目的

(1)学会分析“图形的面积与周长计算程序”案例的实现思路

(2)完成“图形的面积与周长计算程序”案例的代码编写、编译及运行

(3)理解和掌握面向对象的设计过程

(4)掌握抽象类及抽象方法的使用

3、运行结果

4、代码实

package cn.itcast.example;

abstract class Shape{
	//抽象方法:获取面积
	public abstract double getArea();
	//抽象方法:获取周长
	public abstract double getPerimeter();
}
class Circle extends Shape{
	private double radius = 0;//圆的半径
	private final static double PI = 3.14;//常量,圆周率
	//有参构造,初始化圆半径
	public Circle(double radius) {
		this.radius = radius;
	}
	//求圆面积
	public double getArea() {
		return PI*radius*radius;
	}
	//求圆周长
	public double getPerimeter() {
		return 2*radius*PI;
	}
}
class Rectangle extends Shape{
	private double length = 0;//长方形的长
	private double width = 0;//长方形的宽
	//有参构造,初始化长方形的长和宽
	public Rectangle(double length,double width) {
		super();
		this.length = length;
		this.width = width;
	}
	public double getArea() {
		return this.length*this.width;
	}
	public double getPerimeter() {
		return 2*(this.length+this.width);
	}
}
class ShapeCaculate{
//可以计算任意shape子类的面积
	public void calArea (Shape shape) {
		System.out.println(shape.getArea());
	}
	//可以计算任意shape子类的周长
	public void calPerimater(Shape shape) {
		System.out.println(shape.getPerimeter());
	}
}
public class example {

	public static void main (String[] arge) {
		//创建图形计算器
		ShapeCaculate sc = new ShapeCaculate();
		//创建长方形和圆形对象
		Shape rectangle = new Rectangle(3,4);
		Circle circle = new Circle(3);
		//求长方形和圆形的面积
		System.out.println("长方形的面积:");
		sc.calArea(rectangle);
		System.out.println("圆形的面积:");
		sc.calArea(circle);
		//求长方形和圆形的周长
		System.out.println("长方形的周长:");
		sc.calPerimater(rectangle);
		System.out.println("圆形的周长");
		sc.calPerimater(circle);
	}
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用继承来计算图形面积周长,具体的实现方式如下: 1. 创建一个形状(Shape)类作为父类,包含计算面积周长的方法。 ```java public abstract class Shape { public abstract double getArea(); public abstract double getPerimeter(); } ``` 2. 创建各种图形的子类,如圆形(Circle)、矩形(Rectangle)等,继承形状类,并实现父类中的抽象方法。 ```java public class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getPerimeter() { return 2 * Math.PI * radius; } } ``` ```java public class Rectangle extends Shape { private double width; private double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } @Override public double getArea() { return width * height; } @Override public double getPerimeter() { return 2 * (width + height); } } ``` 3. 在主函数中创建图形对象,并调用相应的方法计算面积周长。 ```java public static void main(String[] args) { Shape circle = new Circle(5); System.out.println("圆形的面积为:" + circle.getArea()); System.out.println("圆形的周长为:" + circle.getPerimeter()); Shape rectangle = new Rectangle(4, 6); System.out.println("矩形的面积为:" + rectangle.getArea()); System.out.println("矩形的周长为:" + rectangle.getPerimeter()); } ``` 输出结果: ``` 圆形的面积为:78.53981633974483 圆形的周长为:31.41592653589793 矩形的面积为:24.0 矩形的周长为:20.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值