yuanzhui的代码没有错,还需要看看Rectangle,Area,Volume这个三个接口/类里面有没有yuanzhui必须要实现的代码
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了江岸免费建站欢迎大家使用!
按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("红色");
System.out.println(circle1.toString());
System.out.println("圆的面积为"+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("蓝色");
System.out.println(circlar1.toString());
System.out.println("圆锥的体积为"+circlar1.volume());
}
}
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();
}
}
}
首先定义一个接口:
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;
}
}
如满意请采纳!