abstract class shape{
創(chuàng)新互聯(lián)建站從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元沁水做網(wǎng)站,已為上家服務(wù),為沁水各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話: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;
}
/* 計(jì)算面積 */
private float countArea(float r)
{
s = PI * r * r;
return (s);
}
/* 計(jì)算體積 */
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) + "立方厘米");
}
}
首先定義一個(gè)接口:
public?interface?Inter?{
public?Double?getV(Double?r,Double?h);
}
定義一個(gè)類實(shí)現(xiàn)接口:
import?java.util.Scanner;
public?class?Demo?implements?Inter?{
public?static?void?main(String[]?args)?{
Scanner?in=new?Scanner(System.in);
System.out.println("請(qǐng)輸入圓錐底面圓的半徑:");
double?r=in.nextDouble();
System.out.println("請(qǐng)輸入圓錐的高:");
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;
}
}
如滿意請(qǐng)采納!
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 ("請(qǐng)輸入圓的高:");
try
{
height=Float.parseFloat(br.readLine());
System.out.println ("請(qǐng)輸入圓的半徑:");
r=Float.parseFloat(br.readLine());
area=(r*r*Math.PI);
System.out.println ("面積為:"+area);
break;
}
catch(Exception ex)
{
System.out.println ("請(qǐng)輸入數(shù)字!");
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}