运行结果如下:有不清楚的地方,请留言。没问题,请采纳下。谢谢
#include <stdio.h>#include <stdlib.h>
#define PI 3.14 //宏定义pi等于3.14
int main()
{
float r,h
槐模念 float c1//保存计算圆的周长
码橘 float Sb//保存圆的表面积
float Vb//保存圆柱的体积
printf("请输入圆的半径r=:\n")
scanf("%f",&r)//输入圆的半径
printf("请输入圆柱的高度h=:\n")
scanf("%f",&h)//输入圆柱的高
//计算半径为r的周长c1
c1 = 2*PI*r
//S=4πR^2:S球面积,R球半径,π:圆铅困周率
Sb = 4*PI*r*r
//计算圆柱的体积:底面积乘以高
Vb = PI*r*r*h
printf("半径为r的圆的周长是:%.2f\n",c1)
printf("\n半径为r的圆的表面积是:%.2f\n",Sb)
printf("\n半径为r的圆柱的体积是:%.2f\n",Vb)
return 0
}
1. 抽象形状类package com
//抽象的让衫形状类
public abstract class Shape{
}
2. 显示接口
package com
//接口
public interface IDisplay{
void display() //显示图形的基本信息
double getArea() //计算面积
double getGirth() //计算周长
}
3. 三角形类
package com.tri
import com.*
//三角形类
public class Triangle extends Shape implements IDisplay{
protected double a
protected double b
protected double c
public Triangle(double a, double b, double c){
this.a = a
this.b = b
this.c = c
}
@Override
public double getArea() {
double s = (a + b + c) / 2
return Math.sqrt(s*(s-a)*(s-b)*(s-c))
}
@Override
public double getGirth() {
return this.a + this.b + this.c
}
@Override
public void display() {
System.out.println("三角昌闭形")
System.out.println("边长:" + a + ", " + b + ", " + c)
}
}
4. 矩形类
package com.rec
import com.*
/耐滑裂/矩形类
public class Rectangle extends Shape implements IDisplay {
protected double width
protected double height
public Rectangle(double width, double height){
this.width = width
this.height = height
}
@Override
public double getArea() {
return this.width * this.height
}
@Override
public double getGirth() {
return 2 * ( this.width + this.height)
}
@Override
public void display() {
System.out.println("矩形")
System.out.println("宽:" + this.width + ", 高:" + this.height)
}
}
5. 圆类
package com.cir
import com.*
//圆类
public class Circle extends Shape implements IDisplay {
protected double radius
public Circle(double radius){
this.radius = radius
}
@Override
public double getArea() {
return Math.PI * this.radius * this.radius
}
@Override
public double getGirth() {
return 2 * Math.PI * this.radius
}
@Override
public void display() {
System.out.println("圆")
System.out.println("半径:" + this.radius)
}
}
6. 正多边形类
package com.mul
import com.*
//正多边形类
public class MulSide extends Shape implements IDisplay {
protected double side //边长
protected int n //边数
public MulSide(double side, int n){
this.side = side
this.n = n
}
@Override
public double getArea() {
return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4
}
@Override
public double getGirth() {
return this.side * this.n
}
@Override
public void display() {
System.out.println("正多边形")
System.out.println("连长:" + this.side + ", 边数:" + this.n)
}
}
7. 主类(测试类)
package com
import java.util.Scanner
import com.cir.Circle
import com.mul.MulSide
import com.rec.Rectangle
import com.tri.Triangle
public class Test22 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in)
double a, b, c
double width, height
double radius
double side
int n
IDisplay s
System.out.println("请输入三角形的基本信息")
System.out.print("边长1:")
a = scan.nextDouble()
System.out.print("边长2:")
b = scan.nextDouble()
System.out.print("边长3:")
c = scan.nextDouble()
s = new Triangle(a, b, c)
s.display()
System.out.println("三角形的面积:" + s.getArea())
System.out.println("三角形的周长:" + s.getGirth())
System.out.println("请输入矩形的基本信息")
System.out.print("宽:")
width = scan.nextDouble()
System.out.print("高:")
height = scan.nextDouble()
s = new Rectangle(width, height)
s.display()
System.out.println("矩形的面积:" + s.getArea())
System.out.println("矩的周长:" + s.getGirth())
System.out.println("请输入圆的基本信息")
System.out.print("半径:")
radius = scan.nextDouble()
s = new Circle(radius)
s.display()
System.out.println("圆的面积:" + s.getArea())
System.out.println("圆的周长:" + s.getGirth())
System.out.println("请输入正多边形的基本信息")
System.out.print("边长:")
side = scan.nextDouble()
System.out.print("边数:")
n = scan.nextInt()
s = new MulSide(side, n)
s.display()
System.out.println("正多边形的面积:" + s.getArea())
System.out.println("正多边形的周长:" + s.getGirth())
}
}
运行测试:
请输入三角形的基本信息
边长1:3
边长2:4
边长3:5
三角形
边长:3.0, 4.0, 5.0
三角形的面积:6.0
三角形的周长:12.0
请输入矩形的基本信息
宽:3
高:4
矩形
宽:3.0, 高:4.0
矩形的面积:12.0
矩的周长:14.0
请输入圆的基本信息
半径:2
圆
半径:2.0
圆的面积:12.566370614359172
圆的周长:12.566370614359172
请输入正多边形的基本信息
边长:2
边数:6
正多边形
连长:2.0, 边数:6
正多边形的面积:10.39230484541326
正多边形的周长:12.0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)