用JAVA编写一个计算立方体、球体和圆柱的体积
5个回答
2013-11-15
展开全部
class A{//类A计算立方体体积 }class B{//类A计算球体体积 } class C{//类A计算圆柱体积 }//主类public class test{ static{ System.out.println("请输入1、2、3对应立方体、球体和圆柱的体积计算..."); }public static void main(String args[]){ if(args.length>1 || Integer.parseint(args[0])<1|| Integer.parseint(args[0])>3){//判断输入是否符合标准 System.out.println("错误的选择,程序自动退出.."); System.exit(1); } else{ int x = Integer.parseint(args[0]) ; switch(x){ case 1: //调用A类 case 2: //调用B类 defualt: //调用C类 } } }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-15
展开全部
package com.answer.dataStrutures;import java.math.*;public class BulkAccount
{
String figure; //形状
double r;
double h; public BulkAccount(String figure, double r, double h) //对变量的初始化
{
this.figure = figure;
this.h = h;
this.r = r;
} public double bulkAccount(String figure, double r, double h)
{
double result = 0;
if (figure.trim().equals("cube")) //立方体体积计算
{
result = r * r * r;
}
if (figure.trim().equals("cylinder")) //圆柱体体积计算
{
result = Math.PI * r * r * h;
}
if (figure.trim().equals("ball")) //球体积计算
{
result = (4 / 3) * Math.PI * r * r * r;
} return result;
} public static void main(String[] args)
{
/*BulkAccount ba = new BulkAccount("cube",2,0);
System.out.println(ba.bulkAccount(ba.figure, ba.r, ba.h));*/
}} 没用测数据了,意思写了你看看吧。
{
String figure; //形状
double r;
double h; public BulkAccount(String figure, double r, double h) //对变量的初始化
{
this.figure = figure;
this.h = h;
this.r = r;
} public double bulkAccount(String figure, double r, double h)
{
double result = 0;
if (figure.trim().equals("cube")) //立方体体积计算
{
result = r * r * r;
}
if (figure.trim().equals("cylinder")) //圆柱体体积计算
{
result = Math.PI * r * r * h;
}
if (figure.trim().equals("ball")) //球体积计算
{
result = (4 / 3) * Math.PI * r * r * r;
} return result;
} public static void main(String[] args)
{
/*BulkAccount ba = new BulkAccount("cube",2,0);
System.out.println(ba.bulkAccount(ba.figure, ba.r, ba.h));*/
}} 没用测数据了,意思写了你看看吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-15
展开全部
这个题目挺简单的,主要考察方法的重载;先去了解重载的定义。下面是我做的..希望你能接纳!package com.softeem.TZ;public class caculateVl {
final double PI = 3.14;
double cubeLengh;
double cubeWigth;
double cubeHigh;
double cubeVl;
double sphereRaius;
double sphereVl;
double columnRaius;
double columnHigh;
double columnVl;
// 3/4 Pi rRR
public void caculateVl(double cubeLengh, double cubeWigth, double cubeHigh) {
this.cubeLengh = cubeLengh;
this.cubeWigth = cubeWigth;
this.cubeHigh = cubeHigh;
}
public void caculateVl(double sphereRaius) {
this.sphereRaius = sphereRaius;
} public void caculateVl(double columnRaius, double columnHigh) {
this.columnRaius = columnRaius;
this.columnHigh = columnHigh;
}
public void caculateCube(double cubeLengh, double cubeWigth, double cubeHigh){
cubeVl = cubeLengh * cubeWigth * cubeHigh;
System.out.println("立方体面积 :"+cubeVl);
}
public void caculateSphere(double sphereRaius){
sphereVl = PI * sphereRaius * sphereRaius *sphereRaius*3/4;
System.out.println("球体面积 :"+sphereVl);
}
public void caculateColumn(double columnRaius, double columnHigh){
columnVl = Math.PI * columnRaius * columnRaius*columnHigh;
System.out.println("圆柱体面积 :"+columnVl);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
caculateVl CV = new caculateVl();
CV.caculateCube(cubeLengh, cubeWigth, cubeHigh)//括号里面输入值就可以了。
CV.caculateSphere(sphereRaius)
CV.caculateColumn(columnRaius, columnHigh)
}}
final double PI = 3.14;
double cubeLengh;
double cubeWigth;
double cubeHigh;
double cubeVl;
double sphereRaius;
double sphereVl;
double columnRaius;
double columnHigh;
double columnVl;
// 3/4 Pi rRR
public void caculateVl(double cubeLengh, double cubeWigth, double cubeHigh) {
this.cubeLengh = cubeLengh;
this.cubeWigth = cubeWigth;
this.cubeHigh = cubeHigh;
}
public void caculateVl(double sphereRaius) {
this.sphereRaius = sphereRaius;
} public void caculateVl(double columnRaius, double columnHigh) {
this.columnRaius = columnRaius;
this.columnHigh = columnHigh;
}
public void caculateCube(double cubeLengh, double cubeWigth, double cubeHigh){
cubeVl = cubeLengh * cubeWigth * cubeHigh;
System.out.println("立方体面积 :"+cubeVl);
}
public void caculateSphere(double sphereRaius){
sphereVl = PI * sphereRaius * sphereRaius *sphereRaius*3/4;
System.out.println("球体面积 :"+sphereVl);
}
public void caculateColumn(double columnRaius, double columnHigh){
columnVl = Math.PI * columnRaius * columnRaius*columnHigh;
System.out.println("圆柱体面积 :"+columnVl);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
caculateVl CV = new caculateVl();
CV.caculateCube(cubeLengh, cubeWigth, cubeHigh)//括号里面输入值就可以了。
CV.caculateSphere(sphereRaius)
CV.caculateColumn(columnRaius, columnHigh)
}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-15
展开全部
具体一点行不行?都给定什么参数,比如给定了高什么之类的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-15
展开全部
有测试数据!
/*
* Geometry.java
*/
public class Geometry {
public static int instance = 0;
public Geometry () {
instance++;
}
public static double calculateRectangle(double width, double height) {
return width * height;
}
public static double calculateCube(double width, double height, double length) {
return width * height * length;
}
public static double calculateSphere (double radius) {
return (4 * Math.PI * radius * radius * radius) /3;
}
public static void main(String[] args) {
System.out.println(" 1 " + calculateRectangle(4, 5));
System.out.println(" Instance = " + instance);
System.out.println(" 2 " + calculateCube(4, 5, 6));
System.out.println(" Instance = " + instance);
System.out.println(" 3 " + calculateSphere(3));
System.out.println(" Instance = " + instance);
}
}
/*
* Geometry.java
*/
public class Geometry {
public static int instance = 0;
public Geometry () {
instance++;
}
public static double calculateRectangle(double width, double height) {
return width * height;
}
public static double calculateCube(double width, double height, double length) {
return width * height * length;
}
public static double calculateSphere (double radius) {
return (4 * Math.PI * radius * radius * radius) /3;
}
public static void main(String[] args) {
System.out.println(" 1 " + calculateRectangle(4, 5));
System.out.println(" Instance = " + instance);
System.out.println(" 2 " + calculateCube(4, 5, 6));
System.out.println(" Instance = " + instance);
System.out.println(" 3 " + calculateSphere(3));
System.out.println(" Instance = " + instance);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |