大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Shape.java接口代码
创新互联服务项目包括曲阜网站建设、曲阜网站制作、曲阜网页制作以及曲阜网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,曲阜网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到曲阜省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
import java.util.Scanner;
interface JSolidFigure {
//表面积
void SurfaceArea();
//体积
void Volume();
}
//圆柱
class Cylinder implements JSolidFigure{
@Override
public void SurfaceArea() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入半径:");
double r=sc.nextDouble();
System.out.println("请输入高:");
double h=sc.nextDouble();
double surfaceArea=2*Math.PI*r*r+2*Math.PI*r*h;
System.out.println("表面积:"+surfaceArea);
}
@Override
public void Volume() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入半径:");
double r=sc.nextDouble();
System.out.println("请输入高:");
double h=sc.nextDouble();
double volume=Math.PI*r*r*h;
System.out.println("体积:"+volume);
}
}
//圆锥
class Cone implements JSolidFigure{
@Override
public void SurfaceArea() {
}
@Override
public void Volume() {
}
}
//球
class Ball implements JSolidFigure{
@Override
public void SurfaceArea() {
}
@Override
public void Volume() {
}
}
//长方
class Rectangular implements JSolidFigure{
@Override
public void SurfaceArea() {
}
@Override
public void Volume() {
}
}
//测试
public class SolidFigure{
public static void main(String[] args) {
//多态
JSolidFigure jsf=new Cylinder();
System.out.println("下面求圆柱表面积+++++++++++++++++++++++++++++++");
jsf.SurfaceArea();
System.out.println("下面求圆柱体积+++++++++++++++++++++++++++++++++");
jsf.Volume();
}
}
圆柱体体积公式:V=πr²h。
java中的Math类中提供了π常量:
public static final double PI = 3.14159265358979323846;可以直接使用。
求平方,可以使用Math.pow(r, 2)方法完成,也可以写成r*r。
指定圆柱的半径和高度,即可求出体积。
public class Cylinder {
// 半径(考虑精度问题,使用double)
private double radius;
private double height;
public Cylinder(double radius, double height) {
super();
this.radius = radius;
this.height = height;
}
// 获取当前圆柱对象的体积
public double getVolume() {
return getVolume(radius, height);
}
// 提供计算圆柱体积的通用方法。
public static double getVolume(double radius, double height) {
return Math.PI / 2 * Math.pow(radius, 2) * height;
}
public double getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public static void main(String[] args) {
// radius = 1.5, height = 5;
// 1、
Cylinder cylinder = new Cylinder(1.5f, 5);
System.out.println(cylinder.getVolume());
// 2、
System.out.println(Cylinder.getVolume(1.5f, 5));
}
}
package Area;
// 圆
public class circle {
// 半径
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
// 面积计算
public double getArea(double r){
this.r=r;
return r*r*3.14;
}
}
package Area;
// 圆柱体
public class circularCylinder extends circle{
// 高度
private double h;
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
// 体积
public void getValumn(double r,double h){
this.h=h;
System.out.println(this.getArea(r)*h);
}
}
package Area;
import java.util.Scanner;
// 主程序
public class testArea {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
circle c = new circle();
circularCylinder cc = new circularCylinder();
System.out.println("请输入圆的半径:");
// 手动设置圆半径
c.setR(scanner.nextDouble());
System.out.println("请输入圆柱体高度:");
// 设置圆柱体的高
cc.setH(scanner.nextDouble());
System.out.print("圆面积为:"+c.getArea(c.getR()));
System.out.print("相应的圆柱体体积为:");
cc.getValumn(c.getR(),cc.getH());
}
}
class Cylinder extends Circle
{
double PI=super.getπ();
double r=super.getR();
int h=5;
//定义圆柱体的高
public double getArea()
//求圆柱体表面积
{
return 2*PI*r*r+2*PI*r*h;
}
public double getSize2()
//求 圆柱体体积
{
return PI*r*r*h;
}
}