重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
abstract class shape{
创新互联建站从2013年成立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元沁水做网站,已为上家服务,为沁水各地企业和个人服务,联系电话:18980820575
protected static final double pi = 3.14;
abstract double volume();
}
class spherosome extends shape{
private double radius;
public spherosome(double radius){
this.radius = radius;
}
@override
double volume() {
//
圆球的体积公式=(4/3)πr^3
return pi*radius*radius*radius*4/3;
}
}
class cone extends shape{
private double radius, height;
public cone(double radius, double height){
this.radius = radius;
this.height = height;
}
@override
double volume() {
//
圆锥的体积公式=(1/3)hπr^2
return height * pi * radius * radius / 3;
}
}
public class test {
public void testspherosome(double radius){
spherosome spherosome = new spherosome(radius);
system.out.println("圆球的半径:" + radius + ", 圆球的体积:" + spherosome.volume());
}
public void testcone(double radius, double height){
cone cone = new cone(radius, height);
system.out.println("圆锥的底半径:" + radius + ",高:" + height + ", 圆锥的体积:" + cone.volume());
}
public static void main(string[] args) {
test test = new test();
test.testspherosome(1);
test.testcone(1, 1);
}
}
abstract class Shape{
protected static final double PI = 3.14;
abstract double volume();
}
class Spherosome extends Shape{
private double radius;
public Spherosome(double radius){
this.radius = radius;
}
@Override
double volume() {
// 圆球的体积公式=(4/3)πr^3
return PI*radius*radius*radius*4/3;
}
}
class Cone extends Shape{
private double radius, height;
public Cone(double radius, double height){
this.radius = radius;
this.height = height;
}
@Override
double volume() {
// 圆锥的体积公式=(1/3)hπr^2
return height * PI * radius * radius / 3;
}
}
public class Test {
public void testSpherosome(double radius){
Spherosome spherosome = new Spherosome(radius);
System.out.println("圆球的半径:" + radius + ", 圆球的体积:" + spherosome.volume());
}
public void testCone(double radius, double height){
Cone cone = new Cone(radius, height);
System.out.println("圆锥的底半径:" + radius + ",高:" + height + ", 圆锥的体积:" + cone.volume());
}
public static void main(String[] args) {
Test test = new Test();
test.testSpherosome(1);
test.testCone(1, 1);
}
}
package com.test;
public class Circle {
/* 圆半径 */
private float r;
/* 圆高 */
private float h;
/*圆的面积*/
private float s;
public static final float PI = 3.1415926f;
public Circle(float r, float h) {
this.r = r;
this.h = h;
}
/* 计算面积 */
private float countArea(float r)
{
s = PI * r * r;
return (s);
}
/* 计算体积 */
private float countVolume(float s, float h)
{
return (1.0f/3 * s * h);
}
public static void main(String[] args) {
Circle c = new Circle(3.2f,4.3f);
System.out.println("圆锥的底面积是 " + c.countArea(c.r) + "平方厘米");
System.out.println("圆锥的体积是 " + c.countVolume(c.s, c.h) + "立方厘米");
}
}
首先定义一个接口:
public interface Inter {
public Double getV(Double r,Double h);
}
定义一个类实现接口:
import java.util.Scanner;
public class Demo implements Inter {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入圆锥底面圆的半径:");
double r=in.nextDouble();
System.out.println("请输入圆锥的高:");
double h=in.nextDouble();
Demo de = new Demo();
de.getV(r, h);
}
@Override
public Double getV(Double r, Double h) {
double v;
v=(1d/3)*(Math.PI*r*r)*h;
System.out.println("圆锥的体积v="+v);
return v;
}
}
如满意请采纳!
import java.io.*;
public class Cal
{
public static void main(String[] args)
{
float height=0;
float r=0;
double area=0;
try
{
while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println ("请输入圆的高:");
try
{
height=Float.parseFloat(br.readLine());
System.out.println ("请输入圆的半径:");
r=Float.parseFloat(br.readLine());
area=(r*r*Math.PI);
System.out.println ("面积为:"+area);
break;
}
catch(Exception ex)
{
System.out.println ("请输入数字!");
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}